forked from HarukaKinen/Cardentify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (39 loc) · 1.46 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import requests
import json
from collections import Counter
TOKEN = os.environ.get("TOKEN")
repo = "HarukaKinen/Cardentify"
r = requests.get(
f"https://api.github.com/repos/{repo}/contents/Cards",
headers={"Authorization": f"Bearer {TOKEN}"},
)
repos = r.json()
cards = []
banks = []
for bank in repos:
if bank["type"] == "dir":
path = bank["path"]
name = bank["name"]
r = requests.get(bank["url"])
card_data = r.json()
for i in card_data:
if i["name"] == "data.json":
r = requests.get(i["download_url"])
data = r.json()
for l in data["cards"]:
issuer = data["bank"]
image = f'{i["download_url"].replace("data.json", l["description"])}.{l["ext"]}'
path = f'{i["html_url"].replace("data.json", l["description"])}.{l["ext"]}'
l.update({"image": image, "url": path, "issuer": issuer})
cards.append(l)
banks.append(issuer)
country_counts = Counter([bank['country'] for bank in banks])
banks = sorted(banks, key=lambda x: x['english_name'])
banks = sorted(banks, key=lambda x: (-country_counts[x['country']], x['country']))
if banks:
with open("bank.json", "w") as f:
json.dump(banks, f, indent=4)
cards = sorted(cards, key=lambda x: x["issuer"]["english_name"])
with open("data.json", "w") as f:
json.dump(cards, f, indent=4)