def test_titles_User(self): params = { 'action': 'query', 'list': 'users', 'usprop': ['registration'], 'ususers': [ userlib.User(self.site, u'Example'), userlib.User(self.site, u'Example2') ], } expectedresult = { u'users': [ { u'userid': 215131, u'name': u'Example', u'registration': u'2005-03-19T00:17:19Z' }, { u'userid': 5176706, u'name': u'Example2', u'registration': u'2007-08-26T02:13:33Z' }, ] } self.assertEqualQueryResult(params, expectedresult)
def deadlineOfModerator(mod): modObj = userlib.User(wikipedia.getSite('nl', 'wikipedia'), mod) contributions = modObj.contributions(limit=251) edit250ago = str(nth(contributions, 250)[2]) return datetime.datetime( int(edit250ago[0:4]) + 1, int(edit250ago[4:6]), int(edit250ago[6:8]), int(edit250ago[8:10]), int(edit250ago[10:12]), int(edit250ago[12:14]))
def start_tornado(config, config_filename): if not options.cookie_secret: logging.warn( "\n\n\t\t!!! WARNNING !!!\n\n" "You must specify the cookie_secret option. It should be a long " "random sequence of bytes to be used as the HMAC secret for the " "signature.\n\n" "To keep the Shadowsocks Web Interface runable as always it be, " "it's signed by a random cookie_secret option. Yes, this chould " "keep the service runable, but also effects the users have to " "re-login every time the system administrator restart the " "service or reboot the system. YOU ARE NOTICED.\n\n" "You can create this HMAC string by typing following on server:\n" "\t%s --hmac" % sys.argv[0]) options.cookie_secret = common.hmacstr(key=common.randomstr(1000), msg=common.randomstr(1000), hashtype='sha512') handlers = [ (options.base_url + r"/", RootHandler), (options.base_url + r"/robots.txt", RobotsHandler), (options.base_url + r"/dashboard", DashboardHandler), (options.base_url + r"/login", LoginHandler), (options.base_url + r"/logout", LogoutHandler), (options.base_url + r"/config", ConfigHandler), (options.base_url + r"/control", ServiceControlHandler), (options.base_url + r"/control/start", ServiceControlHandler, dict(action="start")), (options.base_url + r"/control/stop", ServiceControlHandler, dict(action="stop")), (options.base_url + r"/control/restart", ServiceControlHandler, dict(action="restart")), (options.base_url + r"/hideme", PlaneConfigHandler), ] user = userlib.User("ssweb.db") user.db_init() settings = dict(template_path=os.path.join(os.path.dirname(__file__), "templates/" + options.theme), static_path=os.path.join( os.path.dirname(__file__), "templates/" + options.theme + "/assets"), static_url_prefix=options.base_url + "/static/", cookie_secret=options.cookie_secret, login_url="/login", xsrf_cookies=True, debug=options.debug, user=user, svservname=options.service_name) application = tornado.web.Application(handlers, **settings) application.config = config application.config_filename = config_filename http_server = tornado.httpserver.HTTPServer(application) http_server.listen(options.port, options.host) info_("shadowsocks-web start at %s:%s" % (options.host, options.port)) try: tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: info_("shadowsocksweb is stoped.")
def UserContributionsGenerator(username, number = 250, namespaces = [], site = None ): """ Yields number unique pages edited by user:username namespaces : list of namespace numbers to fetch contribs from """ if site is None: site = pywikibot.getSite() user = userlib.User(site, username) for page in user.contributions(number, namespaces): yield page[0]
def readtalk(lang, familyName, sysop = False): site = wikipedia.getSite(code=lang, fam=familyName) if sysop: user = userlib.User(site, config.sysopnames[familyName][lang]) else: user = userlib.User(site, config.usernames[familyName][lang]) page = user.getUserTalkPage() if not site.loggedInAs(sysop): site.forceLogin() if site.messages(sysop): wikipedia.output("cleanning up the account new message notice") pagetext = site.getUrl(site.get_address(page.urlname()), sysop=sysop) del pagetext wikipedia.output(u'Reading talk page from %s' % user) try: wikipedia.output( page.get(get_redirect=True)+"\n") except wikipedia.NoPage: wikipedia.output("Talk page is not exist.") except wikipedia.UserBlocked: wikipedia.output("Account is blocked.")
def parseNewUserLog(self): #if __name__ != '__main__': # if self._checkQueue: # for nm in self._checkQueue: # yield userlib.User(self.site, nm) if not self.site.has_api() or self.site.versionnumber() < 13: for x in self._parseNewUserLogOld(): yield x return starttime = None if globalvar.timeoffset != 0: now = self.site.server_time() - timedelta( minutes=globalvar.timeoffset) starttime = int(now.strftime("%Y%m%d%H%M%S")) elif globalvar.offset != 0: starttime = globalvar.offset count_auto = 0 pywikibot.output("Querying new user log from API....") for x in self.site.logpages(number=globalvar.queryLimit, mode='newusers', start=starttime, dump=True): someone_found = True if 'user' not in x: continue #created twice? if not globalvar.welcomeAuto and x['action'] == 'create2': continue if not globalvar.welcomeAuto and x['action'] == 'autocreate': if not globalvar.quick: showStatus(3) pywikibot.output(u'%s has been created automatically.' % x['user']) count_auto += 1 continue yield userlib.User(self.site, x['user']) if someone_found: if globalvar.quick and count_auto > 0: showStatus() pywikibot.output(u'Ignored %d user(s) by auto-create' % count_auto) showStatus(5) pywikibot.output(u'There is nobody left to be welcomed...') else: pywikibot.output(u'\nLoaded all users...')
def UserContributionsGenerator(username, number=250, namespaces=[], site=None): """ Yields number unique pages edited by user:username namespaces : List of namespace numbers to fetch contribs from. Also accepted are None (default namespace), [] (all namespaces, default) and a callable that returns a list of namespaces. """ if site is None: site = pywikibot.getSite() if callable(namespaces): namespaces = namespaces() user = userlib.User(site, username) for page in user.contributions(number, namespaces): yield page[0]
def main(args): if not args: initial_user = "******" else: initial_user = args[0] nb = wikipedia.Site('en', "noisebridge") spam_user = userlib.User(nb, "SpammerHellDontDelete") ul = userlistpage.user_list_since_user(nb, initial_user).getUsers() for i in ul: print i if i.isBlocked(): print "Merging", i (merged, deleted) = mergeUser(nb, i, spam_user, delete=True) print "Merged:", merged print "Deleted:", deleted
def _parseNewUserLogOld(self): someone_found = False URL = self.site.log_address(globalvar.queryLimit, 'newusers') if globalvar.timeoffset != 0: now = self.site.server_time() - timedelta( minutes=globalvar.timeoffset) globalvar.offset = int(now.strftime("%Y%m%d%H%M%S")) if globalvar.offset != 0: URL += "&offset=%d" % globalvar.offset pywikibot.output("Getting new user log from Special:Log/newusers....") raw = self.site.getUrl(URL) # I search with a regex how many user have not the talk page # and i put them in a list (i find it more easy and secure). # XXX: That's the regex, if there are problems, take a look here. reg = u'\(<a href=\"' + re.escape(self.site.path()) reg += u'\?title=%s(?P<user>.*?)&(?:amp;|)action=(?:edit|editredlink|edit&redlink=1)\"' % re.escape( '%s:' % urllib.quote( self.site.namespace(3).replace(" ", "_").encode( self.site.encoding()))) reg += u'.*?</span> (?P<reason>.*?) *?</li>' p = re.compile(reg, re.UNICODE) for x in p.finditer(raw): someone_found = True userN = unicode(urllib.unquote(str(x.group('user'))), 'utf-8') #skip autocreated users (SUL) if not globalvar.welcomeAuto and self.site.mediawiki_message( 'newuserlog-autocreate-entry') in x.group('reason'): showStatus(3) pywikibot.output( u'%s has been created automatically, skipping...' % userN) continue #FIXME: It counts the first 50 edits # if number > 50, it won't work # (not *so* useful, it should be enough). yield userlib.User(self.site, userN) if someone_found: showStatus(5) pywikibot.output(u'There is nobody left to be welcomed...') else: pywikibot.output(u'\nLoaded all users...')
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 evaluateUAA(self, user_page): if 'User talk:' not in user_page.title(): return page_history = user_page.fullVersionHistory() page_history_check = True for page_revision in page_history: page_date = datetime.datetime.strptime(page_revision[1], "%Y-%m-%dT%H:%M:%SZ") if page_date >= self.run_time: if self.sentinel_text not in page_revision[3]: page_history_check = False break else: #Ok traversed down the history, now to check the 1st rev outside 7 days if self.sentinel_text not in page_revision[3]: page_history_check = False break if page_history_check == True: #Ok, cat has been on for length, now to see if the editor has edited # in 7 days user_name = user_page.titleWithoutNamespace() user_obj = userlib.User(self.site, user_name) last_edit_date = None try: latest_rev = user_obj.contributions().next() last_edit_date = datetime.datetime.strptime( latest_rev[2], "%Y%m%d%H%M%S") except StopIteration: #Hrm... User has no publically visible actions. #Hack a solution in to last_edit_date = self.run_time if last_edit_date <= self.run_time: #Ok, user hasn't edited in the same 7 day window, this is now # a stale report page_text = user_page.get() replace_text = page_text.replace(self.sentinel_text, '') print user_page return
def treat(self, userPage): """ Loads the given page, does some changes, and saves it. """ talkText = self.load(userPage) if not talkText: # sanity check. No talk page found. return unblock_tpl = self.unblock_tpl[self.site.lang] project_name = self.project_name[self.site.lang] user = userlib.User(self.site, userPage.titleWithoutNamespace()) saveAdmin = saveProject = False talkComment = None for templates in userPage.templatesWithParams(): if templates[0] == unblock_tpl: self.getInfo(user) # Step 1 # a new template is set on blocked users talk page. # Notify the blocking admin if templates[1] == [] or templates[1][0] == u'1': if self.info['action'] == 'block' or user.isBlocked(): if self.site.sitename() == 'wikipedia:de': admin = userlib.User(self.site, self.info['user']) adminPage = admin.getUserTalkPage() adminText = adminPage.get() note = pywikibot.translate( self.site.lang, self.note_admin) % self.parts comment = pywikibot.translate( self.site.lang, self.msg_admin) % self.parts adminText += note self.save(adminText, adminPage, comment, False) ### test for pt-wiki ### just print all sysops talk pages elif self.site.sitename() == 'wikipedia:pt': import pagegenerators as pg gen = pg.PreloadingGenerator(self.SysopGenerator()) for sysop in gen: print sysop.title() talkText = talkText.replace(u'{{%s}}' % unblock_tpl, u'{{%s|2}}' % unblock_tpl) talkText = talkText.replace(u'{{%s|1}}' % unblock_tpl, u'{{%s|2}}' % unblock_tpl) talkComment = pywikibot.translate( self.site.lang, self.msg_user % self.parts) # some test stuff if pywikibot.debug and self.site().loggedInAs( ) == u'Xqbot:': testPage = pywikibot.Page(self.site, 'Benutzer:Xqt/Test') test = testPage.get() test += note self.save(test, testPage, '[[WP:BA#SPP-Bot|SPPB-Test]]') else: # nicht blockiert. Fall auf DS abschließen talkText = talkText.replace(u'{{%s}}' % unblock_tpl, u'{{%s|4}}' % unblock_tpl) talkText = talkText.replace(u'{{%s|1}}' % unblock_tpl, u'{{%s|4}}' % unblock_tpl) talkComment = pywikibot.translate( self.site.lang, self.msg_done) # Step 2 # Admin has been notified. # Wait for 2 hours, than put a message to the project page elif templates[1][0] == u'2': if self.info['action'] == 'block' or user.isBlocked(): # TODO: check whether wait time is gone # check whether this entry already esists project = pywikibot.Page(self.site, project_name) projText = project.get() note = pywikibot.translate( self.site.lang, self.note_project) % self.parts comment = pywikibot.translate( self.site.lang, self.msg_admin) % self.parts projText += note self.save(projText, project, comment, botflag=False) talkText = talkText.replace(u'{{%s|2}}' % unblock_tpl, u'{{%s|3}}' % unblock_tpl) talkComment = u'Bot: [[%s|Wikipedia:Sperrprüfung]] eingetragen' \ % project_name else: # User is unblocked. Review can be closed talkText = talkText.replace(u'{{%s|2}}' % unblock_tpl, u'{{%s|4}}' % unblock_tpl) talkComment = pywikibot.translate( self.site.lang, self.msg_done) # Step 3 # Admin is notified, central project page has a message # Discussion is going on # Check whether it can be closed elif templates[1][0] == u'3': if self.info['action'] == 'block' or user.isBlocked(): pass else: # User is unblocked. Review can be closed talkText = talkText.replace(u'{{%s|3}}' % unblock_tpl, u'{{%s|4}}' % unblock_tpl) talkComment = pywikibot.translate( self.site.lang, self.msg_done) # Step 4 # Review is closed elif templates[1][0] == u'4': # nothing left to do pass else: # wrong template found pass # at last if there is a talk comment, users talk page must be changed if talkComment: self.save(talkText, userPage, talkComment)
def setUp(self): self.site = wikipedia.getSite('en', 'wikipedia') self.obj = userlib._GetAllUI(self.site, [userlib.User(self.site, "Example")], None, False)