| 
									
										
										
										
											2015-06-28 00:16:45 +02:00
										 |  |  | #!/usr/bin/env python | 
					
						
							| 
									
										
										
										
											2015-06-27 17:30:17 +02:00
										 |  |  | # Upload a file to a recent MediaWiki using the API. | 
					
						
							| 
									
										
										
										
											2015-06-28 00:16:45 +02:00
										 |  |  | # | 
					
						
							|  |  |  | # The configuration file ~/.config/wiki-upload.yaml must contain Wiki site | 
					
						
							|  |  |  | # information and credentials. For example: | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #    --- | 
					
						
							|  |  |  | #    site: awesomewiki.com | 
					
						
							|  |  |  | #    https: yes | 
					
						
							|  |  |  | #    site_opts: | 
					
						
							|  |  |  | #      path: /wiki/ | 
					
						
							|  |  |  | #    username: UploadBoat | 
					
						
							|  |  |  | #    password: VeRySeCuR3 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2015-06-27 17:30:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-28 00:16:45 +02:00
										 |  |  | from __future__ import division, print_function | 
					
						
							|  |  |  | import argparse | 
					
						
							|  |  |  | import mwclient | 
					
						
							|  |  |  | import os.path | 
					
						
							|  |  |  | import termcolor | 
					
						
							|  |  |  | import yaml | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def login(): | 
					
						
							|  |  |  |     config = yaml.load(open(os.path.expanduser('~/.config/wiki-upload.yaml'))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mw_site = ('https', config['site']) if config['https'] else config['site'] | 
					
						
							|  |  |  |     mw_site_opts = config['site_opts'] if 'site_opts' in config else {} | 
					
						
							|  |  |  |     mw_site_opts['clients_useragent'] = 'wiki-upload' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     site = mwclient.Site(mw_site, **mw_site_opts) | 
					
						
							|  |  |  |     site.login(config['username'], config['password']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return site | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def parse_args(): | 
					
						
							|  |  |  |     parser = argparse.ArgumentParser( | 
					
						
							|  |  |  |         description='Upload a file to a recent MediaWiki using the API.') | 
					
						
							|  |  |  |     parser.add_argument('images', metavar='image.jpg', nargs='+', | 
					
						
							|  |  |  |                         help='an image to upload') | 
					
						
							|  |  |  |     parser.add_argument('-d', '--description', required=True, | 
					
						
							|  |  |  |                         help='description of the images') | 
					
						
							|  |  |  |     args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return args | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     site = login() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     args = parse_args() | 
					
						
							|  |  |  |     for image in args.images: | 
					
						
							|  |  |  |         destination_name = os.path.basename(image) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-28 04:28:03 +02:00
										 |  |  |         print('Uploading {} ... '.format(destination_name)) | 
					
						
							| 
									
										
										
										
											2015-06-28 00:16:45 +02:00
										 |  |  |         ret = site.upload(open(image), destination_name, args.description) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         color = 'green' if ret['result'] == 'Success' else 'yellow' | 
					
						
							| 
									
										
										
										
											2015-06-28 04:28:03 +02:00
										 |  |  |         print(termcolor.colored('  {}'.format(ret['result']), color)) | 
					
						
							|  |  |  |         if 'warnings' in ret: | 
					
						
							|  |  |  |             for warning, what in ret['warnings'].items(): | 
					
						
							|  |  |  |                 print(termcolor.colored('  {}: {}'.format(warning, what), | 
					
						
							|  |  |  |                                         'yellow')) | 
					
						
							| 
									
										
										
										
											2015-06-28 00:16:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | main() |