Xqt has submitted this change and it was merged.
Change subject: remove unused query import, pep8 changes, code improvements ......................................................................
remove unused query import, pep8 changes, code improvements
replaced statements like x = x + 1 by x += 1
Change-Id: I17f981bd991eafa7d6ab24be7776f5788e02398b --- M panoramiopicker.py 1 file changed, 115 insertions(+), 92 deletions(-)
Approvals: Xqt: Looks good to me, approved
diff --git a/panoramiopicker.py b/panoramiopicker.py index e285cf6..b75cada 100644 --- a/panoramiopicker.py +++ b/panoramiopicker.py @@ -6,42 +6,52 @@ ''' # # (C) Multichill, 2010 +# (C) Pywikipedia bot team, 2010-2013 # # Distributed under the terms of the MIT license. # __version__ = '$Id$'
-import sys, urllib, re, StringIO, hashlib, base64, time +import sys +import urllib +import re +import StringIO +import hashlib +import base64 +import time
import wikipedia as pywikibot -import config, query, imagerecat, upload -import externals # check for and install needed +import config +import imagerecat +import upload +import externals # check for and install needed import json from Tkinter import * -from PIL import Image, ImageTk # see: http://www.pythonware.com/products/pil/ +from PIL import Image, ImageTk # see: http://www.pythonware.com/products/pil/ from BeautifulSoup import BeautifulSoup
-def isAllowedLicense(photoInfo = None): + +def isAllowedLicense(photoInfo=None): ''' Check if the image contains the right license
TODO: Maybe add more licenses + ''' allowed = [u'by-sa'] - if photoInfo[u'license'] in allowed: - return True - else: - return False + return photoInfo[u'license'] in allowed
-def downloadPhoto(photoUrl = ''): + +def downloadPhoto(photoUrl=''): ''' Download the photo and store it in a StrinIO.StringIO object.
TODO: Add exception handling
''' - imageFile=urllib.urlopen(photoUrl).read() + imageFile = urllib.urlopen(photoUrl).read() return StringIO.StringIO(imageFile) +
def findDuplicateImages(photo=None, site=pywikibot.getSite(u'commons', u'commons')): @@ -55,11 +65,13 @@ hashObject.update(photo.getvalue()) return site.getFilesFromAnHash(base64.b16encode(hashObject.digest()))
-def getTags(photoInfo = None): + +def getTags(photoInfo=None): ''' Get all the tags on a photo ''' result = []
return result +
def getLicense(photoInfo=None): ''' @@ -67,22 +79,21 @@ Adding it with a beautiful soup hack '''
- photoInfo['license']=u'c' + photoInfo['license'] = u'c' page = urllib.urlopen(photoInfo.get(u'photo_url')) data = page.read() soup = BeautifulSoup(data) - if soup.find("div", {'id' : 'photo-info'}): - pointer = soup.find("div", {'id' : 'photo-info'}) - if pointer.find("div", {'id' : 'photo-details'}): - pointer = pointer.find("div", {'id' : 'photo-details'}) - if pointer.find("ul", {'id' : 'details'}): - pointer = pointer.find("ul", {'id' : 'details'}) - if pointer.find("li", {'class' : 'license by-sa'}): - photoInfo['license']=u'by-sa' + if soup.find("div", {'id': 'photo-info'}): + pointer = soup.find("div", {'id': 'photo-info'}) + if pointer.find("div", {'id': 'photo-details'}): + pointer = pointer.find("div", {'id': 'photo-details'}) + if pointer.find("ul", {'id': 'details'}): + pointer = pointer.find("ul", {'id': 'details'}) + if pointer.find("li", {'class': 'license by-sa'}): + photoInfo['license'] = u'by-sa' # Does Panoramio have more license options?
return photoInfo -
def getFilename(photoInfo=None, site=pywikibot.getSite(u'commons', u'commons'), @@ -94,22 +105,23 @@ username = photoInfo.get(u'owner_name') title = photoInfo.get(u'photo_title') if title: - title = cleanUpTitle(title) + title = cleanUpTitle(title) else: title = u''
if pywikibot.Page(site, u'File:%s - %s - %s.jpg' - % (project, username, title) ).exists(): + % (project, username, title)).exists(): i = 1 while True: if (pywikibot.Page(site, u'File:%s - %s - %s (%s).jpg' % (project, username, title, str(i))).exists()): - i = i + 1 + i += 1 else: return u'%s - %s - %s (%s).jpg' % (project, username, title, str(i)) else: return u'%s - %s - %s.jpg' % (project, username, title) +
def cleanUpTitle(title): ''' Clean up the title of a potential mediawiki page. Otherwise the title of @@ -137,57 +149,56 @@
def getDescription(photoInfo=None, panoramioreview=False, reviewer=u'', - override=u'', addCategory=u''): - ''' - Build description for the image. - ''' + override=u'', addCategory=u''): + ''' Build description for the image. '''
desc = u'' - desc = desc + u'{{Information\n' - desc = desc + u'|description=%(photo_title)s\n' - desc = desc + u'|date=%(upload_date)s (upload date)\n' - desc = desc + u'|source=[%(photo_url)s Panoramio]\n' - desc = desc + u'|author=[%(owner_url)s?with_photo_id=%(photo_id)s %(owner_name)s] \n' - desc = desc + u'|permission=\n' - desc = desc + u'|other_versions=\n' - desc = desc + u'|other_fields=\n' - desc = desc + u'}}\n' + desc += u'{{Information\n' + desc += u'|description=%(photo_title)s\n' + desc += u'|date=%(upload_date)s (upload date)\n' + desc += u'|source=[%(photo_url)s Panoramio]\n' + desc += u'|author=[%(owner_url)s?with_photo_id=%(photo_id)s %(owner_name)s] \n' + desc += u'|permission=\n' + desc += u'|other_versions=\n' + desc += u'|other_fields=\n' + desc += u'}}\n' if photoInfo.get(u'latitude') and photoInfo.get(u'longitude'): - desc = desc + u'{{Location dec|%(latitude)s|%(longitude)s|source:Panoramio}}\n' - desc = desc + u'\n' - desc = desc + u'=={{int:license-header}}==\n' + desc += u'{{Location dec|%(latitude)s|%(longitude)s|source:Panoramio}}\n' + desc += u'\n' + desc += u'=={{int:license-header}}==\n'
if override: - desc = desc + override + desc += override else: - if photoInfo.get(u'license')==u'by-sa': - desc = desc + u'{{Cc-by-sa-3.0}}\n' + if photoInfo.get(u'license') == u'by-sa': + desc += u'{{Cc-by-sa-3.0}}\n' if panoramioreview: - desc = desc + u'{{Panoramioreview|%s|{{subst:CURRENTYEAR}}-{{subst:CURRENTMONTH}}-{{subst:CURRENTDAY2}}}}\n' % (reviewer,) + desc += u'{{Panoramioreview|%s|{{subst:CURRENTYEAR}}-{{subst:CURRENTMONTH}}-{{subst:CURRENTDAY2}}}}\n' % (reviewer,) else: - desc = desc + u'{{Panoramioreview}}\n' + desc += u'{{Panoramioreview}}\n'
- desc = desc + u'\n' + desc += u'\n' cats = u'' if addCategory: - desc = desc + u'\n[[Category:%s]]\n' % (addCategory,) + desc += u'\n[[Category:%s]]\n' % (addCategory,) cats = True
# Get categories based on location if photoInfo.get(u'latitude') and photoInfo.get(u'longitude'): - cats=imagerecat.getOpenStreetMapCats(photoInfo.get(u'latitude'), photoInfo.get(u'longitude')) - cats=imagerecat.applyAllFilters(cats) + cats = imagerecat.getOpenStreetMapCats(photoInfo.get(u'latitude'), + photoInfo.get(u'longitude')) + cats = imagerecat.applyAllFilters(cats) for cat in cats: - desc = desc + u'[[Category:%s]]\n' % (cat,) + desc += u'[[Category:%s]]\n' % (cat,) if not cats: - desc = desc + u'{{subst:Unc}}\n' + desc += u'{{subst:Unc}}\n'
return desc % photoInfo +
def processPhoto(photoInfo=None, panoramioreview=False, reviewer=u'', override=u'', addCategory=u'', autonomous=False): ''' Process a single Panoramio photo ''' -
if isAllowedLicense(photoInfo) or override: #Should download the photo only once @@ -211,14 +222,18 @@ newDescription = description newFilename = filename skip = False - #pywikibot.output(newPhotoDescription) - #if (pywikibot.Page(title=u'File:'+ filename, site=pywikibot.getSite()).exists()): - # I should probably check if the hash is the same and if not upload it under a different name - #pywikibot.output(u'File:' + filename + u' already exists!') - #else: - #Do the actual upload - #Would be nice to check before I upload if the file is already at Commons - #Not that important for this program, but maybe for derived programs +## pywikibot.output(newPhotoDescription) +## if (pywikibot.Page(title=u'File:'+ filename, +## site=pywikibot.getSite()).exists()): +## # I should probably check if the hash is the same and if not upload +## # it under a different name +## pywikibot.output(u'File:' + filename + u' already exists!') +## else: + # Do the actual upload + # Would be nice to check before I upload if the file is already at + # Commons + # Not that important for this program, but maybe for derived + # programs if not skip: bot = upload.UploadRobot(photoInfo.get(u'photo_file_url'), description=newDescription, @@ -233,40 +248,43 @@ class Tkdialog: ''' The user dialog. ''' def __init__(self, photoDescription, photo, filename): - self.root=Tk() + self.root = Tk() #"%dx%d%+d%+d" % (width, height, xoffset, yoffset) - self.root.geometry("%ix%i+10-10"%(config.tkhorsize, config.tkvertsize)) + self.root.geometry("%ix%i+10-10" % (config.tkhorsize, + config.tkvertsize))
self.root.title(filename) self.photoDescription = photoDescription self.filename = filename self.photo = photo - self.skip=False - self.exit=False + self.skip = False + self.exit = False
## Init of the widgets # The image - self.image=self.getImage(self.photo, 800, 600) - self.imagePanel=Label(self.root, image=self.image) + self.image = self.getImage(self.photo, 800, 600) + self.imagePanel = Label(self.root, image=self.image)
self.imagePanel.image = self.image
# The filename - self.filenameLabel=Label(self.root,text=u"Suggested filename") - self.filenameField=Entry(self.root, width=100) + self.filenameLabel = Label(self.root, text=u"Suggested filename") + self.filenameField = Entry(self.root, width=100) self.filenameField.insert(END, filename)
# The description - self.descriptionLabel=Label(self.root,text=u"Suggested description") - self.descriptionScrollbar=Scrollbar(self.root, orient=VERTICAL) - self.descriptionField=Text(self.root) + self.descriptionLabel = Label(self.root, text=u"Suggested description") + self.descriptionScrollbar = Scrollbar(self.root, orient=VERTICAL) + self.descriptionField = Text(self.root) self.descriptionField.insert(END, photoDescription) - self.descriptionField.config(state=NORMAL, height=12, width=100, padx=0, pady=0, wrap=WORD, yscrollcommand=self.descriptionScrollbar.set) + self.descriptionField.config( + state=NORMAL, height=12, width=100, padx=0, pady=0, wrap=WORD, + yscrollcommand=self.descriptionScrollbar.set) self.descriptionScrollbar.config(command=self.descriptionField.yview)
# The buttons - self.okButton=Button(self.root, text="OK", command=self.okFile) - self.skipButton=Button(self.root, text="Skip", command=self.skipFile) + self.okButton = Button(self.root, text="OK", command=self.okFile) + self.skipButton = Button(self.root, text="Skip", command=self.skipFile)
## Start grid
@@ -295,13 +313,13 @@
def okFile(self): ''' The user pressed the OK button. ''' - self.filename=self.filenameField.get() - self.photoDescription=self.descriptionField.get(0.0, END) + self.filename = self.filenameField.get() + self.photoDescription = self.descriptionField.get(0.0, END) self.root.destroy()
def skipFile(self): ''' The user pressed the Skip button. ''' - self.skip=True + self.skip = True self.root.destroy()
def run(self): @@ -315,21 +333,22 @@
def getPhotos(photoset=u'', start_id='', end_id='', interval=100): ''' Loop over a set of Panoramio photos. ''' - i=0 - has_more=True + i = 0 + has_more = True url = u'http://www.panoramio.com/map/get_panoramas.php?set=%s&from=%s&to=%s&...' while has_more: gotInfo = False maxtries = 10 tries = 0 - while(not gotInfo): + while not gotInfo: try: - if ( tries < maxtries ): - tries = tries + 1 - panoramioApiPage = urllib.urlopen(url % (photoset, i, i+interval)) + if tries < maxtries: + tries += 1 + panoramioApiPage = urllib.urlopen(url % (photoset, i, + i + interval)) contents = panoramioApiPage.read().decode('utf-8') gotInfo = True - i = i + interval + i += interval else: break except IOError: @@ -338,31 +357,34 @@ pywikibot.output(u'Got a timeout, let's try again')
metadata = json.loads(contents) - count = metadata.get(u'count') # Useless? + count = metadata.get(u'count') # Useless? photos = metadata.get(u'photos') for photo in photos: yield photo has_more = metadata.get(u'has_more') - return +
def usage(): ''' Print usage information
TODO : Need more. + ''' pywikibot.output( - u"Panoramiopicker is a tool to transfer Panaramio photos to Wikimedia Commons") + u"Panoramiopicker is a tool to transfer Panaramio photos to Wikimedia " + u"Commons") pywikibot.output(u"-set:<set_id>\n") return +
def main(): site = pywikibot.getSite(u'commons', u'commons') pywikibot.setSite(site) - #imagerecat.initLists() +## imagerecat.initLists()
- photoset = u'' #public (popular photos), full (all photos), user ID number + photoset = u'' # public (popular photos), full (all photos), user ID number size = u'original' minx = u'' miny = u'' @@ -376,7 +398,7 @@ uploadedPhotos = 0
# Do we mark the images as reviewed right away? - if config.panoramio ['review']: + if config.panoramio['review']: panoramioreview = config.panoramio['review'] else: panoramioreview = False @@ -415,7 +437,8 @@ elif arg.startswith('-tags'): if len(arg) == 5: tags = pywikibot.input( - u'What is the tag you want to filter out (currently only one supported)?') + u'What is the tag you want to filter out (currently only ' + u'one supported)?') else: tags = arg[6:] elif arg == '-panoramioreview':