User:Satdeep Gill/Sandbox/Source code

This is the source code of a script that is used for updating the List of Wikipedias by sample of articles. Originally written by MarsRover and later tweaked by Boivie, minor fixes by Yerpo.

An old and out-of-date version by Smeira is available at Source code (original).

MakeScoreTable.py

edit
# -*- coding: utf_8 -*-

#
# Include this dependency: https://www.mediawiki.org/wiki/Manual:Pywikibot/Installation#Download_Pywikibot_with_SVN
#

import sys
sys.path.append('./core')

import pywikibot
from pywikibot import pagegenerators
#from pywikibot import catlib
import traceback
import os
import re
#import query
from pywikibot.data import api
import sys
import simplejson as json
from time import strftime
from sets import Set
from datetime import date

#language information
lang_info ={'en': {'name':u'English',     'localname':u'English',  'weight': 1.0},
            'new':{'name':u'Newar / Nepal Bhasa','localname':u'नेपाल भाषा'},
            'te': {'name':u'Telugu',      'localname':u'తెలుగు'},
            'bpy':{'name':u'Bishnupriya Manipuri','localname':u'ইমার ঠার/বিষ্ণুপ্রিয়া মণিপুরী'},
            'hi': {'name':u'Hindi',       'localname':u'हिन्दी', 'weight':1.0},
            'bn': {'name':u'Bengali',     'localname':u'বাংলা'},
            'ta': {'name':u'Tamil',       'localname':u'தமிழ்', 'weight':0.9},
            'ur': {'name':u'Urdu',        'localname':u'اردو'},
            'ml': {'name':u'Malayalam',   'localname':u'മലയാളം', 'weight':1.1},
            'kn': {'name':u'Kannada',     'localname':u'ಕನ್ನಡ', 'weight':0.9},
            'sa': {'name':u'Sanskrit',    'localname':u'संस्कृतम्'},
            'ne': {'name':u'Nepali',      'localname':u'नेपाली'},
            'bh': {'name':u'Bihari',      'localname':u'भोजपुरी'},
            'pi': {'name':u'Pali',        'localname':u'पाऴि'},
            'si': {'name':u'Sinhalese',   'localname':u'සිංහල'},
            'or': {'name':u'Oriya',       'localname':u'ଓଡ଼ିଆ'},
            'gu': {'name':u'Gujarati',    'localname':u'ગુજરાતી'},
            'ks': {'name':u'Kashmiri',    'localname':u'कश्मीरी / كشميري'},
            'pa': {'name':u'Punjabi',     'localname':u'ਪੰਜਾਬੀ'},
            'as': {'name':u'Assamese',    'localname':u'অসমীয়া ভাষা আৰু লিপি'},
            'gom':{'name':u'Konkani',          'localname':u'कोंकणी / Konknni'},
            'tcy':{'name':u'Tulu',             'localname':u'ತುಳು'},
            'dty':{'name':u'Doteli',           'localname':u'डोटेली'},
	    'sat':{'name':u'Santali',          'localname':u'ᱥᱟᱱᱛᱟᱲᱤ'}}
# closed wikis


#languages to process
lang_keys = lang_info.keys()
#lang_keys = ['ro','sl','tr','el']
lang_keys.sort()

#optimize by caching stuff
iw_cache = {}
en_labels = {}
item_list = []

#debug
max_words = -1 

prev_score = {}

#score colors
color10000 = 'BF5FFF'
color4000 = 'FF7F00'
color2000 = 'FFBE00'
color1000 = 'FFFF00'
color500  = 'BEFF00' 
color250  = '40FF00'
color100  = '00FF7D'
color0    = 'EFEFEF'

item_list_path = "ItemList.txt"

def ListOfArticles():
    meta_page = []
    meta_wiki = pywikibot.Site('meta', 'meta')
    meta_page = pywikibot.Page(meta_wiki, 'List of articles every Wikipedia should have')
    return meta_page

def LoadItemList():
    item_path = item_list_path
    if os.path.isfile(item_path):
        return
    f = open(item_path, 'w')
    count = 0
    grand_total = 0
    page = ListOfArticles()
    article   = page.get(get_redirect=False)
    name_last  = 0
    name_first = article.find(u'[[d:', name_last)
    while name_first > -1:
        name_mid  = article.find(u'|',  name_first)
        cat_start =article.rfind(u'\n== ', name_last, name_first)
        if cat_start > -1:
            cat_end   = article.find(u'==',cat_start+3, name_first)
            if cat_end > -1: 
                cat   = article[cat_start+3:cat_end]
                catName = ''.center(len(cat),'-')
                pywikibot.output(u'\n%s' % cat)
                pywikibot.output(u'\n%s' % catName)
                count = 0

        name_last = article.find(u']]', name_first)
        if name_last > name_mid:
            name_last = name_mid
        article_item = article[name_first+4:name_last]
        f.write(article_item.encode("utf_8"))
        f.write('\n')
        count += 1
        grand_total += 1
        pywikibot.output(u'%d %s' % (count,article_item))
        name_first = article.find(u'[[d:', name_last)
 
    f.close()
 
    pywikibot.output(u'\nGRAND TOTAL\n-----------\n%d articles' % (grand_total))

def GetItemList():
    LoadItemList()
    count = 0
    item_file = open(item_list_path, 'r')
    for line in item_file:
        item = line[:-1].decode('utf_8')
        if item in item_list:
            errortext = item + " twice in list\n"
            pywikibot.output(errortext)
            with open("errorlog.txt", "a") as errorlog:
                errorlog.write(errortext)
        else:
            item_list.append(item)
    item_file.close()

def GetManyIws(itemlist):
    pipedword = '|'.join(itemlist)
    wiki = pywikibot.Site('wikidata', 'wikidata')
    #params = {
    #    'action'    :'query',
    #    'prop'      :'revisions',
    #    'redirects' :True,
    #    'titles'    :pipedword,
    #    'rvprop'    :'content'
    #    }
    #queryresult = query.GetData(params, site=wiki)
    pageRequest = api.Request(action="query", prop="revisions", redirects=True, titles=pipedword, rvprop="content", rvslots="main", site=wiki)
    queryresult = pageRequest.submit()
    #pywikibot.output(queryresult)
    pages = queryresult[u'query'][u'pages']
    word_text = {}
    newitemlist = Set()
    for k, v in pages.iteritems():
        #print 'k ', k
        #print 'v ', v
        item = v[u'title']
        newitemlist.add(item)
        if item not in itemlist:
            print 'not in ', item
            item_list.append(item)
            errortext = item + " is redirected to.\n"
            with open("errorlog.txt", "a") as errorlog:
                errorlog.write(errortext)
        try:
            pagetext=v[u'revisions'][0][u'slots'][u'main'][u'*']
            #print pagetext;
        except:
            errortext = item + " has no wikidata item\n"
            if item in item_list:
                item_list.remove(item)
            pywikibot.output(errortext)
            with open("errorlog.txt", "a") as errorlog:
                errorlog.write(errortext)
        data_dict = json.loads(pagetext)
        #print "dd ", data_dict
        try:
            iw_link_info = data_dict[u'sitelinks']
        except:
            iw_link_info = data_dict[u'links']
        iw_links = {}
        #print "ili ", iw_link_info
        print item
        try:
            for linkkey, linkvalue in iw_link_info.iteritems():
                #print "lk: ", linkkey, " lv: ", linkvalue
                iw_links[linkkey] = linkvalue[u'title']
        except:
            errortext = item + " has no links\n"
            if item in item_list:
                item_list.remove(item)
            pywikibot.output(errortext)
            with open("errorlog.txt", "a") as errorlog:
                errorlog.write(errortext)
        try:
            labels = data_dict[u'labels']
            if u'en' in labels:
                en_labels[item] = labels[u'en'][u'value']
            else:
                en_labels[item] = item
        except:
            labels = data_dict[u'label']
            if u'en' in labels:
                en_labels[item] = labels[u'en']
            else:
                en_labels[item] = item
        iw_cache[item] = iw_links
        try:
            word_text[v[u'title']]=en_labels[item]
        except:
            word_text[v[u'title']] = item
    pywikibot.output(str(word_text.values()))
    redir_items = [x for x in itemlist if x not in newitemlist]
    #print 'redirected items:', redir_items
    for redir_item in redir_items:
        item_list.remove(redir_item)
        errortext = redir_item + " is redirected from.\n"
        with open("errorlog.txt", "a") as errorlog:
            errorlog.write(errortext)
    #print 'il', item_list
    return word_text
 
def GetIwLinks():  
    iw_link_path = "IwLinks.json"
    en_label_path = "Labels.json"
    global iw_cache
    global en_labels
    if os.path.isfile(iw_link_path):
        iwf = open(iw_link_path, 'r')
        iw_cache = json.load(iwf)
        iwf.close()
        enf = open(en_label_path, 'r')
        en_labels = json.load(enf)
        enf.close()
        return
    textdict = {}
    article_group = []
    item_file = open(item_list_path)
    for line in item_file:
            item = line[:-1].decode('utf_8')
            article_group.append(item)
            if len(article_group) == 50:
                textdict.update(GetManyIws(article_group))
                article_group = []
    if len(article_group) > 0:
        textdict.update(GetManyIws(article_group))
        article_group = []
    item_file.close()
    iwf = open('IwLinks.json', 'w')
    json.dump(iw_cache, iwf)
    iwf.close()
    enf = open('Labels.json', 'w')
    json.dump(en_labels, enf)
    enf.close()
    return "cleared"

#format with spaces
def FormatNumber(s):
    r = []
    for i, c in enumerate(reversed(str(int(s)))):
        if i and i % 3 == 0:
            r.insert(0, ',')
        r.insert(0, c)
    return ''.join(r)


def GetPreviousScores():

    temp_path = "PreviousScores.txt"  
    if os.path.isfile(temp_path):
       temp_file = open(temp_path)
       for line in temp_file:
            tokens = line.split()
            prev_score[tokens[0]] = float(tokens[1])
       temp_file.close()
    

def GetArticle(item, wiki, lang):
    word = GetArticleInterwikiName(item, lang)
        
    if len(word) > 0:
        page = pywikibot.Page(wiki, word)
        article = page.get(get_redirect=True)

        if u'#REDIRECT' in article.upper():
            text_start = article.find('[[')
            text_end = article.find(']]', text_start)
            word = article[text_start+2:text_end]
            page = pywikibot.Page(wiki, word)
            article = page.get()
    else:
        article = ''

    return article


def GetArticleInterwikiName(item, lang):

    if item in iw_cache:
        iw_links = iw_cache[item]
    else:
        wikidata = pywikibot.Site('wikidata', 'wikidata')
	try:
            datapage = pywikibot.DataPage(wikidata, item)
            data_dict = datapage.get()
	except:
	    print('Where is ' + item)
	    return ''

        iw_links = data_dict[u'links']
        labels = data_dict[u'label']
        iw_cache[item] = iw_links
        if u'en' in labels:
            en_labels[item] = labels[u'en']
	else:
	    en_labels[item] = u''
    lang_wiki = lang.replace("-","_") + u'wiki'

    if lang_wiki in iw_links:
        try:
            local_name = iw_links[lang_wiki][u'name']
        except:
            local_name = iw_links[lang_wiki]
        return local_name
    else:
        return ''


