forked from Bob1437/geo-guesser.code--post22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraping.py
111 lines (86 loc) · 3.98 KB
/
scraping.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
import requests
import json
import threading
import time
from json import dumps
API_KEY = "tXLFRVKRTvdA"
PROJECT_TOKEN = "toq7r-OTLXNj"
RUN_TOKEN = "t2ddTYSXTCjs"
class Data:
def __init__(self, api_key, project_token):
self.api_key = api_key
self.project_token = project_token
self.params = {
"api_key": self.api_key
}
self.data = self.get_data()
#getting data from the most recent run...
def get_data(self):
response = requests.get(f'https://www.parsehub.com/api/v2/projects/{self.project_token}/last_ready_run/data', params=self.params)
data = json.loads(response.text)
return data
#stuff for getting total information...."specification" can be total/deaths/recovered
def get_total_cases(self):
data = self.data["total"]
#what it is doing here is looping through all of the data to find an entry called "coronavirus cases"
for content in data:
if content["name"] == "Coronavirus Cases:":
return content['value']
return "0"
def get_total_deaths(self):
data = self.data["total"]
for content in data:
if content["name"] == "Deaths:":
return content['value']
def get_total_recovered(self):
data = self.data['total']
for content in data:
if content['name'] == "Recovered:":
return content['value']
#stuff for getting country specific info
def get_country_data(self, country):
data= self.data["country"]
for content in data:
if content ['name'].lower() == country.lower():
return content
return "0"
def update_data(self):
response = requests.post(f'https://www.parsehub.com/api/v2/projects/{self.project_token}/run', params=self.params)
def poll():
time.sleep(0.1)
old_data = self.data
while True:
new_data = self.get_data()
if new_data != old_data:
self.data = new_data
print("Data updated")
break
time.sleep(5)
t = threading.Thread(target=poll)
t.start()
data = Data(API_KEY, PROJECT_TOKEN)
def worldtotal():
worlddictionary = {"worldtotal": data.get_total_cases(),
"worlddeaths": data.get_total_deaths(),
"worldrecovered":data.get_total_recovered()}
return worlddictionary
def countrydata():
dictionary1={"brazildata": data.get_country_data("brazil")['total_cases'],
"brazildeaths": data.get_country_data("brazil")['total_deaths'],
"brazilrecovered": data.get_country_data("brazil")['total_recovered'],
"icelandtotal": data.get_country_data("iceland")['total_cases'],
"icelanddeaths": data.get_country_data("iceland")['total_deaths'],
"icelandrecovered": data.get_country_data("iceland")['total_recovered'],
"japantotal": data.get_country_data("japan")['total_cases'],
"japandeaths": data.get_country_data("japan")['total_deaths'],
"japanrecovered": data.get_country_data("japan")['total_recovered'],
"denmarktotal": data.get_country_data("denmark")['total_cases'],
"denmarkdeaths": data.get_country_data("denmark")['total_deaths'],
"denmarkrecovered": data.get_country_data("denmark")['total_recovered'],
"nigertotal": data.get_country_data("niger")['total_cases'],
"nigerdeaths": data.get_country_data("niger")['total_deaths'],
"nigerrecovered": data.get_country_data("niger")['total_recovered'],
"indiatotal": data.get_country_data("india")['total_cases'],
"indiadeaths": data.get_country_data("india")['total_deaths'],
"indiarecovered": data.get_country_data("india")['total_recovered'],}
return dictionary1