Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import xmlrpclib
- from flask import Flask, g, render_template, request, flash
- app = Flask('wiki_copy')
- ADMIN_URL = 'secret'
- PROD_URL = 'secret'
- ADMIN_SITE = 'secret'
- PROD_SITE = 'secret'
- SOURCE_PAGE = 'production-wiki'
- SEPERATOR = '{{=====BEGIN MAPPING HERE=====}}'
- def get_proxies():
- admin = xmlrpclib.ServerProxy(ADMIN_URL)
- admin.users.get_me() # test to see if conn works
- prod = xmlrpclib.ServerProxy(PROD_URL)
- prod.users.get_me()
- return admin, prod
- def get_lines(admin):
- try:
- data = admin.pages.get_one({'site': ADMIN_SITE, 'page': SOURCE_PAGE})['content']
- except:
- flash('Error: unable to fetch the production-wiki page with mappings')
- return
- data = data.splitlines()
- try:
- idx = data.index(SEPERATOR)
- data = data[idx+1:]
- except:
- flash('Error: malformed page mapping')
- return
- return data
- def update_pictures():
- try:
- admin, prod = get_proxies()
- except:
- flash('Error: Unable to initialize XMLRPC connections')
- return
- data = get_lines(admin)
- if not data:
- return
- for idx, line in enumerate(data):
- if line.count('|') != 3:
- continue
- admin_page_url, admin_file_url, prod_page_url = line.split('|')
- if not admin_page_url:
- flash('Line {0}: Error: no admin page url'.format(idx))
- return
- if not admin_file_url:
- flash('Line {0}: Error: no admin file url'.format(idx))
- return
- if not prod_page_url:
- flash('Line {0}: Error: no prod page url'.format(idx))
- return
- try:
- file_ = admin.files.get_one(ADMIN_SITE, admin_page_url, admin_file_url)
- except:
- flash('Line {0]: Error: unable to fetch file {1}/{2}'.format(idx, admin_page_url, admin_file_url))
- return
- kwargs = {'site': PROD_SITE,
- 'page': prod_page_url,
- 'file': admin_file_url,
- 'content': file_['content'],
- 'revision_comment': 'Updated by wiki_copy script'}
- if file_['comment']:
- kwargs['comment'] = admin_page_content['comment']
- try:
- prod.files.save_one(kwargs)
- except:
- flash('Line {0}: Error: unable to save page {1}/{2}->{3}/{2}'.format(idx, admin_page_url, admin_file_url, prod_page_url))
- return
- flash('Line {0}: Successful copy: {1}/{2}->{3}/{2}'.format(idx, admin_page_url, admin_file_url, prod_page_url))
- def update_wiki():
- try:
- admin, prod = get_proxies()
- except:
- flash('Error: Unable to initialize XMLRPC connections')
- return
- data = get_lines(admin)
- if not data:
- return
- for idx, line in enumerate(data):
- if line.count('|') != 1:
- continue
- admin_page_url, prod_page_url = line.split('|')
- if not admin_page_url:
- flash('Line {0}: Error: no admin page url'.format(idx))
- return
- if not prod_page_url:
- flash('Line {0}: Error: no prod page url'.format(idx))
- return
- try:
- admin_page_content = admin.pages.get_one({'site': ADMIN_SITE, 'page': admin_page_url})
- except:
- flash('Line {0]: Error: unable to fetch page {1}'.format(idx, admin_page_url))
- return
- kwargs = {'site': PROD_SITE,
- 'page': prod_page_url,
- 'revision_comment': 'Updated by wiki_copy script'}
- title = admin_page_content['title']
- if not title:
- title = prod_page_url
- kwargs['title'] = title
- content = admin_page_content['content']
- if not content:
- flash('Line {0}: Error: page {1} has no content'.format(idx, admin_page_url))
- return
- kwargs['content'] = content
- if admin_page_content['tags']:
- kwargs['tags'] = admin_page_content['tags']
- try:
- prod.pages.save_one(kwargs)
- except:
- flash('Line {0}: Error: unable to save page {1}->{2}'.format(idx, admin_page_url, prod_page_url))
- return
- flash('Line {0}: Successful copy: {1}->{2}'.format(idx, admin_page_url, prod_page_url))
- return
- @app.route('/images', methods=('GET', 'POST'))
- def do_images():
- update_pictures()
- return render_template('index.html')
- @app.route('/text', methods=('GET', 'POST'))
- def do_text():
- update_wiki()
- return render_template('index.html')
- @app.route('/', methods=('GET', 'POST'))
- def front_page():
- return render_template('index.html')
- if __name__ == "__main__":
- app.debug = True
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement