]> code.communitydata.science - rises_declines_wikia_code.git/blob - mediawiki_dump_tools/Mediawiki-Utilities/examples/lib.sessions.py
add copy of the GPL
[rises_declines_wikia_code.git] / mediawiki_dump_tools / Mediawiki-Utilities / examples / lib.sessions.py
1 """
2 Prints out session information for user "TextAccountForMWUtils"
3 """
4 from mw.api import Session
5 from mw.lib import sessions
6
7 # Gather a user's revisions from the API
8 api_session = Session("https://en.wikipedia.org/w/api.php")
9 revs = api_session.user_contribs.query(
10     user={"TestAccountForMWUtils"},
11     direction="newer"
12 )
13 rev_events = ((rev['user'], rev['timestamp'], rev) for rev in revs)
14
15 # Extract and print sessions
16 for user, session in sessions.cluster(rev_events):
17     print("{0}'s session with {1} revisions".format(user, len(session)))

Community Data Science Collective || Want to submit a patch?