Created
November 26, 2013 12:25
-
-
Save radekstepan/7657515 to your computer and use it in GitHub Desktop.
A backup script that uses the pbworks API to download all pages and files in a given wiki workspace.
This file contains 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/python | |
# -*- coding: utf -*- | |
import urllib2, json, datetime, os, unicodedata, re, codecs | |
from threading import Thread | |
API_KEY = '#' | |
WIKI = '#' | |
def slugify(value): | |
if isinstance(value, unicode): | |
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') | |
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower()) | |
return re.sub('[-\s]+', '-', value) | |
class Page(Thread): | |
def __init__ (self, page, dir): | |
Thread.__init__(self) | |
self.file = '%s/%s.html' % (dir, slugify(page['name'])) | |
self.oid = page['oid'] | |
def run(self): | |
html = json.loads(urllib2.urlopen("https://%s.pbworks.com/api_v2/op/GetPage/oid/%s/read_key/%s" | |
% (WIKI, self.oid, API_KEY)).read().split('\n')[1])['html'] | |
with codecs.open(self.file, 'w', 'utf-8') as f: | |
f.write(html) | |
if __name__ == '__main__': | |
dir = WIKI + '-' + datetime.datetime.now().strftime('%Y-%m-%d-%H%M') | |
os.makedirs(dir) | |
for page in json.loads(urllib2.urlopen("https://%s.pbworks.com/api_v2/op/GetPages/read_key/%s" | |
% (WIKI, API_KEY)).read().split('\n')[1])['pages']: | |
t = Page(page, dir) | |
t.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment