Skip to content

Commit 3e96f7d

Browse files
authored
Merge 10e5b04 into 66c9fdf
2 parents 66c9fdf + 10e5b04 commit 3e96f7d

File tree

10 files changed

+2302
-11
lines changed

10 files changed

+2302
-11
lines changed

app/database/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,15 @@ class InternationalDays(Base):
573573
international_day = Column(String, nullable=False)
574574

575575

576+
class Location(Base):
577+
__tablename__ = "locations"
578+
579+
id = Column(Integer, primary_key=True, index=True)
580+
country = Column(String, nullable=False)
581+
city = Column(String, nullable=False)
582+
zip_number = Column(String, nullable=False)
583+
584+
576585
# insert language data
577586

578587

app/internal/json_data_loader.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
from loguru import logger
66
from sqlalchemy.orm import Session
77

8-
from app.database.models import Base, InternationalDays, Joke, Quote, Zodiac
9-
from app.internal import daily_quotes, international_days, jokes, zodiac
8+
from app.config import RESOURCES_DIR
9+
from app.database.models import (
10+
Base, InternationalDays, Joke, Quote, Location, Zodiac
11+
)
12+
from app.internal import (
13+
daily_quotes, international_days, jokes, locations, zodiac
14+
)
1015

1116

1217
def load_to_database(session: Session) -> None:
@@ -23,28 +28,35 @@ def load_to_database(session: Session) -> None:
2328
"""
2429
_insert_into_database(
2530
session,
26-
'app/resources/zodiac.json',
31+
RESOURCES_DIR / "zodiac.json",
2732
Zodiac,
2833
zodiac.get_zodiac,
2934
)
3035

3136
_insert_into_database(
3237
session,
33-
'app/resources/quotes.json',
38+
RESOURCES_DIR / "quotes.json",
3439
Quote,
3540
daily_quotes.get_quote,
3641
)
3742

3843
_insert_into_database(
3944
session,
40-
'app/resources/international_days.json',
45+
RESOURCES_DIR / "international_days.json",
4146
InternationalDays,
4247
international_days.get_international_day,
4348
)
4449

4550
_insert_into_database(
4651
session,
47-
'app/resources/jokes.json',
52+
RESOURCES_DIR / "locations.json",
53+
Location,
54+
locations.create_location_object,
55+
)
56+
57+
_insert_into_database(
58+
session,
59+
RESOURCES_DIR / "jokes.json",
4860
Joke,
4961
jokes.get_joke,
5062
)

app/internal/locations.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from typing import Any, Dict, Optional
2+
3+
import requests
4+
from sqlalchemy.orm import Session
5+
6+
from app.database.models import Location
7+
8+
9+
def create_location_object(location_: Dict[str, str]) -> Location:
10+
"""Returns a Location object from the dictionary data.
11+
12+
Args:
13+
location_: A dictionary location related information.
14+
15+
Returns:
16+
A new Location object.
17+
"""
18+
return Location(
19+
country=location_["country"],
20+
city=location_["city"],
21+
zip_number=location_["zip_number"],
22+
)
23+
24+
25+
def return_zip_to_location(session: Session) -> Optional[str]:
26+
"""Returns the zip number of the user IP location that match location
27+
object.
28+
29+
Args:
30+
session: The database connection.
31+
32+
Returns:
33+
A zip number for the user location.
34+
"""
35+
response = requests.get("http://ipinfo.io/json")
36+
if not response.ok:
37+
return None
38+
location_by_ip = response.json()
39+
for location in session.query(Location).all():
40+
if (location.city == location_by_ip["city"]
41+
and location.country == location_by_ip["country"]):
42+
return location.zip_number
43+
44+
45+
def get_user_location(session: Session) -> Optional[Dict[str, Any]]:
46+
"""Returns the user location.
47+
48+
Args:
49+
session: The database connection.
50+
51+
Returns:
52+
A user location.
53+
"""
54+
my_location = return_zip_to_location(session)
55+
response = requests.get(
56+
f"https://www.hebcal.com/shabbat?cfg=json&geonameid={my_location}"
57+
)
58+
if response.ok:
59+
return response.json()

app/internal/shabbat.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from datetime import date, datetime
2+
from typing import Any, Dict, Optional, Union
3+
4+
5+
def get_shabbat_if_date_friday(
6+
shabbat_time: Dict[str, Any],
7+
calendar_date: date,
8+
) -> Optional[Dict[str, Union[str, date]]]:
9+
"""Returns shabbat start end ending time if specific date
10+
is Saturday, else None.
11+
12+
Args:
13+
shabbat_time: Shabbat details.
14+
calendar_date: date.
15+
16+
Returns:
17+
Shabbat start end ending time if specific date
18+
is Saturday, else None
19+
"""
20+
shabbat_obj = shabbat_time_by_user_location(shabbat_time)
21+
if calendar_date == shabbat_obj["start_date"]:
22+
return shabbat_obj
23+
24+
25+
def shabbat_time_by_user_location(
26+
shabbat_time: Dict[str, Any]
27+
) -> Dict[str, Union[str, date]]:
28+
"""Returns the shabbat time of the user location..
29+
30+
Args:
31+
shabbat_time: Shabbat details.
32+
33+
Returns:
34+
Shabbat start end ending time.
35+
"""
36+
shabbat_entry = shabbat_time["items"][5]
37+
shabbat_exit = shabbat_time["items"][7]
38+
shabbat_entry_date = shabbat_entry["date"].split("T")[0]
39+
shabbat_exit_date = shabbat_entry["date"].split("T")[0]
40+
shabbat_limit = {
41+
"start_hour": shabbat_entry["title"].split(": ")[1],
42+
"start_date": datetime.strptime(shabbat_entry_date, "%Y-%m-%d").date(),
43+
"end_hour": shabbat_exit["title"].split(": ")[1],
44+
"end_date": datetime.strptime(shabbat_exit_date, "%Y-%m-%d").date(),
45+
}
46+
return shabbat_limit

0 commit comments

Comments
 (0)