-
Notifications
You must be signed in to change notification settings - Fork 0
/
Country_info.py
133 lines (93 loc) · 3.34 KB
/
Country_info.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
131
132
from countryinfo import CountryInfo
from tkinter import *
import pytz
from datetime import datetime
import geocoder
import requests
from bs4 import BeautifulSoup
"""
Class that contains the clock widget
"""
class Clock:
def __init__(self, parent, code):
# create clock widget by timezone
self.zone = pytz.country_timezones(code)[0]
now = datetime.now(pytz.timezone(self.zone))
self.time = now.strftime('%H:%M:%S')
self.widget = Label(parent, text=self.time)
self.widget.after(200, self.tick) # wait 200 ms, then tick
def tick(self):
# update the display clock
now = datetime.now(pytz.timezone(self.zone))
new_time = now.strftime('%H:%M:%S')
if new_time != self.time:
self.time = new_time
self.widget.config(text=self.time)
self.widget.after(200, self.tick)
"""
Return info about user location
"""
def user_geo(country_name="me"):
if country_name == "me":
code = geocoder.ip(country_name).country
country = CountryInfo(code)
else:
country = CountryInfo(country_name)
code = country.iso(2)
capital = country.capital()
name = country.name().capitalize()
currency = country.currencies()
return [code, name, capital, currency]
"""
Return info of search country
"""
def country_info(country_name):
d = []
country = CountryInfo(country_name)
d.append(["name", country.name().capitalize()])
d.append(["capital", country.capital().capitalize()])
d.append(["region", country.region().capitalize()])
d.append(["currency", country.currencies()])
d.append(["area", country.area()])
d.append(["population", country.population()])
d.append(["languages", country.languages()])
d.append(["borders", country.borders()])
d.append(["calling code", country.calling_codes()])
d.append(["lat/long", country.capital_latlng()])
d.append(["code", country.iso(2)])
return d
"""
Return distance between origin and dest
"""
def get_distance(start, destination):
d = geocoder.distance(start, destination)
return round(d, 2)
"""
Read covid data
"""
page = requests.get('https://www.worldometers.info/coronavirus/#countries')
soup = BeautifulSoup(page.content, 'html.parser')
rows = soup.findChildren("tr")
def covid_data(country, covid_check_names):
country = country.lower()
if country in covid_check_names:
country = covid_check_names[country]
data = []
for i in range(len(rows)):
a = rows[i].findChildren("a")
if len(a) != 0 and a[0].text.lower() == country:
items = rows[i].find_all("td")
for k in range(2, 7):
data.append(items[k].text)
break
return data
# key = "aef51fddd1542d99e3e3dd0a3a48368f"
def capital_weather(name, code, key):
url = f"http://api.openweathermap.org/data/2.5/weather?q={name},{code}&units=metric&appid={key}"
data = requests.get(url).json()
return round(data['main']['temp'], 1), data['weather'][0]['description']
def convert(from_currency, to_currency, amount, key):
url = "https://free.currconv.com/api/v7/convert?q={}_{}&compact=ultra&apiKey={}".format(from_currency, to_currency, key)
data = requests.get(url).json()
amount = round(amount * data[f"{from_currency}_{to_currency}"], 2)
return amount