rewrite wiki-upload
parent
2eda5043b5
commit
2bfa3ee3ae
@ -1,37 +1,62 @@
|
|||||||
#!/usr/bin/perl
|
#!/usr/bin/env python
|
||||||
# Upload a file to a recent MediaWiki using the API.
|
# Upload a file to a recent MediaWiki using the API.
|
||||||
|
#
|
||||||
|
# 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
|
||||||
|
#
|
||||||
|
|
||||||
use strict;
|
from __future__ import division, print_function
|
||||||
use IO::File;
|
import argparse
|
||||||
use MediaWiki::API 0.39;
|
import mwclient
|
||||||
|
import os.path
|
||||||
use constant API_URL => 'https://entropia.de/wiki/api.php';
|
import termcolor
|
||||||
|
import yaml
|
||||||
my $wikiuser = $ARGV[0];
|
|
||||||
my $wikipass = $ARGV[1];
|
|
||||||
my $file = $ARGV[2];
|
def login():
|
||||||
my $comment = $ARGV[3];
|
config = yaml.load(open(os.path.expanduser('~/.config/wiki-upload.yaml')))
|
||||||
|
|
||||||
# usage
|
mw_site = ('https', config['site']) if config['https'] else config['site']
|
||||||
if (!defined($comment)) {
|
mw_site_opts = config['site_opts'] if 'site_opts' in config else {}
|
||||||
print "Usage: $0 <wikiuser> <wikipass> <file> <comment>\n";
|
mw_site_opts['clients_useragent'] = 'wiki-upload'
|
||||||
exit;
|
|
||||||
}
|
site = mwclient.Site(mw_site, **mw_site_opts)
|
||||||
|
site.login(config['username'], config['password'])
|
||||||
# log in to the wiki
|
|
||||||
my $mw = MediaWiki::API->new({api_url => API_URL});
|
return site
|
||||||
$mw->login( { lgname => $wikiuser, lgpassword => $wikipass } )
|
|
||||||
|| die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
|
|
||||||
|
def parse_args():
|
||||||
# upload file
|
parser = argparse.ArgumentParser(
|
||||||
my $fh = IO::File->new($file, 'r');
|
description='Upload a file to a recent MediaWiki using the API.')
|
||||||
$fh->binmode();
|
parser.add_argument('images', metavar='image.jpg', nargs='+',
|
||||||
my ($buffer, $data);
|
help='an image to upload')
|
||||||
while ( read($fh, $buffer, 65536) ) {
|
parser.add_argument('-d', '--description', required=True,
|
||||||
$data .= $buffer;
|
help='description of the images')
|
||||||
}
|
args = parser.parse_args()
|
||||||
close($fh);
|
|
||||||
|
return args
|
||||||
$mw->upload( { title => $file,
|
|
||||||
summary => $comment,
|
|
||||||
data => $data } ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
|
def main():
|
||||||
|
site = login()
|
||||||
|
|
||||||
|
args = parse_args()
|
||||||
|
for image in args.images:
|
||||||
|
destination_name = os.path.basename(image)
|
||||||
|
|
||||||
|
print('Uploading {} ... '.format(destination_name), end='')
|
||||||
|
ret = site.upload(open(image), destination_name, args.description)
|
||||||
|
|
||||||
|
color = 'green' if ret['result'] == 'Success' else 'yellow'
|
||||||
|
print(termcolor.colored(ret['result'], color))
|
||||||
|
|
||||||
|
main()
|
||||||
|
Loading…
Reference in New Issue