def GetInterwikiLength(article):
    
    #calculate len of all interwiki links
    interwiki_len   = 0
    interwiki_last  = 0
    interwiki_colon = 0
    interwiki_nl    = 0
    interwiki_first = article.find(u'[[', interwiki_last)
    while interwiki_first > -1:    
        interwiki_last  = article.find(u']]', interwiki_first)
        interwiki_colon = article.find(u':',  interwiki_first)
        if interwiki_colon > -1 and interwiki_colon < interwiki_last:
           curlang = article[interwiki_first+2:interwiki_colon]
           if curlang in lang_info:
               interwiki_nl = article.find(u'\n', interwiki_last)
               if interwiki_nl > -1:
                  interwiki_len += (interwiki_nl - interwiki_first) + 1
               else:
                  interwiki_len += (interwiki_last - interwiki_first) + 2
        interwiki_first = article.find(u'[[', interwiki_last)

    return interwiki_len


def GetCommentLength(article):

    #calculate len of all comments
    comment_len   = 0
    comment_last  = 0
    comment_first = article.find(u'<!--', comment_last)
    while comment_first > -1:    
        comment_last = article.find(u'-->', comment_first)
        if comment_last == -1:
           comment_last = comment_first + 4
            
        comment_len += (comment_last - comment_first) - 4
        comment_first = article.find(u'<!--', comment_last)

    return comment_len

def IsArticleEnglish(article):

    #remove comments
    comments = re.compile(r'<!--(.|\n|\r)*?-->')
    article = comments.sub("", article)

    #remove references
    refs = re.compile(r'<ref(.|\n|\r)*?</ref>')
    article = refs.sub("", article)

    # convert article to lower case word list
    word_list = article.lower().split()

    if len(word_list) == 0:
        return False

    # create dictionary of word:frequency pairs
    freq_dic = {}

    # punctuation marks to be removed
    punctuation = re.compile(r'[.?!,":;]') 
    for word in word_list:
        word = punctuation.sub("", word)
        if word in freq_dic: 
            freq_dic[word] += 1
        else: 
            freq_dic[word] = 1

    # usually English is ~30% these words and non-English at most a few percent
    common_english_words = ['the','of','on','a','is','in','his','have','by','but','that','to','with','for',
                            'an','from''are','was','he','which','be','as','it','this','first', 'new', 'and',
                            'she','also','after','at','become','best','from','had','great', 'into','their',
                            'these','they','time','who','her','not','one','or', 'made', 'would','are','between']
    en_word_count = 0
    for word in common_english_words:
        if word in freq_dic:
            en_word_count += freq_dic[word]

    percent_thats_common_english = 100.0 * en_word_count / len(word_list)

    # flag if 20% or more in the list which means more than half the article is English 
    if percent_thats_common_english > 20 and  en_word_count > 20:
        print "Percent %f, %d out of %d" % (percent_thats_common_english, en_word_count, len(word_list))
        return True
    return False



def GetArticleType(wt_article_size):
   if wt_article_size == 0:
      return 'absent'
   elif 0 < wt_article_size < 10000:
      return 'stubs'
   elif 10000 <= wt_article_size < 30000:
      return 'articles'
   elif wt_article_size >= 30000:
      return 'longarticles'

def GetScoreForLang(lang):
    absent       = lang_info[lang]['absent']
    stubs        = lang_info[lang]['stubs']
    articles     = lang_info[lang]['articles']
    longarticles = lang_info[lang]['longarticles']
    return GetScore(absent, stubs, articles, longarticles)

def GetScore(absent, stubs, articles, longarticles):
    max_count = absent + stubs + articles + longarticles
    max_score = max_count * 9
    raw_score = stubs + (articles*4) + (longarticles*9)
    if max_score > 0:
        score = 100.0 * raw_score / max_score
    else:
        score = 0
    return score

def GetLink(subtable,lang,value):
    return '[[/'+subtable+'#' + lang +' '+lang_info[lang]['localname']+ '|' + value + ']]'

def GetTableNumber(count, min_subtable_count, max_subtable_count0, subtable, lang, max_subtable_count40=0):
    value = FormatNumber(count)

    max_subtable_count = max_subtable_count0
    if GetScoreForLang(lang) > 40 and max_subtable_count40 > 0:
        max_subtable_count = max_subtable_count40

    if count >= min_subtable_count and (count <= max_subtable_count or max_subtable_count==-1):
       return GetLink(subtable,lang,value)
    else:
       return value


num_lang = 0

def CalculateStatistics():
    for lang in lang_keys:
        CalculateStatisticsForLang(lang)

def GetWeightForLang(lang):
    lang_weight = 1.0
    if 'weight' in lang_info[lang]:
        lang_weight = lang_info[lang]['weight']
    elif 'similar_lang' in lang_info[lang]:
        lang_weight = lang_info[lang_info[lang]['similar_lang']]['weight']
    return lang_weight

def GetManyArticles(lang, wordlist):
    #print wordlist
    pipedword = '|'.join(wordlist.values())
    wiki = pywikibot.Site(lang, 'wikipedia')
    pageRequest = api.Request(action="query", prop="revisions", redirects=True, titles=pipedword, rvprop="content", rvslots="main", site=wiki)
    #params = {
    #    'action'    :'query',
    #    'prop'      :'revisions',
    #    'redirects' :True,
    #    'titles'    :pipedword,
    #    'rvprop'    :'content',
    #    }
    item_text = {}
    second_try = {}
    try:
        queryresult = pageRequest.submit()
        #queryresult = query.GetData(params, site=wiki)
        redirects = {}
        if queryresult[u'query'].has_key(u'redirects'):
            for redirpair in queryresult[u'query'][u'redirects']:
                redirects[redirpair[u'from']] = redirpair[u'to']
            pywikibot.output(str(redirects))
        pages = queryresult[u'query'][u'pages']
        word_text = {}
        for k, v in pages.iteritems():
            try:
                word_text[v[u'title']]=v[u'revisions'][0][u'slots'][u'main'][u'*']
            except:
                word_text[v[u'title']]=u''
        for k, v in wordlist.iteritems():
            if redirects.has_key(v):
                word = redirects[v]
            else: 
                word = v
            try:
                item_text[k] = word_text[word]
            except:
                pywikibot.output(word)
                second_try[k] = word
        pywikibot.output(str(item_text.keys()))
    except:
        second_try = wordlist
    if len(second_try)>0:
        if len(second_try)<len(wordlist):
            item_text.update(GetManyArticles(lang, second_try))
        elif len(second_try)>1:
            for k, v in second_try.iteritems():
                one_item = {}
                one_item[k] = v
                item_text.update(GetManyArticles(lang, one_item))
        else:
            for k, v in second_try.iteritems():
                item_text[k] = u''
                pywikibot.output(u'Error getting: ' + k + u' ' + v)
    return item_text
 
def GetArticleTexts(lang):  
    textdict = {}
    article_group = {}
#    item_file = open(item_list_path)
#    for index, line in enumerate(item_file):
#        item = line[:-1].decode('utf_8')
    for item in item_list:
        word            = GetArticleInterwikiName(item, lang)
        if (word == ''):
            textdict[item] = ''
        else:
            article_group[item]=word
            if len(article_group) == 50:
#                print article_group
                textdict.update(GetManyArticles(lang, article_group))
                article_group.clear()
                pywikibot.output(lang +u' '+ str(len(textdict)))
    if len(article_group) > 0:
        textdict.update(GetManyArticles(lang, article_group))
        article_group.clear()
#    item_file.close()
    return textdict


def CalculateStatisticsForLang(lang):
 
    global num_lang
    num_lang += 1
 
    print ('=['+lang+' '+str(num_lang)+ '/' + str(len(lang_keys)) + ']').ljust(76,'=')
 
    try:
 
        lang_info[lang]['total_size']   = 0
        lang_info[lang]['absent']       = 0
        lang_info[lang]['stubs']        = 0
        lang_info[lang]['articles']     = 0
        lang_info[lang]['longarticles'] = 0
 
        lang_info[lang]['art_count']    = 0
 
        temp_path = "~%s_output.txt" % (lang)
        if os.path.isfile(temp_path):
 
            temp_file = open(temp_path)
 
            art_count = int(temp_file.readline())
            lang_info[lang]['art_count']    = art_count  
            for index in range(art_count):
                artKey = 'art_'+str(index)
                lang_info[lang][artKey] = {}
                lang_info[lang][artKey]['item']  = temp_file.readline().decode('utf_8').strip()
                lang_info[lang][artKey]['name']  = temp_file.readline().decode('utf_8').strip()
                linetext = temp_file.readline()
                try:
                    lang_info[lang][artKey]['size']  = int(linetext)
                except:
                    print index, lang_info[lang][artKey]['item'], lang_info[lang][artKey]['name']
                    lang_info[lang][artKey]['size'] = 0
                lang_info[lang][artKey]['error'] = temp_file.readline().decode('utf_8').strip()
 
            temp_file.close()
 
            print '..using previous %s result...' % (lang)
 
        else:        
 
            wiki = pywikibot.Site(lang, 'wikipedia')
 
            textdict = GetArticleTexts(lang)
            word_count = 0
 
            for item, article in textdict.iteritems():
 
                word_count += 1
                if word_count > max_words > 0:
                    break
 
                article_size = 0
                error = ''
 
                try:
                    raw_article_size = len(article)
 
                    interwiki_len = GetInterwikiLength(article)
                    comment_len   = GetCommentLength(article)
                    article_size  = (raw_article_size - interwiki_len - comment_len)
 
                    if lang != "en" and lang != 'simple' and lang != 'sco' and IsArticleEnglish(article):
                        raise TypeError ("Wrong language, [[%s:%s]] has too much untranslated English." % (lang, GetArticleInterwikiName(item, lang).encode("utf-8")))
                    lang_weight = GetWeightForLang(lang)
                    print str(lang).ljust(3), str(word_count).rjust(3), item.ljust(30),
                    print str(article_size * lang_weight).rjust(11), str(lang_weight).rjust(5), str(interwiki_len).rjust(9), str(comment_len).rjust(9)
 
                except KeyboardInterrupt:
                    sys.exit(1)
 
                except Exception:
                    e = sys.exc_info()[1]
                    sys.stderr.write('\n')
                    traceback.print_exc()
                    sys.stderr.write('\n')
                    try:
                        error = CookString(unicode(str(e),'utf-8'))
                    except:
                        error = "Error."
 
                art_index = item_list.index(item)
                artKey = 'art_'+str(art_index)
                lang_info[lang][artKey] = {}
                lang_info[lang][artKey]['item'] = item
                if item in en_labels:
                    lang_info[lang][artKey]['name'] = en_labels[item]
                else:
                    lang_info[lang][artKey]['name'] = item
                lang_info[lang][artKey]['size'] = article_size
                lang_info[lang][artKey]['error'] = error
                lang_info[lang]['art_count'] = lang_info[lang]['art_count'] + 1  
 
 
            temp_file = open(temp_path,'w')
            temp_file.write(str(lang_info[lang]['art_count'])+'\n')
            for index in range(lang_info[lang]['art_count']):
                artKey = 'art_'+str(index)
                temp_file.write(lang_info[lang][artKey]['item'].encode('utf_8')+'\n')
                temp_file.write(lang_info[lang][artKey]['name'].encode('utf_8')+'\n')
                temp_file.write(str(lang_info[lang][artKey]['size'])+'\n')
                temp_file.write(lang_info[lang][artKey]['error'].encode('utf_8')+'\n')
            temp_file.close()
 
        for index in range(lang_info[lang]['art_count']):
            artKey = 'art_'+str(index)
            article_size    = lang_info[lang][artKey]['size']
            wt_article_size = article_size * GetWeightForLang(lang)
            article_type    = GetArticleType(wt_article_size)
            if not lang_info[lang][artKey]['error']:
                lang_info[lang][article_type] = lang_info[lang][article_type] + 1
                lang_info[lang]['total_size'] = lang_info[lang]['total_size'] + article_size
 
    except:
        sys.stderr.write('\n')
        traceback.print_exc()
        sys.stderr.write('\n')


def GetGrowthNumber(lang, score):
    if lang in prev_score:
        return score - prev_score[lang]

def GetGrowth(lang, score):
    if lang in prev_score:
       growth    = "%+2.2f" % round(GetGrowthNumber(lang, score),2)
    else:
       growth    = "n/a"
    if growth == '-0.00':
       growth = '+0.00' 
    return growth


def GetAverageSize(lang, article_count):
    if article_count > 0:
       avg_size = int(round(lang_info[lang]['total_size'] / article_count))
    else:
       avg_size = 0
    return int(avg_size * GetWeightForLang(lang))



def GetMedianSize(lang):
    x = []
    art_count = lang_info[lang]['art_count']  
    for index in range(art_count):
        artKey = 'art_'+str(index)
        size = lang_info[lang][artKey]['size']
        if size > 0:
            x.append(size)
    x.sort()
    mid = len(x)/2

    median_size = 0
    if len(x) > 0:
        if len(x) % 2:
            median_size = x[mid]    
        else:
            median_size = (x[mid-1] + x[mid]) / 2
    return int(median_size * GetWeightForLang(lang))

def PrintResults():

    lang_keys.sort(key=GetScoreForLang, reverse=True)

    print '\n'
    print 'RESULTS\n----------------------------------------------------------------------'
    print u'Lang:',' AvgSize','Median','Absent',' <10k ','10-30k',' >30k ', 'Score', 'Growth'
    for lang in lang_keys:

        absent        = lang_info[lang]['absent']
        stubs         = lang_info[lang]['stubs']
        articles      = lang_info[lang]['articles']
        longarticles  = lang_info[lang]['longarticles']

        article_count = stubs + articles + longarticles
        score         = GetScore(absent, stubs, articles, longarticles)
        growth        = GetGrowth(lang, score)
        avg_size      = GetAverageSize(lang, article_count)
        med_size      = GetMedianSize(lang)

        print lang.ljust(6),
        print str(avg_size).rjust(7),
        print str(med_size).rjust(7),
        print str(absent).rjust(5),
        print str(stubs).rjust(6),
        print str(articles).rjust(6),
        print str(longarticles).rjust(6),
        print ("%6.2f" % score).rjust(6),
        print growth.rjust(6)

def GetWikiTableResults(awards):

    lang_keys.sort(key=GetScoreForLang, reverse=True)

    table = '{|class="wikitable sortable" border="1" cellpadding="2" cellspacing="0" style="width:100%; background: #f9f9f9; border: 1px solid #aaaaaa; border-collapse: collapse; white-space: nowrap; text-align: center"'
    table += '\n|-\n'
    table += u'!width = 45 | № !! width = 55 | Wiki !! width = 220 | Language !! width = 55 | [[Talk:List of Wikipedias by sample of articles/Archives/2007#Proposed weighting of characters for formula_.28Option.232_using_Babel_text.29|Weight]] !! width = 120 | Mean Article<br>Size !! width = 120 | [[Talk:List_of_Wikipedias_by_sample_of_articles#average_or_median.3F|Median Article<br>Size]] !! width = 80 | [[/Absent Articles|Absent]]<br>(0k) !! width=80| Stubs<br>(< 10k)!! width = 80 | Articles<br>(10-30k) !! width = 80 | Long Art.<br>(> 30k) !! width = 80 | [[Talk:List of Wikipedias by sample of articles/Archives/2008#Other possibility of maximum score|Score]]'
    table += '!! width = 50 | [[Talk:List of Wikipedias by sample of articles/Archives/2008#Script_extension|Growth]]'
    table += '\n|-\n'
    i=0
    for lang in lang_keys:
        i += 1

        absent        = lang_info[lang]['absent']
        stubs         = lang_info[lang]['stubs']
        articles      = lang_info[lang]['articles']
        longarticles  = lang_info[lang]['longarticles']

        article_count = stubs + articles + longarticles

        dagger = u'†'
        if absent + article_count == 0:
            lang_footnote = dagger
            absent = lang_info['en']['art_count']
        else:
            lang_footnote = ''

        table += '|' + str(i) + '\n'
        table += '| [[:' + lang + ':|' + lang + ']]' + lang_footnote + '\n'
        table += '| style = "text-align: left" | [[:w:' + lang_info[lang]['name'] + ' language|' + lang_info[lang]['localname'] + ']]\n'

        if 'weight' in lang_info[lang]:
            weight = str(lang_info[lang]['weight'])
        elif 'similar_lang' in lang_info[lang]:
            weight = str(lang_info[lang_info[lang]['similar_lang']]['weight']) + '**' 
        else:
            weight = '1.0*' 

        score         = GetScore(absent, stubs, articles, longarticles)
        growth        = GetGrowth(lang, score)
        avg_size      = GetAverageSize(lang, article_count)
        med_size      = GetMedianSize(lang)


        if HasAwards(awards, lang):
            growth = GetLink('Growth',lang, growth)

        table += '| ' + weight + '\n'
        table += '| ' + GetTableNumber(avg_size,     1,  -1,'Neglected',       lang) + '\n'
        table += '| ' + FormatNumber(med_size) + '\n'
        table += '| ' + GetTableNumber(absent,       1, 250,'Absent Articles', lang) + '\n'
        table += '| ' + GetTableNumber(stubs,        1, 100,'Stubs',           lang, 250) + '\n'
        table += '| ' + GetTableNumber(articles,     1, 100,'Articles',        lang, 250) + '\n'
        table += '| ' + GetTableNumber(longarticles, 1, 100,'Long Articles',   lang) + '\n'

        #color code score
        if score >= 100.00:    
            color = "|style = \"background: "+u'\u0023'+color10000+"\""
        elif score >= 40.00:    
            color = "|style = \"background: "+u'\u0023'+color4000+"\""
        elif score >= 20.00:
            color = "|style = \"background: "+u'\u0023'+color2000+"\""
        elif score >= 10.00:
            color = "|style = \"background: "+u'\u0023'+color1000+"\""
        elif score >= 5.00:
            color = "|style = \"background: "+u'\u0023'+color500+"\""
        elif score >= 2.50:
            color = "|style = \"background: "+u'\u0023'+color250+"\""
        elif score >= 1.00:
            color = "|style = \"background: "+u'\u0023'+color100+"\""
        else:
            color = "|style = \"background: "+u'\u0023'+color0+"\""
            
        table += color + '| ' + ("%.2f" % score) + '\n'
        table += '| ' + growth + '\n'
        table += '|-\n'
        
    table = table[:-2] + '}'
    return table

def GetWikiTableArticles(article_type, min_articles, max_articles_0, max_articles_40=0):
    lang_keys.sort()
    table = u''
    i=0
    for lang in lang_keys:
        i += 1
        count=0

        max_articles = max_articles_0
        score = GetScoreForLang(lang)
        if score > 40 and max_articles_40 > 0:
            max_articles = max_articles_40

        section = u'==='+lang+' [[:w:' + lang_info[lang]['name'] + ' language|' + lang_info[lang]['localname'] + ']]===\n'
        for index in range(lang_info[lang]['art_count']):
            artKey  = 'art_'+str(index)
            artWtSize = GetArticleSize(lang, artKey)
            artType = GetArticleType(artWtSize)
            if artType == article_type:
               section += '#[[d:'+lang_info[lang][artKey]['item']+'|'+lang_info[lang][artKey]['name']+']] '+lang_info[lang][artKey]['error'] + '\n'
               count += 1
        if min_articles <= count <= max_articles:
            table += section

    return table


def GetArticleName(lang, artKey):

    if artKey in lang_info[lang]:
       return lang_info[lang][artKey]['name']
    else:
       return 0


def GetArticleSize(lang, artKey):

    if artKey in lang_info[lang]:
       if lang_info[lang][artKey]['error'] :
          return 0;
       return lang_info[lang][artKey]['size'] * GetWeightForLang(lang)
    else:
       return 0

def GetEdgeFactor(lang, artKey):

    size = GetArticleSize(lang, artKey)
    if size==0:
        return 1
    if 7000 < size < 1000:
        return (size - 7000) / 1000
    if 24000 < size < 30000:
        return (size - 24000) / 1000
    else:
        return 0

def GetRuntFactor(lang, artKey):

    size = GetArticleSize(lang, artKey)

    if size > 0:
        for index in range(lang_info['en']['art_count']):
            otherArtKey =  'art_'+str(index)
            if otherArtKey != artKey:
               otherSize = GetArticleSize(lang, otherArtKey)
               if 0 < otherSize < size:
                   return 0 #you are not the runt
        return 4
    return 0


def GetArticlePoints(lang, artKey):

    size = GetArticleSize(lang, artKey)
    if size > 0 and size < 10000:
       return 1
    elif size > 10000 and size < 30000:
       return 4
    elif size > 30000:
       return 9
    return 0
    

def GetAverageArticlePoints(artKey):
    total = sum(GetArticlePoints(lang, artKey) for lang in lang_keys)
    return float(total) / len(lang_keys)


def GetAverageArticleSize(artKey):
    total = sum(GetArticleSize(lang, artKey) for lang in lang_keys)
    return int(float(total) / len(lang_keys))


def GetNeglectForArticle(lang, artInfo):
    artKey = artInfo['artKey']
    avgPnts = GetAverageArticlePoints(artKey) #0 to 9
    pnts = GetArticlePoints(lang, artKey)     #0 to 9
    edgeFactor = GetEdgeFactor(lang, artKey)  #0 to 6
    runtFactor = GetRuntFactor(lang, artKey)  #0 to 4
    return avgPnts - pnts + edgeFactor + runtFactor

def GetArticlesSortedByNeglect(lang):
    artInfos = []

    if 'art_count' in lang_info['en']:
      for index in range(lang_info['en']['art_count']):
        artKey =  'art_'+str(index)
        artInfos.append( {} )
        artInfos[index]['artKey']  = artKey
        artInfos[index]['popularity']  = GetAverageArticleSize(artKey)
        artInfos[index]['neglect'] = GetNeglectForArticle(lang, artInfos[index])
    artInfos.sort(key=lambda x: (x['neglect'], x['popularity']), reverse=True)
    return artInfos

def GetLargestArticles(artKey, maxLangs):
        
        lang_keys = lang_info.keys()
        lang_keys.sort(key=lambda lang: GetArticleSize(lang, artKey), reverse=True)
        
        item = lang_info['en'][artKey]['item']
        
        ret = []
        for lang in lang_keys[0:maxLangs]:
           ret.append ( '[['+lang+':'+GetArticleInterwikiName(item, lang)+'|'+lang+':'+FormatNumber(GetArticleSize(lang, artKey))+']]' )
        return ' -- '.join(ret)

def GetArticleTypeCount(artKey,points):

        return len([lang for lang in lang_keys if GetArticlePoints(lang, artKey) == points])


def GetNeglectedArticles(lang, max_articles):

    artInfos = GetArticlesSortedByNeglect(lang)

    i=0
    table = u''
    for artInfo in artInfos:

       if artInfo['artKey'] in lang_info[lang]:
        
           item = lang_info[lang][artInfo['artKey']]['item']
           name = lang_info[lang][artInfo['artKey']]['name']
           table += '#[[d:'+item+'|'+name+']]'

           size = int(GetArticleSize(lang, artInfo['artKey']))
           if size > 0:
               iw_name = GetArticleInterwikiName(item, lang)
               if iw_name == '':
                   table += ' ('+str(size) + ')'
               else:
                   iw_link = lang+':'+iw_name
                   table += ' ([['+iw_link+'|'+str(size)+']])'

           table += '\n'
       
       i+=1
       if i >= max_articles: break

    return table


def GetPopularArticles(max_articles):

    artInfos = GetArticlesSortedByNeglect('en')
    artInfos.sort(key=lambda x: x['popularity'], reverse=True)

    i=0

    table = '{|class="wikitable sortable" border="1" cellpadding="2" cellspacing="0" style="width:100%; background: #f9f9f9; border: 1px solid #aaaaaa; border-collapse: collapse; white-space: nowrap; text-align: center"'
    table += '\n|-\n'
    table += u'!width = 45 | № !! width = 90 | Average Size !! width = 150 | Article Name !! width = 80 | [[Talk:List of Wikipedias by sample of articles#Article metric|Absent<br>(0k)]] !! width=80| Stubs<br>(< 10k)!! width = 80 | Articles<br>(10-30k) !! width = 80 | Long Art.<br>(> 30k) !! width = 150 | Largest Articles\n'

    for artInfo in artInfos:
       i+=1
       artKey = artInfo['artKey']
       table += '|-\n'
       table += '|' + str(i)
       table += '||'+FormatNumber(artInfo['popularity']) 
       table += '||style="text-align:left"|[[d:'+lang_info['en'][artKey]['item']+'|'+lang_info['en'][artKey]['name']+']]' 
       table += '||'+str(GetArticleTypeCount(artKey,0))
       table += '||'+str(GetArticleTypeCount(artKey,1))
       table += '||'+str(GetArticleTypeCount(artKey,4))
       table += '||'+str(GetArticleTypeCount(artKey,9))
       table += '||'+GetLargestArticles(artKey,4)+'\n'
       if i >= max_articles > 0: break

    table += '|}\n'

    return table


def GetWikiNeglectedArticles():
    lang_keys.sort()
    table = u''

    print 'writing Popular Articles...'
    table += u'==Popular Articles==\n'
    table += GetPopularArticles(-1)

    print 'writing Neglected Articles...'
    table += u'==Neglected Articles==\n'
    for lang in lang_keys:
        print ' '+lang

        if lang_info[lang]['art_count'] > 0:
            table += u'==='+lang+' [[:w:' + lang_info[lang]['name'] + ' language|' + lang_info[lang]['localname'] + ']]===\n'
            table += GetNeglectedArticles(lang, 10)

        has_errors = False
        section = u'====Errors====\n'
        for index in range(lang_info[lang]['art_count']):
            artKey  = 'art_'+str(index)
            if lang_info[lang][artKey]['error']  :
               section = section + '#[[d:'+lang_info[lang][artKey]['item']+'|'+lang_info['en'][artKey]['name']+']] '+lang_info[lang][artKey]['error'] + '\n'
               has_errors = True
        if has_errors:
            table = table + section
       
    return table



def SaveWikiTableResults(awards):

    print 'writing Results...'
    f = open('results.txt', 'w')
    f.write(GetWikiTableResults(awards).encode("utf_8"))
    f.close()

    print 'writing Absent...'
    f = open('_absent.txt', 'w')
    f.write(GetWikiTableArticles('absent',1, 250).encode("utf_8"))
    f.close()

    print 'writing Stubs...'
    f = open('_stub.txt', 'w')
    f.write(GetWikiTableArticles('stubs',1, 100, 250).encode("utf_8"))
    f.close()

    print 'writing Articles...'
    f = open('_articles.txt', 'w')
    f.write(GetWikiTableArticles('articles',1, 100, 250).encode("utf_8"))
    f.close()

    print 'writing Long Articles...'
    f = open('_longarticles.txt', 'w')
    f.write(GetWikiTableArticles('longarticles',1,100).encode("utf_8"))
    f.close()

    print 'writing Awards...'
    f = open('_growth.txt', 'w')
    f.write(GetWikiAwards(awards).encode("utf_8"))
    f.close()

    print 'writing Suggestions...'
    f = open('_neglectedarticles.txt', 'w')
    f.write(GetWikiNeglectedArticles().encode("utf_8"))
    f.close()
    
    

def CookString(rawString):

    cookString = ''
    for part in rawString.replace("'","||").split("|"):
        if len(part)==0:
            cookString += "'"
        else:
            cookString += eval("u'"+part+"'")
    return cookString        
        
def GetGrowths(article):
    growths = {}
    lang_last  = 0
    lang_first = article.find(u'[[:', lang_last)
    while lang_first > -1:
        lang_last  = article.find(u'|',  lang_first)
        if lang_last == -1:
            break
        lang = article[lang_first+3:lang_last-1]
        score_first = article.find(u'style = "background:',lang_last)
        if score_first == -1:
            break
        score_last  = article.find(u'|', score_first+32)
        if score_last == -1:
            break
        growth_end = article.find(u'\n', score_last)
        growth_str = article[score_last+2:growth_end]
        try:
           growth_pipe = growth_str.find(u'|') 
           if growth_pipe > -1:
              growth_str = growth_str[growth_pipe+1:-2]
           if growth_str.find(u' ‡') > -1:
              growth_str = growth_str[0:-2]
           growth = float(growth_str)
        except:
           growth = 0 
        growths[lang]=growth
        lang_first = article.find(u'[[:', score_last)
    return growths

def GetLastUpdated(article):
    date_first = article.find(u'Last Update')
    if date_first > -1:
       date_last_paren = article.find(u'(', date_first)
       date_last_br = article.find(u'<br/>', date_first)

       if date_last_paren > -1 and date_last_paren < date_last_br :
          date_last = date_last_paren
       else:
          date_last = date_last_br
       if date_last > -1:
           hyphen = article.find(u'-', date_first,date_last)
           if hyphen > -1:
               date_first = hyphen+1
           else:
               date_first += 12

           parts = article[date_first:date_last].strip().split(' ')
           if len(parts[0])==1:
              parts[0] = '0'+parts[0]
           if parts[0][0]==':':
              parts[0] = '0'+parts[0][1]
           parts[1] = parts[1][0:3]
           return ' '.join(parts)

growthsG = {}

def CalculatePlacing(growths,oldid,update):
    global growthsG
    growthsG = growths
    lang_keys = growths.keys()
    lang_keys.sort(key=lambda x: growthsG[x], reverse=True)
    placeNo=0

    print update

    placing = []
    for lang in lang_keys:
        if (placeNo < 3 or growths[lang] > 1) and growths[lang] != None:
           placeNo += 1
           if placeNo==1:
              placestr = '1st Place'
              ribbonimg = 'Article blue.svg'
           elif placeNo==2:
              placestr = '2nd Place'
              ribbonimg = 'Article red.svg'
           elif placeNo==3:
              placestr = '3rd Place'
              ribbonimg = 'Article yellow.svg'
           elif placeNo>3:
              placestr = 'Honorable Mention'
              ribbonimg = 'Article green.svg'

           print " %d  %-3s %+2.2f" % (placeNo, lang, growths[lang])

           place = {'lang':lang,'growth':growths[lang],'oldid':oldid,'update':update,'placestr':placestr,'ribbonimg':ribbonimg}
           placing.append(place) 
    return placing


def GetPreviousAwards():

    article_name = 'List of Wikipedias by sample of articles'

    meta_wiki = pywikibot.Site('meta', 'meta')
    meta_page = pywikibot.Page(meta_wiki, article_name)
    
    awards = {}
    prevUpdate = ''
    prevGrowth = -999

    for oldid,datetime,username,comments in meta_page.getVersionHistory():
        if datetime.year >= 2009 and ("updat" in comments.lower() or 'correct' in comments.lower()) and oldid!=2228213 and oldid!=2264612 and oldid!=3122655 and oldid!=3359817:
            article   = meta_page.getOldVersion(get_redirect=False,oldid=oldid)
            growths = GetGrowths(article)
            if 'en' in growths:
                update = GetLastUpdated(article)
                growth = growths['en']
                if update != prevUpdate and ( prevGrowth != growth or oldid > 3807780 ):
                    prevUpdate = update
                    prevGrowth = growth
                    awards[update] = CalculatePlacing(growths,oldid,update)
    return awards                

def HasAwards(awards, lang):
    
    for placings in awards.values():
        for place in placings:
            if lang == place['lang']:
                return True
    return False

def CompareRows(rowA,rowB):
    if rowA['place']['placestr']==rowB['place']['placestr']:
        return cmp(rowB['place']['growth'],rowA['place']['growth'])
    return cmp(rowA['place']['placestr'],rowB['place']['placestr'])

def GetWikiAwards(awards):
    table = u'==2009-2015 Improvement Awards==\n'
    for lang in lang_keys:
        section = u'==='+lang+' [[:w:' + lang_info[lang]['name'] + ' language|' + lang_info[lang]['localname'] + ']]===\n'
        rows = []
        for update, placings in awards.items():
           for place in placings:
               if lang == place['lang']:
                  mid_section = '|-\n'
                  mid_section += '|width = 150 | [[Image:%s|20px]] %s\n' % (place['ribbonimg'],place['placestr'])
                  if place['oldid'] == -1:  
                      mid_section += '|width = 120 align=center| [[:m:List of Wikipedias by sample of articles|%s]]\n' % (place['update'])
                  else:  
                      mid_section += '|width = 120 align=center| <span class="plainlinks">[http://meta.wikimedia.org/w/index.php?title=List_of_Wikipedias_by_sample_of_articles&oldid=%s %s]</span>\n' % (place['oldid'],place['update'])
                  mid_section += '|width = 80 align=center| %+2.2f\n' % round(place['growth'],2)
                  rows.append({'place':place,'mid_section':mid_section})
        if len(rows) > 0:
            rows.sort(CompareRows)
            if len(rows) > 1:
                section += '{|class="wikitable sortable" cellpadding="6" cellspacing="0"\n'
                section += '! !! !!\n'
            else:
                section += '{|class="wikitable" cellpadding="6" cellspacing="0"\n'
            for row in rows:
                section += row['mid_section']
            section += '|}\n'
            table += section
    return table

def CalculateAwards():

    print "calculating awards..."
    
    todays = {}
    for lang in lang_keys:
        absent        = lang_info[lang]['absent']
        stubs         = lang_info[lang]['stubs']
        articles      = lang_info[lang]['articles']
        longarticles  = lang_info[lang]['longarticles']
        score         = GetScore(absent, stubs, articles, longarticles)
        growth        = GetGrowthNumber(lang, score)
        todays[lang] = growth

    update = strftime("%d %b %Y")
    placing = CalculatePlacing(todays,-1,update)

    awards = GetPreviousAwards()
    awards[update] = placing
    return awards

def SavePreviousScore():
	article_name = 'List of Wikipedias by sample of articles'
 
	meta_wiki = pywikibot.Site('meta', 'meta')
	meta_page = pywikibot.Page(meta_wiki, article_name)
	article   = meta_page.get(get_redirect=False)
 
	f = open('PreviousScores.txt', 'w')
	count = 0
	lang_last  = 0
	lang_first = article.find(u'[[:', lang_last)
	while lang_first > -1:
		lang_last  = article.find(u'|',  lang_first)
 
		lang = article[lang_first+3:lang_last-1]
 
		score_first = article.find(u'style = "background:',lang_last)
		score_last  = article.find(u'|', score_first+32)
 
		score = article[score_first+31:score_last-1]
 
		f.write(lang + ' ' + score + '\n')
 
		count += 1
		print count, lang, score
		lang_first = article.find(u'[[:', score_last)
 
	f.close()

#support dividing up work
if len(sys.argv) == 3:
    part      = int(sys.argv[1])-1
    numparts  = int(sys.argv[2])
    lang_keys = filter(lambda lang: lang_keys.index(lang) % numparts == part, lang_keys)


def main():
    SavePreviousScore()
    GetPreviousScores()
    GetItemList()
    GetIwLinks()
    CalculateStatistics()
    awards = CalculateAwards()
    PrintResults()
    SaveWikiTableResults(awards)
 
if __name__ == '__main__':
    try:
        main()
    finally:
        pywikibot.stopme()