Skip to content

Instantly share code, notes, and snippets.

@legoktm legoktm/upload_multiple.py
Last active Oct 12, 2015

Embed
What would you like to do?
A quick wrapper around upload.py to allow for mass-uploading.
#!/usr/bin/env python
"""
# (C) Rob W.W. Hooft, Andre Engels 2003-2004
# (C) Pywikipedia bot team, 2003-2011
Copyright (C) 2012 Legoktm
Portions of this script are copied from upload.py, which is also released
under the MIT License, which appears below.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
"""
#Script to mass upload images to a pywikibot.Site()
#CONFIGURATION SETTINGS
filename = '' #TEXT FILE, EACH IMAGE SHOULD BE ITS OWN LINE
defaultDescription = """MESSAGE GOES HERE."""
#END CONFIGURATION SETTINGS
import os
import wikipedia as pywikibot
import upload
if not os.path.isfile(filename):
raise Exception("%s doesn't exist" % filename)
def main():
f = open(filename, 'r')
text = f.read()
f.close()
url = u''
description = []
keepFilename = False
useFilename = None
verifyDescription = True
# process all global bot args
# returns a list of non-global args, i.e. args for upload.py
for arg in pywikibot.handleArgs():
if arg:
if arg.startswith('-keep'):
keepFilename = True
elif arg.startswith('-filename:'):
useFilename = arg[10:]
elif arg.startswith('-noverify'):
verifyDescription = False
elif url == u'':
url = arg
else:
description.append(arg)
description = u' '.join(description)
f = open(filename, 'r')
text = f.read()
f.close()
for line in text.splitlines():
if line.isspace():
continue
print 'Will try to upload: %s' % line
if not os.path.isfile(line):
print "Warning: %s doesn't exist!" % line
bot = upload.UploadRobot(url=line, description=defaultDescription, useFilename=useFilename,
keepFilename=keepFilename,
verifyDescription=verifyDescription)
bot.run()
if __name__ == "__main__":
try:
main()
finally:
pywikibot.stopme()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.