dmCat.py (1724B)
1 # To be used on en.wikinews 2 # Creates date and month categories 3 4 import sys 5 from sys import argv 6 from datetime import datetime 7 import pywikibot as pwb 8 9 # Global declaration 10 daysCount = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 11 months = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] 12 13 # Lambda declaration 14 isLeap = lambda year : (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0) 15 16 # Is the input correct? 17 if(len(argv) != 3): 18 print("Usage: `python pwb.py dmCat.py [Month] [year]") 19 sys.exit(-1) 20 21 month = int(argv[1]) 22 year = int(argv[2]) 23 curYear = datetime.now().year 24 curMonth = datetime.now().month 25 26 # Is year passed 27 if(year < curYear): 28 print("Year is already in the past.") 29 sys.exit(0) 30 31 # Is Month valid 32 if(month in range(1, 13)): 33 if(month < curMonth and year == curYear): 34 print("Month is already in the past.") 35 sys.exit(0) 36 else: 37 print("Invalid month.") 38 sys.exit(0) 39 40 if(isLeap(year)): 41 daysCount[2] = 29 42 43 days = daysCount[month] 44 45 monthName = months[month] 46 47 site = pwb.Site() 48 page = pwb.Page(site, u"Category:{0} {1}".format(monthName, year)) 49 text = page.text 50 51 if(text == ''): 52 page.text = u"{{Monthcategory}}" 53 page.save(u"Create") 54 55 for date in range(1, days + 1): 56 title = "Category:{0} {1}, {2}".format(monthName, date, year) 57 page = pwb.Page(site, title) 58 text = page.text 59 if(text == ''): 60 page.text = u"{{Datecategory}}" 61 page.save(u"Create") 62 63 title = "Wikinews:{0}/{1}/{2}".format(year, monthName, date) 64 page = pwb.Page(site, title) 65 text = page.text 66 if(text == ''): 67 page.text = u"{{{{DateDPL|{0}|{1}|{2}}}}}".format(date, monthName, year) 68 page.save(u"Create") 69 70 print("Done!") 71