|
| 1 | +import json |
| 2 | +import os |
| 3 | +from datetime import datetime |
| 4 | +from github import Github |
| 5 | + |
| 6 | +year = str(datetime.now().year) |
| 7 | +date_format = '%Y-%m-%d' |
| 8 | +date_today = datetime.now().strftime(date_format) |
| 9 | + |
| 10 | +# Check if today is already processed |
| 11 | +if os.path.exists('data/last_updated'): |
| 12 | + with open('data/last_updated', 'r') as file: |
| 13 | + last_updated = file.readline().strip() |
| 14 | + if last_updated == date_today: |
| 15 | + print('Today has already been processed!') |
| 16 | + exit(0) |
| 17 | + |
| 18 | +# =====================> Get downloads |
| 19 | +github = Github() |
| 20 | +repo = github.get_repo('FAST-Imaging/FAST') |
| 21 | +releases = repo.get_releases() |
| 22 | +downloads_total = 0 |
| 23 | +downloads_per_OS = { |
| 24 | + 'Windows': 0, |
| 25 | + 'Linux': 0, |
| 26 | + 'macOS x86_64': 0, |
| 27 | + 'macOS arm64': 0, |
| 28 | +} |
| 29 | +downloads_per_type = { |
| 30 | + 'Archive': 0, |
| 31 | + 'Python Wheel': 0, |
| 32 | + 'Installer': 0, |
| 33 | +} |
| 34 | +for release in releases: |
| 35 | + assets = release.get_assets() |
| 36 | + for asset in assets: |
| 37 | + downloads_total += asset.download_count |
| 38 | + |
| 39 | + if 'win' in asset.name: |
| 40 | + downloads_per_OS['Windows'] += asset.download_count |
| 41 | + elif 'ubuntu' in asset.name or 'linux' in asset.name: |
| 42 | + downloads_per_OS['Linux'] += asset.download_count |
| 43 | + elif 'mac' in asset.name: |
| 44 | + if 'arm64' in asset.name: |
| 45 | + downloads_per_OS['macOS arm64'] += asset.download_count |
| 46 | + else: |
| 47 | + downloads_per_OS['macOS x86_64'] += asset.download_count |
| 48 | + else: |
| 49 | + print(f'Error parsing OS for {asset.name}') |
| 50 | + continue |
| 51 | + |
| 52 | + if asset.name.endswith('.tar.xz') or asset.name.endswith('.zip') or asset.name.endswith('.tar.gz'): |
| 53 | + downloads_per_type['Archive'] += asset.download_count |
| 54 | + elif asset.name.endswith('.exe') or asset.name.endswith('.deb'): |
| 55 | + downloads_per_type['Installer'] += asset.download_count |
| 56 | + elif asset.name.endswith('.whl'): |
| 57 | + downloads_per_type['Python Wheel'] += asset.download_count |
| 58 | + else: |
| 59 | + print(f'Error parsing file type for {asset.name}') |
| 60 | + continue |
| 61 | + |
| 62 | + |
| 63 | +# ========================> Total downloads |
| 64 | +if os.path.exists(f'data/{year}.json'): |
| 65 | + with open(f'data/{year}.json', 'r') as file: |
| 66 | + data = json.load(file) |
| 67 | + # Get sum of all |
| 68 | + sum = 0 |
| 69 | + for date, value in data: |
| 70 | + sum += value |
| 71 | + data[date_today] = downloads_total - sum |
| 72 | +else: |
| 73 | + data = {date_today: downloads_total} |
| 74 | + |
| 75 | +with open(f'data/{year}.json', 'w') as file: |
| 76 | + json.dump(data, file, indent=4) |
| 77 | + |
| 78 | + |
| 79 | +# ========================> Per OS |
| 80 | +if os.path.exists(f'data/{year}-OS.json'): |
| 81 | + with open(f'data/{year}-OS.json', 'r') as file: |
| 82 | + data = json.load(file) |
| 83 | + for OS in data.keys(): |
| 84 | + # Find sum for OS |
| 85 | + sum = 0 |
| 86 | + for date, value in data[OS]: |
| 87 | + sum += value |
| 88 | + data[OS][date_today] = downloads_per_OS[OS] - sum |
| 89 | +else: |
| 90 | + data = {} |
| 91 | + for OS in downloads_per_OS.keys(): |
| 92 | + data[OS] = {date_today: downloads_per_OS[OS]} |
| 93 | + |
| 94 | +with open(f'data/{year}-OS.json', 'w') as file: |
| 95 | + json.dump(data, file, indent=4) |
| 96 | + |
| 97 | + |
| 98 | +# ========================> Per file type |
| 99 | +if os.path.exists(f'data/{year}-file-type.json'): |
| 100 | + with open(f'data/{year}-file-type.json', 'r') as file: |
| 101 | + data = json.load(file) |
| 102 | + for type in data.keys(): |
| 103 | + # Find sum for OS |
| 104 | + sum = 0 |
| 105 | + for date, value in data[type]: |
| 106 | + sum += value |
| 107 | + data[type][date_today] = downloads_per_type[type] - sum |
| 108 | +else: |
| 109 | + data = {} |
| 110 | + for type in downloads_per_type.keys(): |
| 111 | + data[type] = {date_today: downloads_per_type[type]} |
| 112 | + |
| 113 | +with open(f'data/{year}-file-type.json', 'w') as file: |
| 114 | + json.dump(data, file, indent=4) |
| 115 | + |
| 116 | +# =====================> Update last_updated file |
| 117 | +with open('data/last_updated', 'w') as file: |
| 118 | + file.write(date_today) |
0 commit comments