示例#1
0
def do_upl(imgname):
	imgname = imgname.replace('_',' ')
	imgname = re.sub('^([Ff]ile|[Ii]mage):','',imgname)
	if '{{!}}' in imgname or '|' in imgname:
		return False
	
	#imgname = re.sub('({{!}}|\|).+$','',imgname)#fixme: paplašinājums
	imgname = re.sub('(<!--.*?-->)','',imgname)
	image = "File:"+imgname
	page = pywikibot.Page(ensite,image)
	#pywikibot.output(page)
	imagePage = pywikibot.FilePage(ensite,image)
	finalTitle = imagePage.title(with_ns=False)
	if imagePage.isRedirectPage():
		finalTitle = imagePage.getRedirectTarget().title(with_ns=False)

	pywikibot.output(imagePage)
	
	desc = "== Avots ==\n[[:en:File:{name}|{name}]]\n\n== Licence ==\n{{{{Filmas plakāts}}}}".format(name=finalTitle)
	
	#url = 'https://upload.wikimedia.org/wikipedia/en/b/b4/Project_X_Poster.jpg'
	
	bot = upload.UploadRobot(url=imagePage.fileUrl(), description=desc,
								 keepFilename=True,
                                 verifyDescription=False, ignoreWarning=True,
                                 targetSite=lvsite)
	bot.run()
	
	return finalTitle
示例#2
0
def main(give_url, image_url, desc):
    url = give_url
    image_url = ''
    if url == '':
        if image_url:
            url = pywikibot.input(u"What URL range should I check "
                                  u"(use $ for the part that is changeable)")
        else:
            url = pywikibot.input(u"From what URL should I get the images?")

    if image_url:
        minimum = 1
        maximum = 99
        answer = pywikibot.input(
            u"What is the first number to check (default: 1)")
        if answer:
            minimum = int(answer)
        answer = pywikibot.input(
            u"What is the last number to check (default: 99)")
        if answer:
            maximum = int(answer)

    if not desc:
        basicdesc = pywikibot.input(
            u"What text should be added at the end of "
            u"the description of each image from this url?")
    else:
        basicdesc = desc

    if image_url:
        ilinks = []
        i = minimum
        while i <= maximum:
            ilinks += [url.replace("$", str(i))]
            i += 1
    else:
        ilinks = get_imagelinks(url)

    for image in ilinks:
        if pywikibot.input_yn(u'Include image %s?' % image,
                              default=False,
                              automatic_quit=False):
            desc = pywikibot.input(u"Give the description of this image:")
            categories = []
            while True:
                cat = pywikibot.input(u"Specify a category (or press enter to "
                                      u"end adding categories)")
                if not cat.strip():
                    break
                if ":" in cat:
                    categories.append(u"[[%s]]" % cat)
                else:
                    categories.append(u"[[%s:%s]]" %
                                      (mysite.namespace(14), cat))
            desc += "\r\n\r\n" + basicdesc + "\r\n\r\n" + \
                    "\r\n".join(categories)
            uploadBot = upload.UploadRobot(image, description=desc)
            uploadBot.run()
        elif answer == 's':
            break
示例#3
0
 def test_png(self):
     """Test uploading a png using upload.py."""
     bot = upload.UploadRobot(url=[join_images_path("MP_sounds.png")],
                              description="pywikibot upload.py script test",
                              useFilename=None, keepFilename=True,
                              verifyDescription=True, aborts=set(),
                              ignoreWarning=True, targetSite=self.get_site())
     bot.run()
示例#4
0
 def test_png_url(self):
     """Test uploading a png from url using upload.py."""
     bot = upload.UploadRobot(url=['https://upload.wikimedia.org/wikipedia/commons/f/fc/MP_sounds.png'],
                              description="pywikibot upload.py script test",
                              useFilename=None, keepFilename=True,
                              verifyDescription=True, aborts=set(),
                              ignoreWarning=True, targetSite=self.get_site())
     bot.run()
