forked from Fevol/obsidian-data-processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreleases.py
166 lines (129 loc) · 6.79 KB
/
releases.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import pandas as pd
import re
import os
import datetime
import json
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from util import download_from_url, save_dataframe, generate_color_palette, draw_bar_graph
def graph_bar_chart(df, linux_downloads, windows_downloads, mac_downloads):
ind = np.arange(len(linux_downloads))
sns.set(style="whitegrid")
plt.bar(ind, linux_downloads, 0.6, color='#d24413', label='linux')
plt.bar(ind, windows_downloads, 0.6, color='#0072cb', label='windows', bottom=linux_downloads)
plt.bar(ind, mac_downloads, 0.6, color='#06cb98', label='mac', bottom=linux_downloads + windows_downloads)
# Get unique version values
versions = df['version'].unique()
plt.xlim(-0.5, len(versions) - .5)
# Set x-axis labels with rotation
plt.xticks(ind, versions, rotation=45)
patches = [mpatches.Patch(color='#d24413', label="Linux"),
mpatches.Patch(color='#0072cb', label="Windows"),
mpatches.Patch(color='#06cb98', label="MacOS")]
plt.legend(handles=patches, loc='upper left')
plt.show()
def graph_releases(configuration):
if configuration['download'] or configuration['provided_file']:
if configuration['download']:
data = download_from_url(
"https://api.github.com/repos/obsidianmd/obsidian-releases/releases",
"raw-data/releases.json" if configuration['save_data'] else "")
timestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S")
else:
with open(configuration['provided_file'], 'r') as f:
data = f.read()
# Find if filename contains timestamp
if re.search(r'\d{4}.?\d{2}.?\d{2}', configuration['provided_file']):
timestamp = re.search(r'\d{4}.?\d{2}.?\d{2}', configuration['provided_file']).group(0) + 'T00-00-00'
else:
timestamp = datetime.datetime.fromtimestamp(os.path.getmtime(configuration['provided_file'])).strftime(
"%Y-%m-%dT%H-%M-%S")
entries = []
for release in data:
version = release['name']
# Turn 0.12.4 to 0.12.04 if last digit is single number
version = '.'.join([s.zfill(2) if s != '0' else '0' for s in version.split('.')])
created_at = release['created_at']
# Convert 2022-07-26T18:40:50Z datetime to epoch
created_at = datetime.datetime.strptime(created_at, '%Y-%m-%dT%H:%M:%SZ').timestamp()
for i, asset in enumerate(release['assets']):
file_type = asset['browser_download_url'].split('/')[-1]
file_type = file_type[re.search(r"\d+.\d+.\d+", file_type).end()+1:]
operation_system = ''
if(file_type.endswith('asar.gz')):
continue
elif (file_type.endswith('dmg')):
operation_system = 'mac'
elif (file_type.endswith('exe')):
operation_system = 'windows'
else:
operation_system = 'linux'
downloads = asset['download_count']
entries.append([version, operation_system, downloads, file_type, created_at])
df = pd.DataFrame(entries, columns=['version', 'os', 'downloads', 'type', 'created_at'])
# To csv with timestamp of current date
if configuration['save_data']:
save_dataframe(df, "processed-data/releases.csv", timestamp)
else:
# Get last fetched releases.json file in the raw folder
theme_files = [f for f in os.listdir("processed-data") if re.match(r"releases_\d{4}.*\.csv", f)]
theme_files.sort()
if not theme_files:
print("No releases.json file found in processed data folder")
return
else:
with open(os.path.join("processed-data", theme_files[-1]), "r") as file:
df = pd.read_csv(file)
df = df.groupby(['version', 'created_at', 'os']).sum().reset_index()
# Filter versions with less than 10000 downloads
df = df.groupby('version').filter(lambda x: x['downloads'].sum() > 10000)
new_rows = []
for version in df['version'].unique():
df_version = df[df['version'] == version]
for operation_system in ['mac', 'windows', 'linux']:
if df_version[df_version['os'] == operation_system].empty:
new_rows.append([version, operation_system, 0, 0])
df = df.append(new_rows, ignore_index=True)
# Sort by version and os
df = df.sort_values(by=['version', 'os'])
if configuration['save_data']:
df.to_csv(os.path.join("processed-data", f"releases_versions.csv"), index=False)
if configuration["chronological"]:
plt.figure(figsize=(20, 6))
if configuration["logarithm"]:
plt.yscale('log')
linux_downloads = df[df['os'] == 'linux']['downloads'].values
windows_downloads = df[df['os'] == 'windows']['downloads'].values
mac_downloads = df[df['os'] == 'mac']['downloads'].values
graph_bar_chart(df, linux_downloads, windows_downloads, mac_downloads)
if configuration["normalize"] or configuration["sorted"]:
total_downloads = df.groupby('version').sum()['downloads'].values
if configuration["normalize"]:
plt.figure(figsize=(20, 6))
linux_downloads = 100 * df[df['os'] == 'linux']['downloads'].values / total_downloads
windows_downloads = 100 * df[df['os'] == 'windows']['downloads'].values / total_downloads
mac_downloads = 100 * df[df['os'] == 'mac']['downloads'].values / total_downloads
plt.ylim(0, 100)
plt.gca().yaxis.set_major_formatter(plt.FuncFormatter('{}%'.format))
graph_bar_chart(df, linux_downloads, windows_downloads, mac_downloads)
if configuration["sorted"]:
plt.figure(figsize=(20, 6))
if configuration["logarithm"]:
plt.yscale('log')
# Group by versions and sort by total downloads
versions = df.groupby('version').sum().sort_values(by='downloads', ascending=False)
df['total_downloads'] = df['version'].map(versions['downloads'])
df = df.sort_values(by=['total_downloads', 'os'], ascending=False)
# Sort original df by the sorted versions
linux_downloads = df[df['os'] == 'linux']['downloads'].values
windows_downloads = df[df['os'] == 'windows']['downloads'].values
mac_downloads = df[df['os'] == 'mac']['downloads'].values
graph_bar_chart(df, linux_downloads, windows_downloads, mac_downloads)
if __name__ == '__main__':
graph_releases({
'chronological': False,
'normalize': False,
'sorted': True
})