wp2git/wp2git.py

115 lines
4.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python2
from __future__ import print_function
from sys import stderr, stdout, platform
import argparse
import mwclient
import subprocess as sp
2014-10-14 14:24:41 -05:00
import urlparse
import os, locale, time
lang = locale.getdefaultlocale()[0].split('_')[0] or ''
def sanitize(s):
forbidden = r'?*<>|:\/"'
for c in forbidden:
s = s.replace(c, '_')
return s
def parse_args():
p = argparse.ArgumentParser(description='Create a git repository with the history of the specified Wikipedia article.')
p.add_argument('article_name')
2015-12-26 23:58:59 -06:00
g=p.add_mutually_exclusive_group()
g.add_argument('-n', '--no-import', dest='doimport', default=True, action='store_false',
help="Don't invoke git fast-import; only generate fast-import data stream")
2015-12-26 23:58:59 -06:00
g.add_argument('-b', '--bare', action='store_true', help="Import to a bare repository (no working tree)")
p.add_argument('-o', '--out', help='Output directory or fast-import stream file')
g=p.add_mutually_exclusive_group()
g.add_argument('--lang', default=lang, help='Wikipedia language code (default %(default)s)')
2014-10-14 14:24:41 -05:00
g.add_argument('--site', help='Alternate site (e.g. http://commons.wikimedia.org[/w/])')
args = p.parse_args()
if not args.doimport:
if args.out is None:
# http://stackoverflow.com/a/2374507/20789
if platform == "win32":
import os, msvcrt
msvcrt.setmode(stdout.fileno(), os.O_BINARY)
args.out = stdout
else:
try:
args.out = argparse.FileType('wb')(args.out)
except argparse.ArgumentTypeError as e:
p.error(e.args[0])
return p, args
def main():
p, args = parse_args()
# Connect to site with mwclient
if args.site is not None:
2014-10-14 14:24:41 -05:00
scheme, host, path = urlparse.urlparse(args.site, scheme='http')[:3]
if path=='':
path = '/w/'
elif not path.endswith('/'):
path += '/'
elif args.lang is not None:
2014-10-14 14:24:41 -05:00
scheme, host, path = 'http', '%s.wikipedia.org' % args.lang, '/w/'
else:
2014-10-14 14:24:41 -05:00
scheme, host, path = 'http', 'wikipedia.org', '/w/'
site = mwclient.Site((scheme, host), path=path)
print('Connected to %s://%s%s' % (scheme, host, path), file=stderr)
# Find the page
page = site.pages[args.article_name]
if not page.exists:
p.error('Page %s does not exist' % s)
fn = sanitize(args.article_name)
if args.doimport:
# Create output directory and pipe to git
if args.out is not None:
path = args.out
else:
path = fn
if os.path.exists(path):
p.error('path %s exists' % path)
else:
os.mkdir(path)
os.chdir(path)
2015-12-26 23:58:59 -06:00
sp.check_call(['git','init'] + (['--bare'] if args.bare else []))
pipe = sp.Popen(['git', 'fast-import','--quiet','--done'], stdin=sp.PIPE)
fid = pipe.stdin
else:
fid = args.out
# Output fast-import data stream to file or git pipe
with fid:
fid.write('reset refs/heads/master\n')
for rev in page.revisions(dir='newer', prop='ids|timestamp|flags|comment|user|content'):
id = rev['revid']
text = rev.get('*','').encode('utf8')
2014-10-14 14:24:41 -05:00
committer = '%s@%s' % (rev['user'].encode('utf8'), site.host[1])
ts = time.mktime(rev['timestamp'])
2015-01-20 00:42:30 -06:00
print((" >> Revision %d by %s at %s: %s" % (id, rev.get('user',''), time.ctime(ts), rev['comment'])).encode('ascii','xmlcharrefreplace'), file=stderr)
2015-07-23 13:45:26 -05:00
summary = '%s\n\nURL: %s://%s%sindex.php?oldid=%d' % (rev.get('comment','').encode('utf8') or '<blank>',
site.host[0], site.host[1], site.path, id)
fid.write('commit refs/heads/master\n')
fid.write('committer <%s> %d +0000\n' % (committer, ts))
fid.write('data %d\n%s\n' % (len(summary), summary))
fid.write('M 644 inline %s.mw\n' % fn)
fid.write('data %d\n%s\n' % (len(text), text))
fid.write('done\n')
if args.doimport:
pipe.communicate()
2015-12-26 23:58:59 -06:00
if not args.bare:
sp.check_call(['git','checkout'])
if __name__=='__main__':
main()