Index: trunk/tools/fancy-uploader/fancy-uploader.py |
— | — | @@ -1,13 +1,50 @@ |
2 | 2 | #!/usr/bin/python |
3 | 3 | |
| 4 | +""" |
| 5 | +Copyright 2011 - Bryan Tong Minh |
| 6 | +Licensed under the terms of the MIT license |
| 7 | +""" |
| 8 | + |
| 9 | +""" |
| 10 | +TODO: |
| 11 | + - Dupe detection |
| 12 | + |
| 13 | +""" |
| 14 | +import sys |
| 15 | + |
| 16 | +import mwclient |
| 17 | +try: |
| 18 | + import json |
| 19 | +except ImportError: |
| 20 | + import simplejson as json |
| 21 | + |
4 | 22 | class FancyUploader(object): |
5 | 23 | |
6 | 24 | def __init__(self): |
7 | 25 | pass |
8 | 26 | |
9 | | - def convert_json_to_wikitext(self, json): |
| 27 | + def construct_title(self): |
10 | 28 | pass |
11 | 29 | |
12 | | - def upload(self, data): |
13 | | - pass |
| 30 | + def convert_dict_to_wikitext(self, template, dict): |
| 31 | + # TODO curly brace escaping |
| 32 | + return '{{%s|%s}}' % (template, '|'.join('%s=%s' for i in dict.iteritems())) |
14 | 33 | |
| 34 | + def upload(self, username, password, file, filename, wikitext): |
| 35 | + site = mwclient.Site('commons.wikimedia.org') |
| 36 | + site.login(username, password) |
| 37 | + return site.upload(file, filename, wikitext) |
| 38 | + |
| 39 | + def run(self, data, filename): |
| 40 | + title = self.construct_title() |
| 41 | + wikitext = self.convert_dict_to_wikitext(data['body_template'], |
| 42 | + data['metadata']) |
| 43 | + result = self.upload(data['username'], data['password'], |
| 44 | + open(filename, 'rb'), title, wikitext) |
| 45 | + |
| 46 | +if __name__ == '__main__': |
| 47 | + uploader = FancyUploader() |
| 48 | + data = json.load(open(sys.argv[1], 'rb')) |
| 49 | + result = uploader.run(data, sys.argv[2]) |
| 50 | + json.dump(sys.stdout, result) |
| 51 | + |
Property changes on: trunk/tools/fancy-uploader/fancy-uploader.py |
___________________________________________________________________ |
Added: svn:executable |
15 | 52 | + * |
Property changes on: trunk/tools/fancy-uploader |
___________________________________________________________________ |
Added: svn:externals |
16 | 53 | + mwclient https://mwclient.svn.sourceforge.net/svnroot/mwclient/trunk/mwclient/ |