Skip to content

Instantly share code, notes, and snippets.

@real-yfprojects
Last active August 26, 2022 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save real-yfprojects/888af25d1054f3619abe3f5ed0d3ce7a to your computer and use it in GitHub Desktop.
Save real-yfprojects/888af25d1054f3619abe3f5ed0d3ce7a to your computer and use it in GitHub Desktop.
Calculate the total number of downloads from flathub in a given time span for the *vorta* app.
"""
Calculate the total number of downloads for a flathub app.
"""
import json
import logging
import sys
from datetime import date, timedelta
from typing import Generator, Tuple
import requests
logging.basicConfig(level=logging.INFO, format='%(message)s')
URL_TEMPLATE = 'https://flathub.org/stats/{y:0>4}/{m:0>2}/{d:0>2}.json'
def make_url_list(from_: date, to: date) -> Generator[Tuple[str, date], None, None]:
td = to - from_
for day in range(td.days):
dt = from_ + timedelta(day)
url = URL_TEMPLATE.format(y=dt.year, m=dt.month, d=dt.day)
yield url, dt
def download_json(url: str):
response = requests.get(url)
response.raise_for_status()
js = json.loads(response.content)
return js
def extract_app_stats(json, app_name):
apps = json['refs']
if app_name not in apps:
raise ValueError(f"App {app_name} couldn't be found")
return apps[app_name]
class DownloadAggregator(Generator[None, dict, Tuple[int, int]]):
def __init__(self) -> None:
self.downloads = {}
self.delta_updates = {}
@property
def total(self):
return sum(self.downloads.values()), sum(self.delta_updates.values())
def __next__(self) -> None:
pass
def __iter__(self):
return self
def throw(self, e):
pass
def send(self, daily_data: dict) -> None:
for arch, data in daily_data.items():
logging.info(data)
self.downloads[arch] = self.downloads.get(arch, 0) + data[0]
self.delta_updates[arch] = self.delta_updates.get(arch, 0) + data[1]
def stat(app_name: str, last_n_days: int):
url_list = make_url_list(date.today() - timedelta(days=last_n_days), date.today())
aggregator = DownloadAggregator()
for url, dt in url_list:
logging.info("Processing json for " + str(dt))
js = extract_app_stats(download_json(url), app_name)
aggregator.send(js)
return aggregator
if __name__ == '__main__':
days = int(sys.argv[1])
agg = stat('com.borgbase.Vorta', days)
downloads, deltas = agg.total
print('================================================================')
print(f"Vorta was downloaded {downloads} times in the past {days} days.")
print(f"There were {deltas} delta downloads.")
print()
for arch, dwds in agg.downloads.items():
out = f"{arch}: {dwds} downloads"
if arch in agg.delta_updates:
out += f" {agg.delta_updates[arch]} delta updates."
print(out)
@real-yfprojects
Copy link
Author

@m3nu Might be interesting for you.

@m3nu
Copy link

m3nu commented Aug 25, 2022

Yeah. No idea how many users there are across all packaging channels. But looks decent and rising.

@real-yfprojects
Copy link
Author

Last release has about 1,700 MacOS downloads (from github). There are about 5 arm, 18 aarch64 and 6,100 x86 downloads on flathub since last release. In the past month about 400 people downloaded vorta via pypi. That makes a total of about 8,200 users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment