-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliveResults.py
131 lines (94 loc) · 4.4 KB
/
liveResults.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# coding: utf-8
# In[470]:
from soup import Soup
import sys, os, git, io, re, datetime, pytz, json
sys.path.append(os.path.join(git.Repo(".", search_parent_directories=True).working_tree_dir, "plotly"))
from plotlyImports import *
from dataset import WriteFile
import pandas as pd
class LiveResults:
def __init__(self, url):
self.url = url
def getResults(self):
return Soup(self.url).getJsonFromResponse()
def getPlotData(self):
soup=Soup(self.url).makeSoup()
plotDat=soup.find_all(name="script", attrs={"type":"text/javascript"})[-1]
plotDat=plotDat.text
plotDat=re.sub("\s*", "", plotDat)
return plotDat
def getSpan(self):
span={
"doughnut" : re.search("DoughnutCharts", self.getPlotData()).span(),
"pie" : re.search("PiCharts", self.getPlotData()).span()
}
return span
def getPlotValues(self, dat):
dat=re.sub(";?var", "", dat)
xValues=re.search("xValues=", dat).span()
yValues=re.search("yValues=", dat).span()
barColors=re.search("barColors=", dat).span()
values = {
"x" : re.findall(r"[A-Za-z\(?)?]+", dat[xValues[1]:yValues[0]]),
"y" : re.findall("\w+", dat[yValues[1]:barColors[0]]),
"colors" : re.findall("\#\w+", dat[barColors[1]:].split(";")[0])
}
return values
def piePlot(self, valuesDict):
fig = go.Figure()
fig.add_trace(go.Pie(values=valuesDict.get("y"),
labels=valuesDict.get("x"),
hole=0.3,
#rotation=90,
#direction="clockwise"
))
fig.update_traces(hoverinfo='label+percent',
textinfo='value',
#textfont_size=20,
marker=dict(colors=valuesDict.get("colors"), line=dict(color='#000000', width=2)))
return fig
def plotValues(self):
span = self.getSpan()
plotDat = self.getPlotData()
donutValues = self.getPlotValues(plotDat[span['doughnut'][1]:span['pie'][0]])
pieValues = self.getPlotValues(plotDat[span['pie'][1]:])
fig = make_subplots(rows=1, cols=2, specs = [[{"type" : "domain"}, {"type" : "domain"}]],
subplot_titles=("Number of Seats", "Percentage Share of Polled Votes"),
vertical_spacing=0,
)
fig.add_trace(go.Pie(self.piePlot(donutValues).data[0]),
row=1, col=1)
fig.add_trace(go.Pie(self.piePlot(pieValues).data[0]),
row=1, col=2)
fig.update_traces(textinfo="percent", row=1, col=2)
fig.update_layout(title=f"<b>Results</b><br><sup>As at: {datetime.datetime.strftime(datetime.datetime.now(tz=pytz.timezone('Asia/Kolkata')), '%B %d %H:%M:%S')}</sup>",
legend={
"orientation" : "h",
#"yanchor" : "bottom",
"x" : 0.095
})
fig.add_annotation(
text=f"""<a href="{self.url}" target="_blank">Source: Election Commission of India</a>""",
showarrow=False,
xref="paper", yref="paper",
x=0.95,
y=-0.05
)
return fig
def getPCWiseResults(self):
soup=Soup(self.url).makeSoup()
dat=[x for x in soup.find_all("div", {"class" : "card-header"}) if "Constituency Wise Results" in x.text][0]
pcValues=[{"pc" : x.text,
"value" : x.attrs.get("value")} for x in dat.find_all("option") if not x.attrs.get("value") == ""]
for pc in pcValues:
label = pc.get('pc').lower().replace(' ', '')
print("="*127)
print("Parliamentary Constituency: ", label)
url = f'{Soup(self.url).getBaseUrl()}/PcResultGenJune2024/Constituencywise{pc.get("value")}.htm'
df=pd.read_html(url)[0]
df.columns=[x.replace(" ", "_").lower() for x in df.columns]
result=json.loads(df.iloc[:,1:].to_json(orient="records"))
WriteFile(basePath="data", dataToWrite=result, fileName=f"tnPE24_{label}", extension="json").writeFileToDisk()
print("="*127)
return None