@@ -65,31 +65,63 @@ def get_publishing_data(starting_year, starting_month):
65
65
df = pd .DataFrame (data ,columns = HEADERS )
66
66
return df
67
67
68
- def get_most_active_data (starting_year , starting_month ):
68
+ def process_most_active_data (most_active ):
69
+ resulting_dfs = {}
70
+ for key in most_active .keys ():
71
+ if key == 'dates' :
72
+ dates = most_active ['dates' ]
73
+ else :
74
+ data = {'dates' : dates }
75
+ for entry in most_active [key ]['unique' ]:
76
+ data [entry ] = [None ] * len (most_active ['dates' ])
77
+ for i in range (len (dates )):
78
+ date = dates [i ]
79
+ for entry in most_active [key ][date ]:
80
+ item = list (entry .values ())[0 ]
81
+ value = list (entry .values ())[1 ]
82
+ data [item ][i ] = value
83
+ df = pd .DataFrame (data , columns = most_active [key ]['unique' ])
84
+ df ['date' ] = dates
85
+ resulting_dfs [key ] = df
86
+ return resulting_dfs
87
+
88
+ def most_active_data_append_unique (most_active , json_results , top_prop , key ):
89
+ for item in json_results [top_prop ]:
90
+ if item [key ] not in most_active [top_prop ]['unique' ]:
91
+ most_active [top_prop ]['unique' ].append (item [key ])
92
+
93
+ def extract_most_active_data_from_json (most_active , json_results , year , month ):
94
+ top_publishers = 'topMostActivePublishingUsers'
95
+ top_extensions = 'topNamespaceExtensions'
96
+ top_extension_versions = 'topNamespaceExtensionVersions'
97
+ top_downloads = 'topMostDownloadedExtensions'
98
+
99
+ no_data = True
100
+ top_props = [top_publishers , top_extensions , top_extension_versions , top_downloads ]
101
+ for top_prop in top_props :
102
+ if len (json_results [top_prop ]) > 0 :
103
+ no_data = False
104
+ break
105
+
106
+ if no_data :
107
+ return
69
108
70
- def process_data (most_active ):
71
- resulting_dfs = {}
72
- for key in most_active .keys ():
73
- if key == 'dates' :
74
- dates = most_active ['dates' ]
75
- else :
76
- data = {'dates' : dates }
77
- for entry in most_active [key ]['unique' ]:
78
- data [entry ] = [None ] * len (most_active ['dates' ])
79
- for i in range (len (dates )):
80
- date = dates [i ]
81
- for entry in most_active [key ][date ]:
82
- item = list (entry .values ())[0 ]
83
- value = list (entry .values ())[1 ]
84
- data [item ][i ] = value
85
- df = pd .DataFrame (data , columns = most_active [key ]['unique' ])
86
- df ['date' ] = dates
87
- resulting_dfs [key ] = df
88
- return resulting_dfs
89
-
90
- current_year = date .today ().year
91
- current_month = date .today ().month
109
+ year_month = '%s/%s' % (month , str (year )[2 :])
110
+ most_active ['dates' ].append (year_month )
92
111
112
+ most_active [top_publishers ][year_month ] = json_results [top_publishers ]
113
+ most_active_data_append_unique (most_active , json_results , top_publishers , 'userLoginName' )
114
+
115
+ most_active [top_extensions ][year_month ] = json_results [top_extensions ]
116
+ most_active_data_append_unique (most_active , json_results , top_extensions , 'namespace' )
117
+
118
+ most_active [top_extension_versions ][year_month ] = json_results [top_extension_versions ]
119
+ most_active_data_append_unique (most_active , json_results , top_extension_versions , 'namespace' )
120
+
121
+ most_active [top_downloads ][year_month ] = json_results [top_downloads ]
122
+ most_active_data_append_unique (most_active , json_results , top_downloads , 'extensionIdentifier' )
123
+
124
+ def get_most_active_data (starting_year , starting_month ):
93
125
most_active = {
94
126
'dates' : [],
95
127
'topMostActivePublishingUsers' : {
@@ -105,44 +137,22 @@ def process_data(most_active):
105
137
'unique' : []
106
138
}
107
139
}
108
- for year in range (starting_year , current_year + 1 ):
109
- for month in range (1 , 13 ):
110
- if year == starting_year and month >= starting_month or \
111
- starting_year < year < current_year or year == current_year and month <= current_month :
112
- url = '%sadmin/report?year=%s&month=%s&token=%s' % (API_ENDPOINT , year , month , ACCESS_TOKEN )
113
- response = requests .get (url )
114
- if response .status_code == 200 :
115
- try :
116
- json_results = response .json ()
117
- if len (json_results ['topMostActivePublishingUsers' ]) > 0 or len (json_results ['topNamespaceExtensions' ]) > 0 or \
118
- len (json_results ['topNamespaceExtensionVersions' ]) > 0 or len (json_results ['topMostDownloadedExtensions' ]) > 0 :
119
- year_month = '%s/%s' % (month , str (year )[2 :])
120
- most_active ['dates' ].append (year_month )
121
- most_active ['topMostActivePublishingUsers' ][year_month ] = json_results ['topMostActivePublishingUsers' ]
122
- for item in json_results ['topMostActivePublishingUsers' ]:
123
- if item ['userLoginName' ] not in most_active ['topMostActivePublishingUsers' ]['unique' ]:
124
- most_active ['topMostActivePublishingUsers' ]['unique' ].append (item ['userLoginName' ])
125
- most_active ['topNamespaceExtensions' ][year_month ] = json_results ['topNamespaceExtensions' ]
126
- for item in json_results ['topNamespaceExtensions' ]:
127
- if item ['namespace' ] not in most_active ['topNamespaceExtensions' ]['unique' ]:
128
- most_active ['topNamespaceExtensions' ]['unique' ].append (item ['namespace' ])
129
- most_active ['topNamespaceExtensionVersions' ][year_month ] = json_results ['topNamespaceExtensionVersions' ]
130
- for item in json_results ['topNamespaceExtensionVersions' ]:
131
- if item ['namespace' ] not in most_active ['topNamespaceExtensionVersions' ]['unique' ]:
132
- most_active ['topNamespaceExtensionVersions' ]['unique' ].append (item ['namespace' ])
133
- most_active ['topMostDownloadedExtensions' ][year_month ] = json_results ['topMostDownloadedExtensions' ]
134
- for item in json_results ['topMostDownloadedExtensions' ]:
135
- if item ['extensionIdentifier' ] not in most_active ['topMostDownloadedExtensions' ]['unique' ]:
136
- most_active ['topMostDownloadedExtensions' ]['unique' ].append (item ['extensionIdentifier' ])
137
- except JSONDecodeError :
138
- json_results = None
139
- print ("Error decoding JSON results for %s" % url )
140
- else :
141
- print ("%s error processing results for %s" % (response .status_code , url ))
142
-
143
- resulting_dfs = process_data (most_active )
144
140
145
- return resulting_dfs
141
+ today = date .today ()
142
+ start_date = date (starting_year , starting_month , 1 )
143
+ while start_date .year < today .year or (start_date .year == today .year and start_date .month < today .month ):
144
+ url = '%sadmin/report?year=%s&month=%s&token=%s' % (API_ENDPOINT , start_date .year , start_date .month , ACCESS_TOKEN )
145
+ response = requests .get (url )
146
+ if response .status_code == 200 :
147
+ try :
148
+ extract_most_active_data_from_json (most_active , response .json (), start_date .year , start_date .month )
149
+ except JSONDecodeError :
150
+ print ("Error decoding JSON results for %s" % url )
151
+ else :
152
+ print ("%s error processing results for %s" % (response .status_code , url ))
153
+ start_date = start_date + relativedelta (months = 1 )
154
+
155
+ return process_most_active_data (most_active )
146
156
147
157
if __name__ == '__main__' :
148
158
reports = get_available_reports ()
0 commit comments