示例#5
0
def processPhoto(photoInfo=None,
                 panoramioreview=False,
                 reviewer=u'',
                 override=u'',
                 addCategory=u'',
                 autonomous=False,
                 site=None):
    """Process a single Panoramio photo."""
    if not site:
        site = pywikibot.Site('commons', 'commons')

    if isAllowedLicense(photoInfo) or override:
        # Should download the photo only once
        photo = downloadPhoto(photoInfo.get(u'photo_file_url'))

        # Don't upload duplicate images, should add override option
        duplicates = findDuplicateImages(photo, site=site)
        if duplicates:
            pywikibot.output(u'Found duplicate image at %s' % duplicates.pop())
        else:
            filename = getFilename(photoInfo, site=site)
            pywikibot.output(filename)
            description = getDescription(photoInfo, panoramioreview, reviewer,
                                         override, addCategory)

            pywikibot.output(description)
            if not autonomous:
                (newDescription, newFilename,
                 skip) = Tkdialog(description, photo, filename).show_dialog()
            else:
                newDescription = description
                newFilename = filename
                skip = False


#         pywikibot.output(newPhotoDescription)
#         if (pywikibot.Page(title=u'File:'+ filename,
#                            site=pywikibot.Site()).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,
                                         useFilename=newFilename,
                                         keepFilename=True,
                                         verifyDescription=False,
                                         site=site)
                bot.upload_image(debug=False)
                return 1
    return 0
def processPhoto(flickr=None,
                 photo_id=u'',
                 flickrreview=False,
                 reviewer=u'',
                 override=u'',
                 addCategory=u'',
                 removeCategories=False,
                 autonomous=False):
    """Process a single Flickr photo."""
    if photo_id:
        pywikibot.output(str(photo_id))
        (photoInfo, photoSizes) = getPhoto(flickr, photo_id)
    if isAllowedLicense(photoInfo) or override:
        # Get the url of the largest photo
        photoUrl = getPhotoUrl(photoSizes)
        # Should download the photo only once
        photo = downloadPhoto(photoUrl)

        # Don't upload duplicate images, should add override option
        duplicates = findDuplicateImages(photo)
        if duplicates:
            pywikibot.output(u'Found duplicate image at %s' % duplicates.pop())
        else:
            filename = getFilename(photoInfo)
            flinfoDescription = getFlinfoDescription(photo_id)
            photoDescription = buildDescription(flinfoDescription,
                                                flickrreview, reviewer,
                                                override, addCategory,
                                                removeCategories)
            # pywikibot.output(photoDescription)
            if not autonomous:
                (newPhotoDescription, newFilename,
                 skip) = Tkdialog(photoDescription, photo, filename).run()
            else:
                newPhotoDescription = photoDescription
                newFilename = filename
                skip = False
        # pywikibot.output(newPhotoDescription)
        # if (pywikibot.Page(title=u'File:'+ filename, site=pywikibot.Site()).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(photoUrl,
                                         description=newPhotoDescription,
                                         useFilename=newFilename,
                                         keepFilename=True,
                                         verifyDescription=False)
                bot.upload_image(debug=False)
                return 1
    else:
        pywikibot.output(u'Invalid license')
    return 0
示例#7
0
 def test_png_list(self):
     """Test uploading a list of pngs using upload.py."""
     image_list = []
     for directory_info in os.walk(join_images_path()):
         for dir_file in directory_info[2]:
             image_list.append(os.path.join(directory_info[0], dir_file))
     bot = upload.UploadRobot(url=image_list,
                              description="pywikibot upload.py script test",
                              useFilename=None, keepFilename=True,
                              verifyDescription=True, aborts=set(),
                              ignoreWarning=True, targetSite=self.get_site())
     bot.run()
    def processImage(self, fields):
        """Work on a single image."""
        cid = self.buildNewImageDescription(fields)
        pywikibot.output(cid)
        bot = upload.UploadRobot(url=fields.get('imagepage').fileUrl(),
                                 description=cid, useFilename=fields.get('filename'),
                                 keepFilename=True, verifyDescription=False,
                                 ignoreWarning=True,
                                 targetSite=pywikibot.Site('commons', 'commons'))
        bot.run()

        self.tagNowcommons(fields.get('imagepage'), fields.get('filename'))
        self.replaceUsage(fields.get('imagepage'), fields.get('filename'))
