-
Notifications
You must be signed in to change notification settings - Fork 1
/
election_server.py
58 lines (48 loc) · 2.5 KB
/
election_server.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
# election.py
# License is BSD-1 clause ; Don't relicense my code, that's all I care about
import requests
from bs4 import BeautifulSoup
import os, time, json, sys
from collections import namedtuple
Candidate = namedtuple("Candidate", ('name', 'electoral', 'percentage', 'count'))
result = "waiting for update"
while True:
try:
url = "https://www.google.com/search?q=us+election+results&rlz=1C1SQJL_enUS909US911&aqs=chrome.0.0i67i131i433i457j0i20i131i263i433j0i131i433j0i67j0i131i433l2j69i60l2.1612j0j9&sourceid=chrome&ie=UTF-8"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')
blox = soup.body.select('#main #search div table td span')
candidatefirst = 'Joe Biden' if 'iden' in soup.body.select('th div a div div')[1].get_text() else 'Donald Trump'
candidatesecond = 'Joe Biden' if candidatefirst != 'Joe Biden' else 'Donald Trump'
first = Candidate(candidatefirst, blox[0].get_text(), blox[1].get_text(), blox[2].get_text())
second = Candidate(candidatesecond, blox[3].get_text(), blox[4].get_text(), blox[5].get_text())
if sys.stdout.isatty() and 'noterm' not in sys.argv:
print(first)
print(second)
else:
fun = [
{
'candidate':first.name,
'electoral':first.electoral,
'percentage':first.percentage,
'count':first.count
},
{
'candidate':second.name,
'electoral':second.electoral,
'percentage':second.percentage,
'count':second.count
}
]
print(json.dumps(fun))
with open("/usr/share/nginx/html/results.json", "w") as output:
json.dump(fun, output)
with open("/usr/share/nginx/html/index.html", "w") as output:
json.dump(fun, output)
if requests.get("https://raw.githubusercontent.com/KritantaDev/election/main/update1").status_code == 200:
if requests.get("https://raw.githubusercontent.com/KritantaDev/election/main/update1").text == 'update':
print("UPDATE AVAILABLE - REDOWNLOAD FROM https://github.com/kritantadev/election OR RUN git pull")
time.sleep(10)
except IndexError:
pass