def send_mailnotification(text, subject): username = pywikibot.config.usernames[pywikibot.config.family][ pywikibot.config.mylang] pos = username.lower().find('bot') username = username[:pos] if (pos > 0) else username pywikibot.output(u'Sending mail "%s" to "%s" as notification!' % (subject, username)) # JIRA: DRTRIGON-87; output even more debug info (tip by: [email protected]) site = pywikibot.getSite() pywikibot.output(u'Bot allowed to send email: %r' % (site.isAllowed('sendemail'), )) pywikibot.output(u'Permissions: %r' % (site._rights, )) if not site.isAllowed('sendemail'): pywikibot.output(u'Try getting new token: %r' % (site.getToken(getagain=True), )) usr = userlib.User(site, username) try: if usr.sendMail(subject=subject, text=text): # 'text' should be unicode! return except: # break exception handling recursion pywikibot.exception(tb=True) pywikibot.error(u'mail to %s could not be sent!' % username) pywikibot.output(u'May be not logged in - try to send emergency email') try: import smtplib from email.mime.text import MIMEText # sender's and recipient's email addresses FROM = "*****@*****.**" % username.lower() TO = [FROM] # must be a list # Create a text/plain message msg = MIMEText(text) msg['Subject'] = "!EMERGENCY! " + subject msg['From'] = FROM msg['To'] = ", ".join(TO) # Send the mail server = smtplib.SMTP("localhost") server.sendmail(FROM, TO, msg.as_string()) server.quit() except: # break exception handling recursion pywikibot.exception(tb=True) pywikibot.error(u'emergency mail to %s could not be sent!' % TO)
def set_all(keys, values, verbose=False): log = open('preferences.txt', 'a') log.write('PREFERENCES\t%s\n' % time.gmtime()) log.write('KEYS\t%s\n' % keys) log.write('VALUES\t%s\n' % values) for family in config.usernames: for lang in config.usernames[family]: try: set_for(lang, family, keys, values, verbose) except (SystemExit, KeyboardInterrupt): return except: pywikibot.exception(tb=True) pywikibot.warning(u'An exception occured!') log.write('FAILED\t%s\t%s\n' % (family, lang)) else: log.write('SUCCESS\t%s\t%s\n' % (family, lang)) log.close()
def send_mailnotification(text, subject): username = pywikibot.config.usernames[pywikibot.config.family][pywikibot.config.mylang] pos = username.lower().find("bot") username = username[:pos] if (pos > 0) else username pywikibot.output(u'Sending mail "%s" to "%s" as notification!' % (subject, username)) # JIRA: DRTRIGON-87; output even more debug info (tip by: [email protected]) site = pywikibot.getSite() pywikibot.output(u"Bot allowed to send email: %r" % (site.isAllowed("sendemail"),)) pywikibot.output(u"Permissions: %r" % (site._rights,)) if not site.isAllowed("sendemail"): pywikibot.output(u"Try getting new token: %r" % (site.getToken(getagain=True),)) usr = userlib.User(site, username) try: if usr.sendMail(subject=subject, text=text): # 'text' should be unicode! return except: # break exception handling recursion pywikibot.exception(tb=True) pywikibot.error(u"mail to %s could not be sent!" % username) pywikibot.output(u"May be not logged in - try to send emergency email") try: import smtplib from email.mime.text import MIMEText # sender's and recipient's email addresses FROM = "*****@*****.**" % username.lower() TO = [FROM] # must be a list # Create a text/plain message msg = MIMEText(text) msg["Subject"] = "!EMERGENCY! " + subject msg["From"] = FROM msg["To"] = ", ".join(TO) # Send the mail server = smtplib.SMTP("localhost") server.sendmail(FROM, TO, msg.as_string()) server.quit() except: # break exception handling recursion pywikibot.exception(tb=True) pywikibot.error(u"emergency mail to %s could not be sent!" % TO)
"release_rev": str(__release["rev"]), "release_ver": __release["hsh"][:7], "online_ver": version.getversion_onlinerepo()[:7], } pywikibot.output(d["bot_control"]) pywikibot.output(u"=== " * 14) d.close() sys.path.append(os.path.split(sys.argv[0])[0]) execfile(sys.argv[0]) exitcode = ERROR_SGE_ok pywikibot.output(u"") pywikibot.output(u"DONE") except: pywikibot.exception(tb=True) error = traceback.format_exc() if pywikibot.logger.isEnabledFor(pywikibot.DEBUG): exitcode = ERROR_SGE_ok # print traceback of re-raised errors by skipping sys.exit() raise else: send_mailnotification(error, u"Bot ERROR") pywikibot.output(u"") pywikibot.warning(u"DONE with Exception(s) occured in Bot") finally: site = pywikibot.getSite() name = str("%s-%s-%s" % (bot_name, site.family.name, site.lang)) d = shelve.open(pywikibot.config.datafilepath("cache", "state_bots")) d[name] = { "error": str(bool(error)), "traceback": str(error.encode("utf-8")),
'release_rev': str(__release['rev']), 'release_ver': __release['hsh'][:7], 'online_ver': version.getversion_onlinerepo()[:7], } pywikibot.output(d['bot_control']) pywikibot.output(u'=== ' * 14) d.close() sys.path.append(os.path.split(sys.argv[0])[0]) execfile(sys.argv[0]) exitcode = ERROR_SGE_ok pywikibot.output(u'') pywikibot.output(u'DONE') except: pywikibot.exception(tb=True) error = traceback.format_exc() if pywikibot.logger.isEnabledFor(pywikibot.DEBUG): exitcode = ERROR_SGE_ok # print traceback of re-raised errors by skipping sys.exit() raise else: send_mailnotification(error, u'Bot ERROR') pywikibot.output(u'') pywikibot.warning(u'DONE with Exception(s) occured in Bot') finally: site = pywikibot.getSite() name = str('%s-%s-%s' % (bot_name, site.family.name, site.lang)) d = shelve.open(pywikibot.config.datafilepath('cache', 'state_bots')) d[name] = { 'error': str(bool(error)), 'traceback': str(error.encode('utf-8')),
def data_save(self, page, data): """Stores the content to Wikidata. @param page: Page containing template. @type page: page object @param data: New content. @type data: dict Returns nothing, but stores the changed content to linked labels. """ # DRTRIGON-130: check for changes and then write/change/set values datapage = pywikibot.DataPage(self.site, page.title()) dataitem = u'%s:%s' % (self._bot_config['BotName'], datapage.title().split(u':')[1]) links = [{u'aliases': [u'%s:%s' % (dataitem, p.sortkeyprefix)], u'id': p.toggleTalkPage().title().lower()} for p in catlib.Category(self.site, dataitem).articles()] links += datapage.searchentities(dataitem) for element in links: propid = int(self._bot_config['data_PropertyId']) el = element[u'aliases'][0].split(u':') item = el[2] if item not in data: pywikibot.output(u'Value "%s" not found.' % (item,)) data[item] = u'%s: N/A' % self._bot_config['BotName'] if len(el) > 3: propid = int(el[3]) dataoutpage = pywikibot.DataPage(self.site, element['id']) # check for changes and then write/change/set values summary = (u'Bot: update data because of configuration on %s.' % page.title(asLink=True)) buf = dataoutpage.get() claim = [claim for claim in buf[u'claims'] if (claim['m'][1] == propid)] # TODO: does this check (if) work with multiple claims per property? if (not claim) or (claim[0]['m'][3] != data[item]): pywikibot.output(u'%s in %s changed to "%s"' % (element[u'aliases'][0], dataoutpage.title(asLink=True), data[item])) ### BUG 57480: references cannot be set correctly anymore ### ('try ... except' has to be considered just a work-a-round) try: dataoutpage.editclaim( u'p%s' % propid, data[item], refs={"p%s" % propid: [{"snaktype": "value", "property": "p%s" % propid, "datavalue": {u'type': u'string', u'value': datapage.title()}}, {"snaktype": "value", "property": "p585", # point in time #"property": "p578", # Sandbox-TimeValue "datavalue": {u'type': u'time', u'value': {u'after': 0, u'precision': 11, u'time': (u'+0000000%sZ' % pywikibot.Timestamp.now().isoformat().split('.')[0]), u'timezone': 0, u'calendarmodel': u'http://www.wikidata.org/entity/Q1985727', u'before':0}}}, ]}, comment=summary) except RuntimeError: pywikibot.exception()
def data_save(self, page, data): """Stores the content to Wikidata. @param page: Page containing template. @type page: page object @param data: New content. @type data: dict Returns nothing, but stores the changed content to linked labels. """ # DRTRIGON-130: check for changes and then write/change/set values datapage = pywikibot.DataPage(self.site, page.title()) dataitem = u'%s:%s' % (self._bot_config['BotName'], datapage.title().split(u':')[1]) links = [{ u'aliases': [u'%s:%s' % (dataitem, p.sortkeyprefix)], u'id': p.toggleTalkPage().title().lower() } for p in catlib.Category(self.site, dataitem).articles()] links += datapage.searchentities(dataitem) for element in links: propid = int(self._bot_config['data_PropertyId']) el = element[u'aliases'][0].split(u':') item = el[2] if item not in data: pywikibot.output(u'Value "%s" not found.' % (item, )) data[item] = u'%s: N/A' % self._bot_config['BotName'] if len(el) > 3: propid = int(el[3]) dataoutpage = pywikibot.DataPage(self.site, element['id']) # check for changes and then write/change/set values summary = (u'Bot: update data because of configuration on %s.' % page.title(asLink=True)) buf = dataoutpage.get() claim = [ claim for claim in buf[u'claims'] if (claim['m'][1] == propid) ] # TODO: does this check (if) work with multiple claims per property? if (not claim) or (claim[0]['m'][3] != data[item]): pywikibot.output(u'%s in %s changed to "%s"' % (element[u'aliases'][0], dataoutpage.title(asLink=True), data[item])) ### BUG 57480: references cannot be set correctly anymore ### ('try ... except' has to be considered just a work-a-round) try: dataoutpage.editclaim( u'p%s' % propid, data[item], refs={ "p%s" % propid: [ { "snaktype": "value", "property": "p%s" % propid, "datavalue": { u'type': u'string', u'value': datapage.title() } }, { "snaktype": "value", "property": "p585", # point in time #"property": "p578", # Sandbox-TimeValue "datavalue": { u'type': u'time', u'value': { u'after': 0, u'precision': 11, u'time': (u'+0000000%sZ' % pywikibot.Timestamp.now( ).isoformat().split('.')[0]), u'timezone': 0, u'calendarmodel': u'http://www.wikidata.org/entity/Q1985727', u'before': 0 } } }, ] }, comment=summary) except RuntimeError: pywikibot.exception()