示例#9
0
    def _doUpload(self, photo):
        duplicates = photo.findDuplicateImages(self.site)
        if duplicates:
            pywikibot.output(u"Skipping duplicate of %r" % duplicates)
            return duplicates[0]

        title = photo.getTitle(self.titlefmt)
        description = photo.getDescription(self.pagefmt)

        bot = upload.UploadRobot(url=photo.URL,
                                 description=description,
                                 useFilename=title,
                                 keepFilename=True,
                                 verifyDescription=False,
                                 targetSite=self.site)
        bot._contents = photo.downloadPhoto().getvalue()
        bot._retrieved = True
        bot.run()

        return title
示例#10
0
    def transferImage(self, sourceImagePage):
        """
        Download image and its description, and upload it to another site.

        @return: the filename which was used to upload the image
        """
        sourceSite = sourceImagePage.site
        url = sourceImagePage.fileUrl().encode('utf-8')
        pywikibot.output(u"URL should be: %s" % url)
        # localize the text that should be printed on the image description page
        try:
            description = sourceImagePage.get()
            # try to translate license templates
            if (sourceSite.sitename,
                    self.targetSite.sitename) in licenseTemplates:
                for old, new in licenseTemplates[(
                        sourceSite.sitename,
                        self.targetSite.sitename)].items():
                    new = '{{%s}}' % new
                    old = re.compile('{{%s}}' % old)
                    description = textlib.replaceExcept(
                        description, old, new,
                        ['comment', 'math', 'nowiki', 'pre'])

            description = i18n.twtranslate(
                self.targetSite, 'imagetransfer-file_page_message',
                dict(site=sourceSite, description=description))
            description += '\n\n'
            description += sourceImagePage.getFileVersionHistoryTable()
            # add interwiki link
            if sourceSite.family == self.targetSite.family:
                description += u'\r\n\r\n{0}'.format(sourceImagePage)
        except pywikibot.NoPage:
            description = ''
            print("Image does not exist or description page is empty.")
        except pywikibot.IsRedirectPage:
            description = ''
            print("Image description page is redirect.")
        else:
            bot = upload.UploadRobot(url=url,
                                     description=description,
                                     targetSite=self.targetSite,
                                     urlEncoding=sourceSite.encoding(),
                                     keepFilename=self.keep_name,
                                     verifyDescription=not self.keep_name,
                                     ignoreWarning=self.ignore_warning)
            # try to upload
            targetFilename = bot.run()
            if targetFilename and self.targetSite.family.name == 'commons' and \
               self.targetSite.code == 'commons':
                # upload to Commons was successful
                reason = i18n.twtranslate(sourceSite,
                                          'imagetransfer-nowcommons_notice')
                # try to delete the original image if we have a sysop account
                if sourceSite.family.name in config.sysopnames and \
                   sourceSite.lang in config.sysopnames[sourceSite.family.name]:
                    if sourceImagePage.delete(reason):
                        return
                if sourceSite.lang in nowCommonsTemplate and \
                   sourceSite.family.name in config.usernames and \
                   sourceSite.lang in config.usernames[sourceSite.family.name]:
                    # add the nowCommons template.
                    pywikibot.output(u'Adding nowCommons template to %s' %
                                     sourceImagePage.title())
                    sourceImagePage.put(
                        sourceImagePage.get() + '\n\n' +
                        nowCommonsTemplate[sourceSite.lang] % targetFilename,
                        summary=reason)
