-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81c537b
commit ac6eb22
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.3.0 | ||
hooks: | ||
- id: trailing-whitespace | ||
- id: end-of-file-fixer | ||
- id: check-yaml | ||
- id: check-added-large-files | ||
- repo: https://github.com/charliermarsh/ruff-pre-commit | ||
rev: v0.0.262 | ||
hooks: | ||
- id: ruff | ||
args: [--fix] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import sqlite3 | ||
import os | ||
from typing import List, Optional | ||
from contextlib import contextmanager | ||
from typing import Generator | ||
|
||
|
||
DATABASE: str = os.path.join(os.path.dirname(__file__), 'timeapi.db') | ||
|
||
@contextmanager | ||
def get_db_connection() -> Generator[sqlite3.Connection, None, None]: | ||
conn = sqlite3.connect(DATABASE, timeout=30.0) | ||
try: | ||
yield conn | ||
finally: | ||
conn.close() | ||
|
||
def get_all_countries(conn): | ||
countries = conn.execute('SELECT * FROM countries').fetchall() | ||
countries = [x[0] for x in countries] | ||
return countries | ||
|
||
def get_all_cities(conn): | ||
cities = conn.execute('SELECT * FROM cities').fetchall() | ||
cities = [x[0] for x in cities] | ||
return cities | ||
|
||
# def insert_country(country: str) -> Optional[bool]: | ||
# try: | ||
# conn = get_db_connection() | ||
# cursor = conn.cursor() | ||
# country = country.lower().strip().replace(' ', '-') | ||
# cursor.execute('INSERT INTO countries (country) VALUES (?)', (country,)) | ||
# conn.commit() | ||
# conn.close() | ||
# return True | ||
# except sqlite3.Error as e: | ||
# print(f'SQL error = {e}') | ||
# return False | ||
|
||
# def insert_city(city: str) -> Optional[bool]: | ||
# try: | ||
# conn = get_db_connection() | ||
# cursor = conn.cursor() | ||
# city = city.lower().strip().replace(' ', '-') | ||
# cursor.execute('INSERT INTO cities (city) VALUES (?)', (city,)) | ||
# conn.commit() | ||
# conn.close() | ||
# return True | ||
# except sqlite3.Error as e: | ||
# print(f'SQL error = {e}') | ||
# return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import requests, json | ||
from typing import List | ||
from bs4 import BeautifulSoup as bs4 | ||
from time import sleep | ||
from unidecode import unidecode | ||
|
||
continet_list_links = [ | ||
'https://www.timeanddate.com/weather/?continent=namerica', | ||
'https://www.timeanddate.com/weather/?continent=samerica', | ||
'https://www.timeanddate.com/weather/?continent=asia' | ||
'https://www.timeanddate.com/weather/?continent=australasia', | ||
'https://www.timeanddate.com/weather/?continent=europe' | ||
] | ||
|
||
table_css_selector = 'body > div.main-content-div > section.bg--grey.pdflexi-t--small > div > section > div:nth-child(3) > div > table > tbody > tr:nth-child(1)' | ||
names_cities = 'body > main > article > section.pdflexi > div > table > tbody > tr > td > a' | ||
|
||
def get_temperature(country, city): | ||
url = f'https://www.timeanddate.com/weather/{country}/{city}' | ||
response = requests.get(url) | ||
|
||
if response.status_code == 200: | ||
soup = bs4(response.text, 'html.parser') | ||
element = soup.select_one('#qlook > div.h2') | ||
if element: | ||
temperature = element.text.strip() | ||
return temperature | ||
else: | ||
return "Temperature element not found" | ||
else: | ||
return f"Failed to retrieve data, status code: {response.status_code}" | ||
|
||
def update_cities_requests(countries, cities): | ||
seletor = 'body > main > article > section.pdflexi > div > table > *' | ||
|
||
for country in countries: | ||
sleep(0.1) | ||
try: | ||
response = requests.get(f'https://www.timeanddate.com/weather/{country}') | ||
if response.status_code == 200: | ||
soup = bs4(response.text, 'html.parser') | ||
table = soup.select(seletor) | ||
#[cities.append(c.text) for c in row.select('a')] for row in table if c.text not in cities] | ||
for row in table: | ||
city_list = row.select('a') | ||
for c in city_list: | ||
if c.text not in cities: | ||
cities.append(unidecode(c.text)) | ||
else: | ||
print(f'Failed to get data for country {country}, status code: {response.status_code}') | ||
except Exception as e: | ||
print(f'Skipping country {country}, ERRO: {e}') | ||
|
||
if __name__ == '__main__': | ||
json_cities = open('cities.json').read() | ||
cities: List[str] = json.loads(json_cities) | ||
|
||
json_countries = open('countries.json').read() | ||
countries: List[str] = json.loads(json_countries) | ||
|
||
update_cities_requests(countries, cities) | ||
print('finish') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import Union, List | ||
from fastapi import FastAPI, HTTPException, Depends | ||
from handler import get_temperature | ||
from fastapi import FastAPI | ||
from crud import get_all_cities, get_all_countries, get_db_connection | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/all/countries") | ||
async def read_all_countries(): | ||
with get_db_connection() as conn: | ||
countries = get_all_countries(conn) | ||
return countries | ||
|
||
@app.get("/all/cities") | ||
async def read_all_cities(): | ||
with get_db_connection() as conn: | ||
cities = get_all_cities(conn) | ||
return cities | ||
|
||
|
||
@app.get("/{country}/{city}") | ||
async def read_item(country: str, city: str): | ||
# if country not in countries: | ||
# raise HTTPException(status_code=404, detail="Country not located") | ||
# if city not in cities: | ||
# raise HTTPException(status_code=404, detail="City not located") | ||
|
||
temperature = get_temperature(country, city) | ||
return temperature |
Binary file not shown.