Usuari:KRLS/codi/PESCAR.py

De la Viquipèdia, l'enciclopèdia lliure
# -*- coding: utf-8 -*-

class Ranking():
    def __init__(self):
        self.globalNumberEditions = 0
        self.globalEditedArticles = 0 
        self.usersEngaged = []
        self.bytesAdded = 0

        self.globalRanking = {}

        self.politicsRanking = {}
        self.economicsRanking = {}
        self.societyRanking = {}
        self.cultureRanking = {}
        self.artsRanking = {}
        self.religionRanking = {}

        self.ancientHistoryRanking = {}
        self.middleAgesRanking = {}
        self.earlyModernPeriodRanking = {}
        self.modernityRanking = {}
        self.category = {u'P': self.politicsRanking, u'E': self.economicsRanking, u'S': self.societyRanking, u'C': self.cultureRanking, u'A': self.artsRanking, u'R': self.religionRanking} 
        self.era = {u'gener': self.ancientHistoryRanking , u'febrer': self.ancientHistoryRanking , u'març': self.ancientHistoryRanking , u'abril': self.middleAgesRanking, u'maig': self.middleAgesRanking, u'juny': self.middleAgesRanking, u'juliol': self.earlyModernPeriodRanking, u'agost': self.earlyModernPeriodRanking, u'setembre': self.earlyModernPeriodRanking, u'octubre': self.modernityRanking, u'novembre': self.modernityRanking, u'desembre': self.modernityRanking}

class Article():

    def __init__(self, name, category, value, month):
        self.name = name
        self.category = category
        self.value = value
        self.month = month

import pywikibot
import sys
import re
from datetime import datetime, date
import codecs

ranking = Ranking()
fOut = codecs.open('output.output', 'wb', 'utf8')

def setArticles(page):
    listArticles = {}
    articles = page.split('\n')
    for art in articles:
        atributes = art.split('|')
        name = atributes[0].strip()
        article = Article(name, atributes[1].strip(), atributes[2].strip(), atributes[3].strip())
        listArticles[name] = article
    return listArticles

def readArticles():
    site = pywikibot.Site("ca", "wikipedia")
    page = pywikibot.Page(site, u"Viquiprojecte:PESCAR/seguiment/articles")
    text = page.get()
    listArticles = setArticles(text)
    return listArticles

def convertToPuntation(puntuation, article):
    convertedList = {}
    for punt in puntuation:
        points = puntuation[punt] / 1000
        if points > 0:
            convertedList[punt] = int(points) * int(article.value)
    return convertedList

def getScore(listArticles):
    site = pywikibot.Site("ca", "wikipedia")
    globalPuntuation = {}
    for art in listArticles:
        article = listArticles[art]
        print article.name
        page = pywikibot.Page(site, article.name)
        try:
            if page.isRedirectPage():
                page = page.getRedirectTarget()
            text = page.get()
            specificPuntuation = {}
            found = False
            for rev in page.revisions():
                #print dir(rev)
                date = rev.timestamp
                if date >= datetime(2017,1, 1, 0, 0, 0) and date < datetime(2018, 1, 1, 0, 0, 0):
                    found = True
                    ranking.globalNumberEditions += 1 #countEditions
                    previousRevLength = 0 if rev.parent_id == 0 else len(page.getOldVersion(rev.parent_id))
                    currentRevLength = len(page.getOldVersion(rev.revid))
                    diffFromPrecedingVersion = currentRevLength-previousRevLength
                    ranking.bytesAdded += diffFromPrecedingVersion #countBytesAdded
                    if rev.user in specificPuntuation:
                        specificPuntuation[rev.user] = specificPuntuation[rev.user] + diffFromPrecedingVersion
                    else:
                        if rev.user not in ranking.usersEngaged:
                            ranking.usersEngaged.append(rev.user) #countUsersEngaged
                        specificPuntuation[rev.user] = diffFromPrecedingVersion
            if found:
                ranking.globalEditedArticles += 1 #countEditedArticles
            convertedList = convertToPuntation(specificPuntuation, article)
            globalPuntuation[article.name] = convertedList
        except pywikibot.exceptions.NoPage as nopage:
            print "EXCEPTION " + article.name

    return globalPuntuation

def assignPuntuation(score, listArticles):
    for articlePunt in score:
        sortedPunt = sorted(score[articlePunt].iteritems(), key=lambda x: x[1], reverse=True)

        for elem in sortedPunt:
            #Global Puntuation assignation
            if elem[0] not in ranking.globalRanking:
                ranking.globalRanking[elem[0]] = elem[1]
            else:
                ranking.globalRanking[elem[0]] += elem[1]
            
            #Era assignation
            month = listArticles[articlePunt].month
            monthEra = ranking.era[month]

            if elem[0] in monthEra:
                monthEra[elem[0]] += elem[1]
            else:
                monthEra[elem[0]] = elem[1]

            #Category assignation:

            category = listArticles[articlePunt].category
            categoryList = ranking.category[category]

            if elem[0] in categoryList:
                categoryList[elem[0]] += elem[1]
            else:
                categoryList[elem[0]] = elem[1]

