Cannabis Ruderalis

"""
Copyright (c) 2022 theleekycauldron

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
"""
import pywikibot as pwb
import vandyke
from vandyke import tag,site,months
import datetime

shortened_months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]

def update_summary_tables(year):
    # create new summary tables
    for t in ["Total","Imaged","Non-imaged"]:
        new_page = pwb.Page(site,f"Wikipedia:Did you know/Statistics/Monthly summary statistics{f'/{year}/{t}' if t != 'Total' else ''}")
        new_page.text = f"{{{{Wikipedia:Did you know/Statistics/Monthly summary statistics/Template|{year}|{t.lower()}}}}}"
        new_page.save(summary=f"creating new summary tables for {year} {tag}")
    
    redir_page = pwb.Page(site,f"Wikipedia:Did you know/Statistics/Monthly summary statistics/{year}/Total")
    redir_page.text = "#redirect [[Wikipedia:Did you know/Statistics/Monthly summary statistics]]"
    redir_page.save(summary=f"redirecting to main table {tag}")
    
def update_summary_navigation_page(year):
    summary_navigation_new_text = f"""|-
! {year+1}
| [[Wikipedia:Did you know/Statistics/Monthly summary statistics/{year}/Total|Total]]
| [[Wikipedia:Did you know/Statistics/Monthly summary statistics/{year}/Imaged|Imaged]]
| [[Wikipedia:Did you know/Statistics/Monthly summary statistics/{year}/Non-imaged|Non-imaged]]
"""
    summary_navigation_page = pwb.Page(site,f"Wikipedia:Did you know/Statistics/Monthly summary statistics/Navigation")
    sumnavpagetext = summary_navigation_page.text.splitlines()
    sumnavpagetext = sumnavpagetext[:2] + summary_navigation_new_text.splitlines() + sumnavpagetext[2:]
    summary_navigation_page.text = "\n".join(sumnavpagetext)
    summary_navigation_page.save(summary=f"updating summary navigation for {year} {tag}")
    
def new_year_navigation_page(npt,year):
    npt.insert(2,f"! [[Wikipedia:Did you know/Statistics/Monthly DYK pageview leaders/{year}|{year}]]")
    npt.insert(2,"|-")
    return npt
    
def create_new_nav_redirects(year):
    for m in range(12):
        new_nav_redir = pwb.Page(site,f"Wikipedia:Did you know/Statistics/Monthly DYK pageview leaders/{year}/{months[m]}")
        new_nav_redir.text = "#redirect [[Wikipedia:Did you know/Statistics/Monthly DYK pageview leaders]]"
        new_nav_redir.save(summary=f"populating navigation redirects for {year} {tag}")

def last_month():
    now = datetime.datetime.now()
    year = now.year
    month = now.month-2
    monthsave = month+1
    yearsave = year
    
    navigation_page = pwb.Page(site,"Wikipedia:Did you know/Statistics/Monthly DYK pageview leaders/Navigation")
    navpagetext = navigation_page.text.splitlines()
    
    if (month==-1):
        update_summary_tables(year)
        update_summary_navigation_page(year)
        navpagetext = new_year_navigation_page(navpagetext,year)
        create_new_nav_redirects(year)
        month = 11
        year -= 1
    
    navpagetext.insert(4+monthsave,f"| [[Wikipedia:Did you know/Statistics/Monthly DYK pageview leaders/{yearsave}/{months[monthsave]}|{shortened_months[monthsave]}]]")
    navigation_page.text = "\n".join(navpagetext)
    navigation_page.save(summary=f"updating navigation table {tag}")
    
    archivepagename = f"Wikipedia:Recent additions/{year}/{months[month]}"
    vandyke.main(archivepagename=archivepagename,jitter=True,notify=True)

if __name__ == "__main__":
    last_month()

Leave a Reply