示例#11
0
    def run(self):
        tosend = {
            'language': self.imagePage.site.language().encode('utf-8'),
            'image': self.imagePage.title(withNamespace=False).encode('utf-8'),
            'newname': self.newname.encode('utf-8'),
            'project': self.imagePage.site.family.name.encode('utf-8'),
            'username': '',
            'commonsense': '1',
            'remove_categories': '1',
            'ignorewarnings': '1',
            'doit': 'Uitvoeren'
        }

        tosend = urlencode(tosend)
        pywikibot.output(tosend)
        CH = pageTextPost('http://tools.wmflabs.org/commonshelper/index.php',
                          tosend)
        pywikibot.output('Got CH desc.')

        tablock = CH.split('<textarea ')[1].split('>')[0]
        CH = CH.split('<textarea ' + tablock + '>')[1].split('</textarea>')[0]
        CH = CH.replace(u'&times;', u'×')
        CH = self.fixAuthor(CH)
        pywikibot.output(CH)

        # I want every picture to be tagged with the bottemplate so i can check my contributions later.
        CH = u'\n\n{{BotMoveToCommons|' + self.imagePage.site.language() + \
             '.' + self.imagePage.site.family.name + \
             '|year={{subst:CURRENTYEAR}}|month={{subst:CURRENTMONTHNAME}}|day={{subst:CURRENTDAY}}}}' + \
             CH

        if self.category:
            CH = CH.replace(
                u'{{subst:Unc}} <!-- Remove this line once you have added categories -->',
                u'')
            CH += u'[[Category:' + self.category + u']]'

        bot = upload.UploadRobot(url=self.imagePage.fileUrl(),
                                 description=CH,
                                 useFilename=self.newname,
                                 keepFilename=True,
                                 verifyDescription=False,
                                 ignoreWarning=True,
                                 targetSite=pywikibot.Site(
                                     'commons', 'commons'))
        bot.run()

        # Should check if the image actually was uploaded
        if pywikibot.Page(pywikibot.Site('commons', 'commons'),
                          u'Image:' + self.newname).exists():
            # Get a fresh copy, force to get the page so we dont run into edit
            # conflicts
            imtxt = self.imagePage.get(force=True)

            # Remove the move to commons templates
            if self.imagePage.site.language() in moveToCommonsTemplate:
                for moveTemplate in moveToCommonsTemplate[
                        self.imagePage.site.language()]:
                    imtxt = re.sub(u'(?i)\{\{' + moveTemplate + u'[^\}]*\}\}',
                                   u'', imtxt)

            # add {{NowCommons}}
            if self.imagePage.site.language() in nowCommonsTemplate:
                addTemplate = nowCommonsTemplate[
                    self.imagePage.site.language()] % self.newname
            else:
                addTemplate = nowCommonsTemplate['_default'] % self.newname

            commentText = i18n.twtranslate(
                self.imagePage.site, 'commons-file-now-available', {
                    'localfile': self.imagePage.title(withNamespace=False),
                    'commonsfile': self.newname
                })

            pywikibot.showDiff(self.imagePage.get(), imtxt + addTemplate)
            self.imagePage.put(imtxt + addTemplate, comment=commentText)

            self.gen = pagegenerators.FileLinksGenerator(self.imagePage)
            self.preloadingGen = pagegenerators.PreloadingGenerator(self.gen)

            # If the image is uploaded under a different name, replace all instances
            if self.imagePage.title(withNamespace=False) != self.newname:
                moveSummary = i18n.twtranslate(
                    self.imagePage.site, 'commons-file-moved', {
                        'localfile': self.imagePage.title(withNamespace=False),
                        'commonsfile': self.newname
                    })

                imagebot = image.ImageRobot(
                    generator=self.preloadingGen,
                    oldImage=self.imagePage.title(withNamespace=False),
                    newImage=self.newname,
                    summary=moveSummary,
                    always=True,
                    loose=True)
                imagebot.run()
        return