jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/576633 )
Change subject: [IMPR] Simplify code ......................................................................
[IMPR] Simplify code
isAllowedLicense(): - just return flickr_allowed_license lookup result
getTags(): - use list comprehension instead of building a list
getFilename(): - create FilePage title only once - use FilePage instead of Pag which respects namespace identifier - use exists() condition instead of additional return statement
buildDescription(): - combine "if flickrreview" and "if reviewer"
Change-Id: Ie132b7053b29b9eb65ece0272d574c1dcc63a43b --- M scripts/flickrripper.py 1 file changed, 24 insertions(+), 37 deletions(-)
Approvals: D3r1ck01: Looks good to me, but someone else must approve Huji: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/flickrripper.py b/scripts/flickrripper.py index 7de0d27..9dc6652 100755 --- a/scripts/flickrripper.py +++ b/scripts/flickrripper.py @@ -93,11 +93,8 @@
TODO: Maybe add more licenses """ - license = photoInfo.find('photo').attrib['license'] - if flickr_allowed_license[int(license)]: - return True - else: - return False + photo_license = photoInfo.find('photo').attrib['license'] + return flickr_allowed_license[int(photo_license)]
def getPhotoUrl(photoSizes): @@ -142,16 +139,14 @@
def getTags(photoInfo, raw=False): - """Get all the tags on a photo.""" - result = [] - for tag in photoInfo.find('photo').find('tags').findall('tag'): - if raw: - # use original tag name - # see https://www.flickr.com/services/api/misc.tags.html - result.append(tag.attrib['raw'].lower()) - else: - result.append(tag.text.lower()) - return result + """Get all the tags on a photo. + + @param raw: use original tag name + see https://www.flickr.com/services/api/misc.tags.html + @type raw: bool + """ + return [tag.attrib['raw'].lower() if raw else tag.text.lower() + for tag in photoInfo.find('photo').find('tags').findall('tag')]
def getFlinfoDescription(photo_id): @@ -199,20 +194,14 @@
fileformat = photoInfo.find('photo').attrib['originalformat'] if not fileformat and photo_url: - fileformat = photo_url.split('.')[-1] - - if pywikibot.Page(site, 'File:{} - {} - {}.{}' - .format(title, project, username, fileformat)).exists(): - i = 1 - while True: - name = '{} - {} - {} ({}).{}'.format(title, project, username, + _, fileformat = photo_url.rsplit('.', 1) + filename = '{} - {} - {}.{}'.format(title, project, username, fileformat) + i = 1 + while pywikibot.FilePage(site, filename).exists(): + filename = '{} - {} - {} ({}).{}'.format(title, project, username, i, fileformat) - if pywikibot.Page(site, 'File:' + name).exists(): - i += 1 - else: - return name - else: - return '{} - {} - {}.{}'.format(title, project, username, fileformat) + i += 1 + return filename
def cleanUpTitle(title): @@ -270,13 +259,13 @@ '') description = description.replace('=={{int:license}}==', '=={{int:license}}==\n' + override) - elif flickrreview: - if reviewer: - description = description.replace( - '{{flickrreview}}', - '{{flickrreview|%s|' - '{{subst:CURRENTYEAR}}-{{subst:CURRENTMONTH}}-' - '{{subst:CURRENTDAY2}}}}' % reviewer) + elif flickrreview and reviewer: + description = description.replace( + '{{flickrreview}}', + '{{flickrreview|%s|' + '{{subst:CURRENTYEAR}}-{{subst:CURRENTMONTH}}-' + '{{subst:CURRENTDAY2}}}}' % reviewer) + if '{{subst:unc}}' not in description: # Request category check description += '\n{{subst:chc}}\n' @@ -424,8 +413,6 @@ pywikibot.output('Flickr api problem, sleeping') pywikibot.sleep(30)
- return -
def main(*args): """
pywikibot-commits@lists.wikimedia.org