You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
5.3 KiB
128 lines
5.3 KiB
9 years ago
|
#!/usr/bin/env python
|
||
|
from __future__ import print_function
|
||
|
from argparse import ArgumentParser
|
||
|
import sys, json, re, os
|
||
|
from datetime import datetime
|
||
|
from urllib import urlencode
|
||
9 years ago
|
from urllib2 import HTTPError
|
||
9 years ago
|
from common import *
|
||
|
|
||
|
"""
|
||
9 years ago
|
pull(meta):
|
||
9 years ago
|
Update meta data files for those that have changed.
|
||
|
Check for changed pads by looking at revisions & comparing to existing
|
||
|
|
||
|
"""
|
||
|
|
||
|
def main (args):
|
||
9 years ago
|
p = ArgumentParser("Check for pads that have changed since last sync (according to .meta.json)")
|
||
9 years ago
|
p.add_argument("padid", nargs="*", default=[])
|
||
9 years ago
|
p.add_argument("--padinfo", default=".etherdump/settings.json", help="settings, default: .etherdump/settings.json")
|
||
9 years ago
|
p.add_argument("--zerorevs", default=False, action="store_true", help="include pads with zero revisions, default: False (i.e. pads with no revisions are skipped)")
|
||
9 years ago
|
p.add_argument("--pub", default=".", help="folder to store files for public pads, default: pub")
|
||
9 years ago
|
p.add_argument("--group", default="g", help="folder to store files for group pads, default: g")
|
||
9 years ago
|
p.add_argument("--skip", default=None, type=int, help="skip this many items, default: None")
|
||
9 years ago
|
p.add_argument("--meta", default=False, action="store_true", help="download meta to PADID.meta.json, default: False")
|
||
|
p.add_argument("--text", default=False, action="store_true", help="download text to PADID.txt, default: False")
|
||
|
p.add_argument("--html", default=False, action="store_true", help="download html to PADID.html, default: False")
|
||
|
p.add_argument("--dhtml", default=False, action="store_true", help="download dhtml to PADID.dhtml, default: False")
|
||
9 years ago
|
p.add_argument("--all", default=False, action="store_true", help="download all files (meta, text, html, dhtml), default: False")
|
||
9 years ago
|
args = p.parse_args(args)
|
||
|
|
||
9 years ago
|
info = loadpadinfo(args.padinfo)
|
||
9 years ago
|
data = {}
|
||
|
data['apikey'] = info['apikey']
|
||
9 years ago
|
|
||
|
if args.padid:
|
||
|
padids = args.padid
|
||
|
else:
|
||
9 years ago
|
padids = getjson(info['apiurl']+'listAllPads?'+urlencode(data))['data']['padIDs']
|
||
9 years ago
|
padids.sort()
|
||
|
numpads = len(padids)
|
||
9 years ago
|
# maxmsglen = 0
|
||
9 years ago
|
count = 0
|
||
|
for i, padid in enumerate(padids):
|
||
|
if args.skip != None and i<args.skip:
|
||
|
continue
|
||
9 years ago
|
progressbar(i, numpads, padid)
|
||
9 years ago
|
|
||
9 years ago
|
data['padID'] = padid.encode("utf-8")
|
||
|
p = padpath(padid, args.pub, args.group)
|
||
|
metapath = p + ".meta.json"
|
||
|
revisions = None
|
||
9 years ago
|
tries = 1
|
||
|
skip = False
|
||
|
while True:
|
||
|
try:
|
||
|
if os.path.exists(metapath):
|
||
|
with open(metapath) as f:
|
||
|
meta = json.load(f)
|
||
9 years ago
|
revisions = getjson(info['apiurl']+'getRevisionsCount?'+urlencode(data))['data']['revisions']
|
||
9 years ago
|
if meta['revisions'] == revisions:
|
||
|
skip=True
|
||
|
break
|
||
|
|
||
|
meta = {'padid': padid.encode("utf-8")}
|
||
|
if revisions == None:
|
||
9 years ago
|
meta['revisions'] = getjson(info['apiurl']+'getRevisionsCount?'+urlencode(data))['data']['revisions']
|
||
9 years ago
|
else:
|
||
|
meta['revisions' ] = revisions
|
||
|
|
||
|
if (meta['revisions'] == 0) and (not args.zerorevs):
|
||
|
# print("Skipping zero revs", file=sys.stderr)
|
||
|
skip=True
|
||
|
break
|
||
|
|
||
|
# todo: load more metadata!
|
||
|
meta['pad'], meta['group'] = splitpadname(padid)
|
||
|
meta['pathbase'] = p
|
||
9 years ago
|
meta['lastedited_raw'] = int(getjson(info['apiurl']+'getLastEdited?'+urlencode(data))['data']['lastEdited'])
|
||
9 years ago
|
meta['lastedited_iso'] = datetime.fromtimestamp(int(meta['lastedited_raw'])/1000).isoformat()
|
||
9 years ago
|
meta['author_ids'] = getjson(info['apiurl']+'listAuthorsOfPad?'+urlencode(data))['data']['authorIDs']
|
||
9 years ago
|
break
|
||
|
except HTTPError as e:
|
||
|
tries += 1
|
||
|
if tries > 3:
|
||
|
print ("Too many failures ({0}), skipping".format(padid).encode("utf-8"), file=sys.stderr)
|
||
|
skip=True
|
||
|
break
|
||
|
|
||
|
if skip:
|
||
9 years ago
|
continue
|
||
|
|
||
|
count += 1
|
||
9 years ago
|
|
||
|
print (padid.encode("utf-8"))
|
||
|
|
||
|
if args.all or (args.meta or args.text or args.html or args.dhtml):
|
||
|
try:
|
||
|
os.makedirs(os.path.split(metapath)[0])
|
||
|
except OSError:
|
||
|
pass
|
||
|
|
||
|
if args.all or args.meta:
|
||
|
with open(metapath, "w") as f:
|
||
|
json.dump(meta, f)
|
||
|
|
||
|
# Process text, html, dhtml, all options
|
||
|
if args.all or args.text:
|
||
9 years ago
|
text = getjson(info['apiurl']+'getText?'+urlencode(data))
|
||
9 years ago
|
text = text['data']['text']
|
||
|
with open(p+".txt", "w") as f:
|
||
|
f.write(text.encode("utf-8"))
|
||
|
|
||
|
if args.all or args.html:
|
||
9 years ago
|
html = getjson(info['apiurl']+'getHTML?'+urlencode(data))
|
||
9 years ago
|
html = html['data']['html']
|
||
|
with open(p+".html", "w") as f:
|
||
|
f.write(html.encode("utf-8"))
|
||
|
|
||
|
if args.all or args.dhtml:
|
||
|
data['startRev'] = "0"
|
||
9 years ago
|
html = getjson(info['apiurl']+'createDiffHTML?'+urlencode(data))
|
||
9 years ago
|
html = html['data']['html']
|
||
|
with open(p+".diff.html", "w") as f:
|
||
|
f.write(html.encode("utf-8"))
|
||
|
|
||
|
print("\n{0} pad(s) changed".format(count), file=sys.stderr)
|