forked from philipsabri/bostad-stockholm-auto-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
38 lines (32 loc) · 1.6 KB
/
main.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
from src.repositories.apartment_firestore import ApartmentFirestoreRepository
from src.usecase.apartment import ApartmentUseCase
from src.infrastructure.discord import DiscordInfrastructure
from src.repositories.scraper import ScraperRepository
from firebase_admin import firestore, initialize_app, credentials
import os
import json
def main():
scraper_repository = ScraperRepository()
apartment_repository = ApartmentFirestoreRepository(db)
discord_infra = DiscordInfrastructure(INPUT_DISCORD_WEBHOOK_URL)
apartment_usecase = ApartmentUseCase(scraper_repository, apartment_repository, discord_infra)
apartments = apartment_usecase.get_all_apartments(INPUT_APARTMENTS_URL, INPUT_APARTMENTS_FILTER)
for apartment in apartments:
if apartment_usecase.store_apartment(apartment) == True:
apartment_usecase.post_to_discord(apartment)
if __name__ == "__main__":
INPUT_DISCORD_WEBHOOK_URL = os.environ["DISCORD_WEBHOOK_URL"]
INPUT_APARTMENTS_URL = (
os.environ.get("APARTMENTS_URL")
or "https://bostad.stockholm.se/bostad/?sort=annonserad-fran-desc&ungdom=1"
)
INPUT_APARTMENTS_FILTER = os.environ.get("APARTMENTS_FILTER") # Defaults to None if env not set
if INPUT_APARTMENTS_FILTER:
try:
INPUT_APARTMENTS_FILTER = json.loads(INPUT_APARTMENTS_FILTER)
except (TypeError, json.JSONDecodeError):
raise ValueError("APARTMENTS_FILTER must be a valid JSON object")
FIRESTORE_CRED = credentials.Certificate(json.loads(os.environ["FIREBASE_ACCOUNT"]))
initialize_app(FIRESTORE_CRED)
db = firestore.client()
main()