Compare commits

..
11 Commits
Author SHA1 Message Date
abdus 2db373f54a chore: Upgrade python 2023-03-31 16:42:07 +02:00
abdus b7bf1d8c98 fix: Ignore new Hijri date column on the page 2023-03-31 16:41:16 +02:00
abdus 5b4c818451 fix: Add a fake user agent 2022-02-27 15:57:37 +03:00
abdus f31f0def29 fix: Replace loguru for stdlib logger 2022-02-27 15:56:04 +03:00
abdus 36f6932bb7 fix: Increase cache TTL 2022-02-27 15:55:53 +03:00
abdus b631b10ca6 fix: Remove proxy 2022-02-27 15:55:39 +03:00
abdus 71f6298708 fix: Add a temp proxy 2022-02-27 15:51:22 +03:00
abdus 15b76871cf fix: Bump python version 2022-02-27 15:45:39 +03:00
abdus 22fb2c9bc9 fix: Add in-memory cache 2022-02-27 15:43:34 +03:00
abdus 958fe4996c fix: Increase timeouts 2022-02-27 15:43:24 +03:00
abdus d29edccb21 fix: Update packages 2022-02-27 15:43:11 +03:00
5 changed files with 41 additions and 23 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
FROM python:3.7-slim FROM python:3.11
WORKDIR /app WORKDIR /app
COPY ./install.sh ./requirements.txt ./ COPY ./install.sh ./requirements.txt ./
+4 -1
View File
@@ -1,9 +1,12 @@
import logging
from fastapi import FastAPI from fastapi import FastAPI
from loguru import logger
from starlette.requests import Request from starlette.requests import Request
from app.api import api as api_router from app.api import api as api_router
logger = logging.getLogger(__name__)
app = FastAPI(title='prayertimes', app = FastAPI(title='prayertimes',
version='1.0.0', version='1.0.0',
description='An API for fetching islamic prayer times from Presidency of Religious Affairs of Turkey') description='An API for fetching islamic prayer times from Presidency of Religious Affairs of Turkey')
+28 -14
View File
@@ -3,10 +3,17 @@ from datetime import datetime
from typing import List from typing import List
import httpx import httpx
from aiocache import cached, Cache
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from pydantic import BaseModel from pydantic import BaseModel
_http = httpx.AsyncClient() _http = httpx.AsyncClient(
timeout=20,
follow_redirects=True,
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0'
}
)
class PrayerTimes(BaseModel): class PrayerTimes(BaseModel):
@@ -19,22 +26,30 @@ class PrayerTimes(BaseModel):
isha: str isha: str
@cached(ttl=3 * 60 * 60, cache=Cache.MEMORY)
async def parse_prayer_times(url: str) -> List[PrayerTimes]: async def parse_prayer_times(url: str) -> List[PrayerTimes]:
res = await _http.get(url) res = await _http.get(url)
res.raise_for_status()
soup = BeautifulSoup(res.text, 'html.parser') soup = BeautifulSoup(res.text, 'html.parser')
items = [] items = []
for row in soup.select('#tab-1 .vakit-table tbody tr'): for row in soup.select('#tab-1 .vakit-table tbody tr'):
cell_texts = (c.text.strip() for c in row.select('td')) cells = [c.text.strip() for c in row.select('td')]
headers = PrayerTimes.__fields__.keys() items.append(PrayerTimes(
parsed = dict(zip(headers, cell_texts)) date=datetime.strptime(cells[0], '%d.%m.%Y'),
parsed['date'] = datetime.strptime(parsed['date'], '%d.%m.%Y') fajr=cells[2],
items.append(PrayerTimes(**parsed)) sun=cells[3],
dhuhr=cells[4],
asr=cells[5],
maghrib=cells[6],
isha=cells[7],
))
return items return items
def make_location_url(location_id: int) -> str: def make_location_url(location_id: int) -> str:
return f'https://namazvakitleri.diyanet.gov.tr/en-US/{location_id}' url = f'https://namazvakitleri.diyanet.gov.tr/en-US/{location_id}'
return url
async def get_prayer_times(location_id: int) -> List[PrayerTimes]: async def get_prayer_times(location_id: int) -> List[PrayerTimes]:
@@ -117,16 +132,15 @@ async def _get_countries() -> dict:
if __name__ == '__main__': if __name__ == '__main__':
import asyncio import asyncio
import core.diyanetdb
async def main(): async def main():
with core.diyanetdb.get_connection() as conn: # with core.diyanetdb.get_connection() as conn:
id = core.diyanetdb.get_location_id(conn, # id = core.diyanetdb.get_location_id(conn,
country_name='AVUSTURYA', # country_name='AVUSTURYA',
city_name='PESSENDELLACH') # city_name='PESSENDELLACH')
url = make_location_url(id) # url = make_location_url(id)
times = await parse_prayer_times(url) times = await parse_prayer_times('https://namazvakitleri.diyanet.gov.tr/en-US')
print(times) print(times)
+1 -1
View File
@@ -7,7 +7,7 @@ from pydantic import BaseModel
from app import config from app import config
_http = httpx.AsyncClient() _http = httpx.AsyncClient(timeout=20, follow_redirects=True)
class Location(BaseModel): class Location(BaseModel):
+6 -5
View File
@@ -1,5 +1,6 @@
bs4==0.0.1 beautifulsoup4==4.10.0
fastapi==0.47.1 fastapi==0.74.1
httpx==0.11.1 httpx==0.23.3
loguru==0.4.1 uvicorn==0.17.5
uvicorn==0.11.1 aiocache==0.11.1
pydantic==1.10.7