本文整理汇总了Python中pywikibot.output函数的典型用法代码示例。如果您正苦于以下问题:Python output函数的具体用法?Python output怎么用?Python output使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了output函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getPhotos
def getPhotos(photoset=u'', start_id='', end_id='', interval=100):
"""Loop over a set of Panoramio photos."""
i = 0
has_more = True
url = ('http://www.panoramio.com/map/get_panoramas.php?'
'set=%s&from=%s&to=%s&size=original')
while has_more:
gotInfo = False
maxtries = 10
tries = 0
while not gotInfo:
try:
if tries < maxtries:
tries += 1
panoramioApiPage = urlopen(url % (photoset, i, i + interval))
contents = panoramioApiPage.read().decode('utf-8')
gotInfo = True
i += interval
else:
break
except IOError:
pywikibot.output(u'Got an IOError, let\'s try again')
except socket.timeout:
pywikibot.output(u'Got a timeout, let\'s try again')
metadata = json.loads(contents)
photos = metadata.get(u'photos')
for photo in photos:
yield photo
has_more = metadata.get(u'has_more')
return
示例2: _do_insert
def _do_insert(self, valuesdict):
sqlreq = u"insert into `%(DB)s`.`%(table)s` ("%self.infos
for i in valuesdict:
sqlreq += u"`%s`,"%self.connect.escape_string(i)
sqlreq = sqlreq.strip(',')
sqlreq += u") values ("
for i in valuesdict:
valuesdict[i]=valuesdict[i].replace("'","\\'")
sqlreq += u"'%s',"%valuesdict[i]
sqlreq = sqlreq.strip(',')
sqlreq += u")"
try:
self.cursor.execute(sqlreq)
except UnicodeError:
sqlreq = sqlreq.encode('utf8')
self.cursor.execute(sqlreq)
except Exception as e:
if verbose: wikipedia.output(sqlreq)
raise e
self.querycount+=1
if not self.querycount%1000:
qcstr = str(self.querycount)
qcstr = qcstr + chr(8)*(len(qcstr)+1)
if verbose: print qcstr,
示例3: isUncat
def isUncat(page):
"""
Do we want to skip this page.
If we found a category which is not in the ignore list it means
that the page is categorized so skip the page.
If we found a template which is in the ignore list, skip the page.
"""
pywikibot.output(u'Working on ' + page.title())
for category in page.categories():
if category not in ignoreCategories:
pywikibot.output(u'Got category ' + category.title())
return False
for templateWithTrail in page.templates():
# Strip of trailing garbage
template = templateWithTrail.title().rstrip('\n').rstrip()
if template in skipTemplates:
# Already tagged with a template, skip it
pywikibot.output(u'Already tagged, skip it')
return False
elif template in ignoreTemplates:
# template not relevant for categorization
pywikibot.output(u'Ignore ' + template)
else:
pywikibot.output(u'Not ignoring ' + template)
return False
return True
示例4: run
def run(self):
"""
Starts the robot.
"""
for imagePage in self.generator:
pywikibot.output(u'Working on %s' % (imagePage.title(),))
if imagePage.title(withNamespace=False) in self.withImage:
pywikibot.output(u'Image is already in use in item %s' % (self.withImage.get(imagePage.title(withNamespace=False),)))
continue
text = imagePage.get()
regex = '\s*\|\s*accession number\s*=\s*([^\s]+)\s*'
match = re.search(regex, text)
if match:
paintingId = match.group(1).strip()
pywikibot.output(u'Found ID %s on the image' % (paintingId,))
if paintingId in self.withoutImage:
pywikibot.output(u'Found an item to add it to!')
paintingItemTitle = u'Q%s' % (self.withoutImage.get(paintingId),)
paintingItem = pywikibot.ItemPage(self.repo, title=paintingItemTitle)
paintingItem.get()
if u'P18' not in paintingItem.claims:
newclaim = pywikibot.Claim(self.repo, u'P18')
newclaim.setTarget(imagePage)
pywikibot.output('Adding image claim to %s' % paintingItem)
summary = u'Adding image based on %s' % (paintingId,)
paintingItem.addClaim(newclaim, summary=summary)
示例5: test_archivebot
def test_archivebot(self, code=None):
"""Test archivebot for one site."""
site = self.get_site(code)
if code != 'de': # bug T69663
page = pywikibot.Page(site, 'user talk:xqt')
else:
page = pywikibot.Page(site, 'user talk:ladsgroup')
talk = archivebot.DiscussionPage(page, None)
self.assertIsInstance(talk.archives, dict)
self.assertIsInstance(talk.archived_threads, int)
self.assertTrue(talk.archiver is None)
self.assertIsInstance(talk.header, basestring)
self.assertIsInstance(talk.timestripper, TimeStripper)
self.assertIsInstance(talk.threads, list)
self.assertGreaterEqual(
len(talk.threads), THREADS[code],
u'{0:d} Threads found on {1!s},\n{2:d} or more expected'.format(len(talk.threads), talk, THREADS[code]))
for thread in talk.threads:
self.assertIsInstance(thread, archivebot.DiscussionThread)
self.assertIsInstance(thread.title, basestring)
self.assertIsInstance(thread.now, datetime)
self.assertEqual(thread.now, talk.now)
self.assertIsInstance(thread.ts, TimeStripper)
self.assertEqual(thread.ts, talk.timestripper)
self.assertIsInstance(thread.code, basestring)
self.assertEqual(thread.code, talk.timestripper.site.code)
self.assertIsInstance(thread.content, basestring)
try:
self.assertIsInstance(thread.timestamp, datetime)
except AssertionError:
if thread.code not in self.expected_failures:
pywikibot.output('code {0!s}: {1!s}'.format(thread.code, thread.content))
raise
示例6: main
def main(*args):
"""
Process command line arguments and invoke bot.
If args is an empty list, sys.argv is used.
@param args: command line arguments
@type args: list of unicode
"""
all = False
new = False
sysop = False
for arg in pywikibot.handle_args(args):
if arg in ('-all', '-update'):
all = True
elif arg == '-new':
new = True
elif arg == '-sysop':
sysop = True
if all:
refresh_all(sysop=sysop)
elif new:
refresh_new(sysop=sysop)
else:
site = pywikibot.Site()
watchlist = refresh(site, sysop=sysop)
pywikibot.output(u'{0:d} pages in the watchlist.'.format(len(watchlist)))
for page in watchlist:
try:
pywikibot.stdout(page.title())
except pywikibot.InvalidTitle:
pywikibot.exception()
示例7: addReleased
def addReleased(self, item, imdbid):
'''
Add the first airdate to the item based on the imdbid
'''
pywikibot.output(u'Trying to add date to %s based on %s' % (item, imdbid))
data = item.get()
claims = data.get('claims')
if u'P1191' in claims:
return True
if imdbid not in self.imdbcache:
return False
releasedate = self.imdbcache[imdbid].get('released')
regex = u'^(\d\d\d\d)-(\d\d)-(\d\d)$'
match = re.match(regex, releasedate)
if not match:
return False
newdate = pywikibot.WbTime(year=int(match.group(1)),
month=int(match.group(2)),
day=int(match.group(3)),)
newclaim = pywikibot.Claim(self.repo, u'P1191')
newclaim.setTarget(newdate)
pywikibot.output('Adding release date claim %s to %s' % (releasedate, item))
item.addClaim(newclaim)
refurl = pywikibot.Claim(self.repo, u'P854')
refurl.setTarget(u'http://www.omdbapi.com/?i=%s' % (imdbid,))
refdate = pywikibot.Claim(self.repo, u'P813')
today = datetime.datetime.today()
date = pywikibot.WbTime(year=today.year, month=today.month, day=today.day)
refdate.setTarget(date)
newclaim.addSources([refurl, refdate])
示例8: findCommonscatLink
def findCommonscatLink(self, page=None):
"""Find CommonsCat template on interwiki pages.
In Pywikibot 2.0, page.interwiki() now returns Link objects,
not Page objects
@rtype: unicode, name of a valid commons category
"""
for ipageLink in page.langlinks():
ipage = pywikibot.page.Page(ipageLink)
pywikibot.log("Looking for template on %s" % (ipage.title()))
try:
if (not ipage.exists() or ipage.isRedirectPage() or
ipage.isDisambig()):
continue
commonscatLink = self.getCommonscatLink(ipage)
if not commonscatLink:
continue
(currentTemplate,
possibleCommonscat, linkText, Note) = commonscatLink
checkedCommonscat = self.checkCommonscatLink(possibleCommonscat)
if (checkedCommonscat != u''):
pywikibot.output(
u"Found link for %s at [[%s:%s]] to %s."
% (page.title(), ipage.site.code,
ipage.title(), checkedCommonscat))
return checkedCommonscat
except pywikibot.BadTitle:
# The interwiki was incorrect
return u''
return u''
示例9: run
def run(self):
# If the enable page is set to disable, turn off the bot
# (useful when the bot is run on a server)
if not self.enable_page():
pywikibot.output('The bot is disabled')
return
super(LonelyPagesBot, self).run()
示例10: createGraph
def createGraph(self):
"""
Create graph of the interwiki links.
For more info see U{http://meta.wikimedia.org/wiki/Interwiki_graphs}
"""
pywikibot.output(u'Preparing graph for %s'
% self.subject.originPage.title())
# create empty graph
self.graph = pydot.Dot()
# self.graph.set('concentrate', 'true')
self.octagon_sites = self._octagon_site_set()
for page in self.subject.foundIn.keys():
# a node for each found page
self.addNode(page)
# mark start node by pointing there from a black dot.
firstLabel = self.getLabel(self.subject.originPage)
self.graph.add_node(pydot.Node('start', shape='point'))
self.graph.add_edge(pydot.Edge('start', firstLabel))
for page, referrers in self.subject.foundIn.items():
for refPage in referrers:
self.addDirectedEdge(page, refPage)
self.saveGraphFile()
示例11: fillCaches
def fillCaches(collectionqid):
'''
Build an ID cache so we can quickly look up the id's for property.
Only return items in this ID cache for which we don't already have the Art UK artwork ID (P1679) link
Build a second art uk -> Qid cache for items we don't have to process
'''
invcache = {}
artukcache = {}
sq = pywikibot.data.sparql.SparqlQuery()
# FIXME: Do something with the collection qualifier
query = u'SELECT ?item ?inv ?artukid WHERE { ?item wdt:P195 wd:%s . ?item wdt:P217 ?inv . OPTIONAL { ?item wdt:P1679 ?artukid } } ' % (collectionqid,)
sq = pywikibot.data.sparql.SparqlQuery()
queryresult = sq.select(query)
for resultitem in queryresult:
qid = resultitem.get('item').replace(u'http://www.wikidata.org/entity/', u'')
if resultitem.get('artukid'):
artukcache[resultitem.get('artukid')] = qid
else:
invcache[resultitem.get('inv')] = qid
pywikibot.output(u'The query "%s" returned %s with and %s items without an ART UK work link' % (query,
len(artukcache),
len(invcache)))
return (invcache,artukcache)
示例12: update_or_create_page
def update_or_create_page(self, old_page, new_text):
"""
Reads the current text of page old_page,
compare it with new_text, prompts the user,
and uploads the page
"""
# Read the original content
old_text = old_page.get()
# Give the user some context
if old_text != new_text:
pywikibot.output(new_text)
pywikibot.showDiff(old_text, new_text)
# Get a decision
prompt = u'Modify this page ?'
# Did anything change ?
if old_text == new_text:
pywikibot.output(u'No changes necessary to %s' % old_page.title());
else:
if not self.acceptall:
choice = pywikibot.input_choice(u'Do you want to accept these changes?', [('Yes', 'Y'), ('No', 'n'), ('All', 'a')], 'N')
if choice == 'a':
self.acceptall = True
if self.acceptall or choice == 'y':
# Write out the new version
old_page.put(new_text, summary)
示例13: addQualifier
def addQualifier(self, item, claim, qual):
"""
Check if a qualifier is present at the given claim,
otherwise add it
Known issue: This will qualify an already referenced claim
this must therefore be tested before
param item: itemPage to check
param claim: Claim to check
param qual: Qualifier to check
"""
# check if already present
if self.hasQualifier(qual, claim):
return False
qClaim = self.make_simple_claim(qual.prop, qual.itis)
try:
claim.addQualifier(qClaim) # writes to database
pywikibot.output('Adding qualifier %s to %s in %s' %
(qual.prop, claim.getID(), item))
return True
except pywikibot.data.api.APIError, e:
if e.code == u'modification-failed':
pywikibot.output(u'modification-failed error: '
u'qualifier to %s to %s in %s' %
(qual.prop, claim.getID(), item))
return False
else:
raise pywikibot.Error(
'Something went very wrong trying to add a qualifier: %s' %
e)
示例14: addReference
def addReference(self, item, claim, ref):
"""Add a reference if not already present.
param item: the item on which all of this happens
param claim: the pywikibot.Claim to be sourced
param ref: the WD.Reference to add
"""
# check if any of the sources are already present
# note that this can be in any of its references
if ref is None:
return False
if any(self.hasRef(source.getID(), source.getTarget(), claim)
for source in ref.source_test):
return False
try:
claim.addSources(ref.get_all_sources()) # writes to database
pywikibot.output('Adding reference claim to %s in %s' %
(claim.getID(), item))
return True
except pywikibot.data.api.APIError, e:
if e.code == u'modification-failed':
pywikibot.output(u'modification-failed error: '
u'ref to %s in %s' % (claim.getID(), item))
return False
else:
raise pywikibot.Error(
'Something went very wrong trying to add a source: %s' % e)
示例15: fillCache
def fillCache(self, collectionqid, idProperty, queryoverride=u'', cacheMaxAge=0):
'''
Query Wikidata to fill the cache of items we already have an object for
'''
result = {}
if queryoverride:
query = queryoverride
else:
query = u'CLAIM[195:%s] AND CLAIM[%s]' % (collectionqid.replace(u'Q', u''),
idProperty,)
wd_queryset = wdquery.QuerySet(query)
wd_query = wdquery.WikidataQuery(cacheMaxAge=cacheMaxAge)
data = wd_query.query(wd_queryset, props=[str(idProperty),])
if data.get('status').get('error')=='OK':
expectedItems = data.get('status').get('items')
props = data.get('props').get(str(idProperty))
for prop in props:
# FIXME: This will overwrite id's that are used more than once.
# Use with care and clean up your dataset first
result[prop[2]] = prop[0]
if expectedItems==len(result):
pywikibot.output('I now have %s items in cache' % expectedItems)
else:
pywikibot.output('I expected %s items, but I have %s items in cache' % (expectedItems, len(result),))
return result