Skip to content

Commit

Permalink
added wikiupdater
Browse files Browse the repository at this point in the history
  • Loading branch information
athulvis committed Dec 10, 2023
1 parent fe3de33 commit 739598d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,5 @@ cython_debug/
#.idea/

activate.sh
logs/
pywikibot-Athulvis.lwp
1 change: 1 addition & 0 deletions new_schema.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"date","type":"string","title":{"en":"Date"}},{"name":"covishield","type":"number","title":{"en":"Covishield"}},{"name":"covaxin","type":"number","title":{"en":"Covaxin"}},{"name":"sputnik","type":"number","title":{"en":"Sputnik"}},{"name":"corbevax","type":"number","title":{"en":"Corbevax"}},{"name":"covovax","type":"number","title":{"en":"Covovax"}},{"name":"dose1","type":"number","title":{"en":"Dose 1"}},{"name":"dose2","type":"number","title":{"en":"Dose 2"}},{"name":"precautiondose","type":"number","title":{"en":"Precaution Dose"}}]
1 change: 1 addition & 0 deletions old_schema.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"date","type":"string","title":{"en":"Date"}},{"name":"covishield","type":"number","title":{"en":"Covishield"}},{"name":"covaxin","type":"number","title":{"en":"Covaxin"}},{"name":"sputnik","type":"number","title":{"en":"Sputnik"}},{"name":"dose1","type":"number","title":{"en":"Dose 1"}},{"name":"dose2","type":"number","title":{"en":"Dose 2"}}]
88 changes: 88 additions & 0 deletions wikiupdater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import pywikibot
import pandas as pd
from pathlib import Path
from ast import literal_eval

import time


fpath = Path("vaccine_data").glob('**/*.csv')


files = [x for x in fpath if x.is_file()]

with open("old_schema.txt",'r') as f:
old_schema = f.read()

with open("new_schema.txt",'r') as f:
new_schema = f.read()

def header_fixer(pout):

pmod = pout.split('"data":')[0]+'"data":'
wdata = literal_eval(pout.split('"data":')[1][:-1])
if old_schema in pmod:
updt_pmod = pmod.replace(old_schema, new_schema)
updt = 0
else:
updt_pmod = pout[:-2]
updt = 1

return updt_pmod, updt, wdata

def string_data(file, wdata):


df = pd.read_csv(file)
df2 = df[['date', 'covishield', 'covaxin', 'sputnik', 'corbevax',
'covovax','Total dose 1', 'Total dose 2', 'Total Precuation dose' ]]

if len(wdata[0]) != len(df2.columns):
diff_json = df2.to_json(orient="values")
else:
wdf = pd.DataFrame(wdata, columns=df2.columns)

merge_df = pd.merge(df2,wdf, on=list(df2.columns), how='inner')
df_diff = df2[~df2['date'].isin(merge_df['date'])]
if df_diff.empty:
diff_json = 0
else:
diff_json = df_diff.to_json(orient="values")
return diff_json


def main(district):

site = pywikibot.Site('commons', 'commons')
if district == 'kerala':
page = pywikibot.Page(site, 'Data:COVID-19/Vaccinations/India/Kerala.tab')
else:
page = pywikibot.Page(site, f'Data:COVID-19/Vaccinations/India/Kerala/{district}.tab')



text = page.text

pmod, updt, wdata = header_fixer(text)

covid_json = string_data(files[-1], wdata)

if covid_json !=0 :
if updt == 0:

page.text = pmod + covid_json + "}"
page.save('Replacing data')
elif updt == 1:
pmod += "," + covid_json[1:] + "}"
page.text = pmod
page.save('Replacing data')



if __name__=="__main__":

for dist in files:
district = dist.stem
print(district)
main(district)
time.sleep(5)

0 comments on commit 739598d

Please sign in to comment.