Mapping_ya_tracker/asana_users.py

49 lines
1.3 KiB
Python
Raw Normal View History

import csv
import json
import os
import sys
base_dir = os.path.dirname(os.path.realpath(__file__))
asana_data_json = os.path.join(base_dir, './data/data_asana.json')
sys.stdout.reconfigure(encoding='utf-8')
def extract_unique_names_and_ids(data):
""" Получение из json-файла данных о пользователях """
unique_names = set()
unique_ids = set()
for item in data["data"]:
assignee = item["assignee"]
followers = item["followers"]
if assignee:
unique_names.add(assignee["name"])
unique_ids.add(assignee["gid"])
if followers:
for follower in followers:
unique_names.add(follower["name"])
unique_ids.add(follower["gid"])
return unique_names, unique_ids
def export_to_csv(unique_names, unique_ids):
with open("output.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile, delimiter=",")
writer.writerow(["Name", "ID"])
for name, id in zip(unique_names, unique_ids):
writer.writerow([name, id])
file = open(asana_data_json, "r", encoding="utf8")
json_data = json.loads(file.read())
unique_names, unique_ids = extract_unique_names_and_ids(json_data)
export_to_csv(unique_names, unique_ids)