Created
December 22, 2018 22:28
-
-
Save grischard/d3e947fa8e3ccac1a5c327f920a418b5 to your computer and use it in GitHub Desktop.
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 python | |
import argparse | |
# results = [] | |
from requests_xml import XMLSession | |
import re | |
import datetime | |
from collections import Counter | |
session = XMLSession() | |
def main(cohort): | |
now = datetime.datetime.utcnow().replace(microsecond=0).isoformat() | |
hotr = re.compile("#hotosm-project-\d+") | |
since="2018-06-11T00:00:00" | |
allhot = Counter() | |
for name in cohort: | |
name = name.rstrip() | |
all_comments = getcomments(name, since, now) | |
hot_changesets = list(filter(hotr.match, all_comments)) | |
hot_activities = Counter(map(lambda x: hotr.findall(x)[0][16:], hot_changesets)) | |
if len(hot_activities) == 0: | |
hot_activities = "none" | |
else: | |
allhot.update(hot_activities) | |
results = {'name': name, 'since': since, 'changesets': len(all_comments), 'hot': len(hot_changesets), 'hot_activities': hot_activities} | |
print(results) | |
print(sorted(allhot.items())) | |
def getcomments(name, since, now): | |
r = session.get('https://api.openstreetmap.org/api/0.6/changesets?display_name='+name+'&time='+since+','+now+'"') | |
comments = [item[0] for item in r.xml.search('<tag k="comment" v="{}"/>')] | |
if len(comments) == 100: #recurse for more | |
oldest = r.xml.xpath('(//osm/changeset)[last()]', first=True) | |
now = oldest.attrs['created_at'] | |
comments.extend(getcomments(name, since, now)) | |
return(comments) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('cohort', type=argparse.FileType('r')) | |
args = parser.parse_args() | |
main(args.cohort.readlines()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment