Created
August 26, 2020 14:01
-
-
Save jtattermusch/b2a4b2c18915f720f590b8b1065e2d31 to your computer and use it in GitHub Desktop.
Fetch nuget download stats (version 2020-08-26)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Copyright 2020 The gRPC Authors | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
"""Fetch download stats (for the last 6 weeks) for gRPC C# nuget packages.""" | |
import json | |
import urllib.request | |
def _fetch_stats_for_package(package_name): | |
"""Fetch download stats for given nuget package""" | |
# Fetches statistics over the last 6 weeks, grouped by package version. | |
# This is the same data as displayed by e.g. https://www.nuget.org/stats/packages/Grpc.Core?groupby=Version | |
url = 'https://www.nuget.org/stats/reports/packages/%s?groupby=Version' % package_name | |
response = urllib.request.urlopen(url) | |
json_data = response.read() | |
data = json.loads(json_data) | |
version_stats = [] | |
for row in data['Table']: | |
if len(row) != 2: | |
raise Exception('Malformed row %s' % row) | |
package_version = row[0]["Data"] | |
download_count = int(row[1]["Data"]) | |
version_stats.append((package_version, download_count)) | |
return { | |
'last_updated_utc': _parse_last_updated_utc_field(data['LastUpdatedUtc']), | |
'downloads_total': data['Total'], | |
'downloads_by_version': version_stats } | |
def _parse_last_updated_utc_field(value): | |
"""Parses the field in format /Date(1598436958000)/""" | |
prefix = '/Date(' | |
suffix = ')/' | |
if value.startswith(prefix): | |
value = value[len(prefix):] | |
if value.endswith(suffix): | |
value = value[:-len(suffix)] | |
return int(value) | |
def _print_summary_for_package(package_name): | |
"""Fetch download stats for given nuget package""" | |
stats = _fetch_stats_for_package(package_name) | |
print('%s\t stats last updated timestamp\t %s' % (package_name, stats['last_updated_utc'])) | |
print('%s\t total downloads over last 6 weeks\t %s' % (package_name, stats['downloads_total'])) | |
print() | |
for row in stats['downloads_by_version']: | |
print('%s\t %s\t %s' % (package_name, row[0], row[1])) | |
print() | |
print() | |
_print_summary_for_package('Grpc.Core.Api') | |
_print_summary_for_package('Grpc.Core') | |
_print_summary_for_package('Grpc.Tools') | |
_print_summary_for_package('Grpc.Net.Client') | |
_print_summary_for_package('Grpc.AspNetCore.Server') | |
_print_summary_for_package('Grpc.Net.Client.Web') | |
_print_summary_for_package('Google.Protobuf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment