At the most basic, the library can detect reverts in a stream of "revision" events. This is useful for finding out what reverts have taken place in a sequence of edits to a page. In this case, we'll look at reverts in https://en.wikipedia.org/wiki/Wikipedia:Teahouse using the detect
method. This pattern is generally useful if you'd like to look for reverting activity for an entire page.
In [1]:
import mwapi, mwreverts
from itertools import islice # For limiting the result set
In [2]:
# Gather a page's revisions from the API
session = mwapi.Session("https://en.wikipedia.org", user_agent="mwreverts basic usage script")
response_docs = session.get(action="query",
prop="revisions",
titles={"Wikipedia:Teahouse"},
rvprop={'ids', 'sha1', 'timestamp'},
rvdir="newer", # Direction "newer" is important for detection
continuation=True)
# Creates a revsion event iterator of (checksum, rev_doc) pairs
rev_events = ((rev_doc['sha1'], rev_doc)
for r_doc in response_docs
for page_doc in r_doc['query']['pages'].values()
for rev_doc in page_doc['revisions']
if 'sha1' in rev_doc)
# Detect and print reverts
for revert in islice(mwreverts.detect(rev_events), 5):
print("{0} reverts {1} back to {2}"
.format(revert.reverting['revid'],
[rev_doc['revid'] for rev_doc in revert.reverteds],
revert.reverted_to['revid']))
An edit can reverting other edits, it can be reverted, or it can be reverted_to by another edit. The mwreverts.api.check
method will use a mwapi.Session
to check on the status of an edit by its "revid"
. This pattern is generally useful if you'd like to check on the status of a set of edits that span multiple pages. For example, if you were looking at the activities of a particular user.
In [3]:
import mwapi, mwreverts.api
In [4]:
# Gather a page's revisions from the API
session = mwapi.Session("https://en.wikipedia.org", user_agent="mwreverts basic usage script")
def print_revert_status(rev_id, reverting, reverted, reverted_to):
"""Prints a nice, pretty version of a revert status."""
print(str(rev_id) + ":")
if reverting is not None:
print(" - reverting {0} other edits".format(len(reverting.reverteds)))
if reverted is not None:
print(" - reverted in {revid} by {user}".format(**reverted.reverting))
if reverted_to is not None:
print(" - reverted_to in {revid} by {user}".format(**reverted_to.reverting))
# Checking the status of 3 interesting edits seen above
reverting, reverted, reverted_to = mwreverts.api.check(session, 477059772, rvprop={'user'})
print_revert_status(477059772, reverting, reverted, reverted_to)
reverting, reverted, reverted_to = mwreverts.api.check(session, 477059985, rvprop={'user'})
print_revert_status(477059985, reverting, reverted, reverted_to)
reverting, reverted, reverted_to = mwreverts.api.check(session, 477059735, rvprop={'user'})
print_revert_status(477059735, reverting, reverted, reverted_to)
In [ ]: