User:DschwenBot/daycategories.py

#!/usr/bin/python

import sys, os
print(os.environ['HOME'])

sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), 'pywikibot-core'))
import pywikibot
import datetime

site = pywikibot.Site()

# define month names to be locale independent
month = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
# list of categories to be created and their content templates
cats = [
  { 'page': "CfD {year}-{monthnum}", 'daily': False,
    'template': "[[Category:Categories for discussion by month]]" },
  { 'page': "Categories for discussion/{year}/{monthnum}", 'daily': False,
    'template': "<includeonly>== {month} {year} ==</includeonly>\n<noinclude>{{CFD month header}}</noinclude>" },
  { 'page': "Categories for discussion/Archive/{year}/{monthnum}", 'daily': False,
    'template': "<noinclude>{{Categories for discussion|{year}|{monthnum}}}</noinclude>" },
  { 'page': "Permission pending as of {day} {month} {year}", 'daily': True,
    'template': "{{{{Permission pending header|day={day}|month={month}|year={year}}}}}" },
  { 'page': "Permission received as of {day} {month} {year}", 'daily': True,
    'template': "{{{{Permission received header|day={day}|month={month}|year={year}}}}}" },
  { 'page': "Deletion requests {month} {year}", 'daily': False,
    'template': "__HIDDENCAT__\n[[Category:Deletion requests|{year}{monthnum}]]\n{{{{CategoryTOC|numerals=days|letters=no}}}}" },
  { 'page': "Commons users indefinitely blocked in {month} {year}", 'daily': False,
    'template': "__NOINDEX__\n\n[[Category:Commons users indefinitely blocked|{year}-{monthnum}]]" },
  { 'page': "Media needing categories as of {day} {month} {year}", 'daily': True,
    'template': "{{{{UncategorizedHeader|day={day}|month={month}|year={year}}}}}" },
  { 'page': "Media needing category review as of {day} {month} {year}", 'daily': True,
    'template': "{{{{CheckcategoriesHeader|day={day}|month={month}|year={year}}}}}" },
  { 'page': "Media without a license as of {day} {month} {year}", 'daily': True,
    'template': "{{{{Delete cat when empty|1={year}{monthnum}{daynum}}}}}\n{{{{UnknownHeader}}}}\n\n[[Category:Media without a license| {year}{monthnum}{daynum}]]" },
  { 'page': "Media without a source as of {day} {month} {year}", 'daily': True,
    'template': "{{{{Delete cat when empty|1={year}{monthnum}{daynum}}}}}\n{{{{UnknownHeader}}}}\n\n[[Category:Media without a source| {year}{monthnum}{daynum}]]" },
  { 'page': "Media missing permission as of {day} {month} {year}", 'daily': True,
    'template': "{{{{Delete cat when empty|1={year}{monthnum}{daynum}}}}}\n{{{{UnknownHeader}}}}\n\n[[Category:Media missing permission| {year}{monthnum}{daynum}]]" },
  { 'page': "Files moved to Commons requiring review as of {day} {month} {year}", 'daily': True,
    'template': "{{{{BotMoveToCommonsHeader|day={day}|month={month}|year={year}}}}}" },
  { 'page': "Files moved from en.wikipedia to Commons requiring review as of {day} {month} {year}", 'daily': True,
    'template': "{{{{BotMoveToCommonsHeader|lang=en|project=wikipedia|day={day}|month={month}|year={year}}}}}" },
  { 'page': "Files moved from it.wikipedia to Commons requiring review as of {day} {month} {year}", 'daily': True,
    'template': "{{{{BotMoveToCommonsHeader|lang=it|project=wikipedia|day={day}|month={month}|year={year}}}}}" },
  { 'page': "Files moved from de.wikipedia to Commons requiring review as of {day} {month} {year}", 'daily': True,
    'template': "{{{{BotMoveToCommonsHeader|lang=de|project=wikipedia|day={day}|month={month}|year={year}}}}}" }
]

today = datetime.datetime.today()

day = today.day
month = month[today.month-1]
year = today.year
monthnum = "%02d" % today.month
daynum = "%02d" % today.day

for cat in cats :
  if not cat['daily'] and day != 1 :
    continue

  catname = cat['page'].format(day=day,month=month,year=year,monthnum=monthnum,daynum=daynum)
  text = cat['template'].format(day=day,month=month,year=year,monthnum=monthnum,daynum=daynum)

  page = pywikibot.Page(site, 'Category:' + catname )

  if not page.exists() :
    print("creating [[Category:%s]]." % catname)
    print(text)
    page.put(text, summary = "Creating daily/monthly maintenance categories.")