def showSpecificRanking(rank):
    text = u''
    sortedRank = sorted(rank.iteritems(), key=lambda x: x[1], reverse=True)
    for i in range(0, min(5, len(sortedRank))):
        elem = sortedRank[i]
        user = elem[0]
        punt = elem[1]
        if i > 10:
            #Els 3 primers només en negreta
            text += u"# '''{{{{u|{0}}}}}''' {1} punts\n".format(user, int(punt)) 
        else:
            text += u"# {{{{u|{0}}}}} {1} punts\n".format(user, int(punt))
    if len(sortedRank) > 5:
        text += '\n\n{{collapse top|Llista completa|bg=#fff}}\n{|class="wikitable sortable"\n! N. !! Viquipedista !! Punts\n'
        for i in range(0, len(sortedRank)):
            elem = sortedRank[i]
            user = elem[0]
            punt = elem[1]
            text += u'|-\n| {0} || {{{{u|{1}}}}} || {2}\n'.format(i+1, user, int(punt))

        text += u'|}\n{{collapse bottom}}\n'
    return text

def showPuntuation():
    site = pywikibot.Site("ca", "wikipedia")
    page = pywikibot.Page(site, "Viquiprojecte:PESCAR/seguiment 2017")
    text = u"<center>[[Fitxer:P-AvenidaBoyacáCentro.png|50px]][[Fitxer:E-NQS Central.png|50px]][[Fitxer:S_Societat.svg|50px]][[Fitxer:TransMilenio Estacion C Suba.svg|50px]][[Fitxer:A-Caracas.svg|50px]][[Fitxer:R_Religió.svg|50px]]</center>\nEstadístiques de seguiment del [[Viquiprojecte:PESCAR]]. Les dades són relacionades amb les edicions documentades amb l\'etiqueta [https://tools.wmflabs.org/hashtags/search/PESCAR #PESCAR] en el resum d'edició durant el 2017 en algun dels articles llistats a la pàgina de projecte.\n\n:\'\'Darrera actualització: {0}\'\'".format(datetime.utcnow())
    
    #showGlobalStatistic

    text += u"\n== Resum Global PESCAR ==\n# '''{0}''' edicions\n# '''{1}''' articles editats\n# '''{2}''' participants\n# '''{3}''' bytes modificats\n".format(int(ranking.globalNumberEditions), int(ranking.globalEditedArticles), int(len(ranking.usersEngaged)), int(ranking.bytesAdded))
    

    #showGlobalRanking
    text += u"== Rànquing Global PESCAR ==\nRànquing de viquipedistes per nombre de punts:\n"
    text += showSpecificRanking(ranking.globalRanking)

    #ShowEixos
    text += u"== Rànquing per eixos ==\n"
    #showPolitics
    text += u"=== [[Fitxer:P-AvenidaBoyacáCentro.png|50px]] ([[política]]) ===\n"
    text += showSpecificRanking(ranking.category[u'P'])
    #showEconomics
    text += u"=== [[Fitxer:E-NQS Central.png|50px]] ([[economia]]) ===\n"
    text += showSpecificRanking(ranking.category[u'E'])
    #showSociety
    text += u"=== [[Fitxer:S_Societat.svg|50px]] ([[societat]]) ===\n"
    text += showSpecificRanking(ranking.category[u'S'])
    #showCulture
    text += u"=== [[Fitxer:TransMilenio Estacion C Suba.svg|50px]] ([[cultura]]) ===\n"
    text += showSpecificRanking(ranking.category[u'C'])
    #showArts
    text += u"=== [[Fitxer:A-Caracas.svg|50px]] ([[arts]]) ===\n"
    text += showSpecificRanking(ranking.category[u'A'])
    #showReligion
    text += u"=== [[Fitxer:R_Religió.svg|50px]] ([[religió]]) ===\n"
    text += showSpecificRanking(ranking.category[u'R'])

    #ShowEras
    text += u"== Rànquing per Edats ==\n"
    #showAncientHistory
    text += u"=== [[Fitxer:Berlín - Pergamon - Porta d'Ishtar - Ur.JPG|50px]] Edat Antiga ===\n"
    text += showSpecificRanking(ranking.era[u'gener'])
    #showMiddleAges
    text += u"=== [[Fitxer:Monogramme Karolus, Aachen, Germany.jpg|50px]] Edat Mitjana ===\n"
    text += showSpecificRanking(ranking.era[u'abril'])
    #showEarlyModernPeriod
    text += u"=== [[Fitxer:NewtonsPrincipia.jpg|50px]] Edat moderna ===\n"
    text += showSpecificRanking(ranking.era[u'juliol'])
    #showModernity
    text += u"=== [[Fitxer:Into the Jaws of Death 23-0455M edit.jpg|50px]] Edat Contemporània ===\n"
    text += showSpecificRanking(ranking.era[u'octubre'])

    print text
    page.put(text, comment=u'En fase de proves: Actualitzant puntuació #PESCAR', minorEdit = True)
    
def main():
    listArticles = readArticles()
    score = getScore(listArticles)
    assignPuntuation(score, listArticles)
    showPuntuation()

if __name__ == '__main__':
    main()