User:WindBOT/Filters: Difference between revisions

Jump to navigation Jump to search
m (let's C)
Line 352: Line 352:
  addFilter(dictUpdater)
  addFilter(dictUpdater)
  scheduleTask(dictUpdater.scheduledRun, 3)
  scheduleTask(dictUpdater.scheduledRun, 3)
=== Update checklists on [[User:WindBOT/Item checklists|list of subscribers]] ===
def itemChecklists():
    def updateItemChecklist(checklist, schema, support):
        if not checklist.getParam('steamid'):
            checklist.setParam('error', 'Unspecified Steam ID.')
            return True
        supportedItems = {}
        for i in support:
            supportedItems[i] = 0
        try:
            steamUser = steam.user.profile(checklist.getParam('steamid'))
            backpack = steam.p2.backpack(steamUser, schema=schema)
        except steam.user.ProfileError as e:
            checklist.setParam('error', u(e))
            return True
        except Exception as e:
            checklist.setParam('error', u(e))
            return False
        for item in backpack:
            itemName = u(item.get_name()).lower()
            if itemName in supportedItems:
                supportedItems[itemName] += 1
        for item in supportedItems:
            if supportedItems[item] > 1:
                checklist.setParam(item, supportedItems[item])
            elif supportedItems[item] == 1:
                checklist.setParam(item, 'yes')
            else:
                p = checklist.getParam(item)
                if p is not None:
                    p = p.lower()
                if p in (None, 'no', '0'):
                    checklist.setParam(item, 'no')
                elif p not in ('wanted', 'want', 'do not', 'anti', 'do not want'):
                    checklist.setParam(item, 'had')
        return True
    try:
        schema = steamGetGameSchema(steam.p2)
        allItems = []
        for item in schema:
            allItems.append(u(item.get_name()).lower())
    except:
        return # No schema means no fancy
    support = []
    templateParams = compileRegex(r'\{\{\{\s*([^{}|]+?)\s*\|')
    templateCode = page('Template:User weapon checklist').getWikiText() + page('Template:User hat checklist').getWikiText() + page('Template:User weapon checklist compact').getWikiText() + page('Template:User hat checklist compact').getWikiText()
    res = templateParams.search(templateCode)
    while res:
        item = u(res.group(1)).lower()
        if item not in support and item in allItems:
            support.append(item)
        templateCode = templateCode[res.end():]
        res = templateParams.search(templateCode)
    checkPage, checkLinks, checkKeys = linkExtract(page('User:WindBOT/Item_checklists').getWikiText())
    linksLeft = checkLinks.values()[:]
    for i in range(12):
        randLink = random.choice(linksLeft)
        linksLeft.remove(randLink)
        checklist = page(randLink.getLink())
        print 'Updating', checklist
        update = False
        oldContent = u(checklist.getWikiText())
        content, templatelist, templatekeys = templateExtract(oldContent)
        for t in templatelist.values():
            if t.getName().lower().find(u'checklist') != -1:
                update = updateItemChecklist(t, schema, support)
                break
        content = templateRestore(content, templatelist, templatekeys)
        if update and oldContent != content:
            editPage(checklist, content, summary=<nowiki>u'Updated Item checklist [[:' + u(checklist.title) + u']]'</nowiki>, minor=True)
scheduleTask(itemChecklists, 2)