Skip to content

Instantly share code, notes, and snippets.

@tgr
Created May 12, 2020 12:13
Show Gist options
  • Save tgr/0d6b81f0089bdbc171498f28666cf6db to your computer and use it in GitHub Desktop.
Save tgr/0d6b81f0089bdbc171498f28666cf6db to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json
import re
from datetime import datetime
URL = 'https://trends.builtwith.com/cms/MediaWiki'
r = requests.get(URL)
data_json = re.search( r'var data = (.*?);', r.text).group(1)
data = json.loads(data_json)
rowCount = len(data['labels'])
# labels are messed up; make our own
firstMonth = datetime.strptime(data['labels'][0], '%Y/%m')
lastMonth = datetime.strptime(data['labels'][-1], '%Y/%m')
def getDate(i):
firstTs = int(firstMonth.strftime('%s'))
lastTs = int(lastMonth.strftime('%s'))
ts = firstTs + (lastTs - firstTs) / (rowCount - 1) * i
return datetime.fromtimestamp(ts)
for group in data['series']:
print('\n== %s ==' % group['name'])
for i, count in enumerate(group['data']):
print(getDate(i).strftime('%Y-%m'), count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment