Jump to content

Wikipedia:Featured articles/By length/Configuration

From Wikipedia, the free encyclopedia

featuredarticlesbylength.py

[edit]
#! /usr/bin/env python
# Public domain; bjweeks, MZMcBride; 2008, 2017

import oursql

import wikitools

import settings

report_title = 'Wikipedia:Featured articles/By length'

report_template = u'''\
Articles in [[:Category:Featured articles]] sorted by page length (in bytes); \
data as of <onlyinclude>~~~~~</onlyinclude>.

{| class="wikitable sortable plainlinks" style="width:100%%; margin:auto;"
|- style="white-space:nowrap;"
! No.
! Article
! Length
|-
%s
|}
'''

wiki = wikitools.Wiki(settings.apiurl)
wiki.login(settings.username, settings.password)

conn = oursql.connect(
    host=settings.host,
    db=settings.dbname,
    read_default_file='~/.my.cnf'
)
cursor = conn.cursor()
cursor.execute('''
/* long-feats.py SLOW_OK */
SELECT
  page_title,
  page_len
FROM categorylinks
JOIN page
ON cl_from = page_id
WHERE cl_to = 'Featured_articles'
AND page_namespace = 0
ORDER BY page_len DESC;
''')

i = 1
output = []
for row in cursor.fetchall():
    page_title = u'[[{0}]]'.format(row[0].decode('utf-8').replace('_', ' '))
    page_len = '{:,.0f}'.format(row[1])
    table_row = u'''\
| %d
| %s
| %s
|-''' % (i, page_title, page_len)
    output.append(table_row)
    i += 1

report = wikitools.Page(wiki, report_title)
report_text = report_template % ('\n'.join(output))
report_text = report_text.encode('utf-8')
report.edit(report_text, summary=settings.editsumm, bot=1)

cursor.close()
conn.close()