forked from commandercool/funda-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
60 lines (51 loc) · 1.59 KB
/
scraper.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
from funda import Funda
from telegram_bot import TelegramBot
import os
import sys
import re
from openai import OpenAI
MESSAGE_TEMPLATE = """
*%s*
[check out on funda](%s)
"""
ENV_FUNDA_DB = "FUNDA_DB"
ENV_TELEGRAM_TOKEN = "TELEGRAM_TOKEN"
ENV_CHAT_ID = "CHAT_ID"
ENV_SEARCH_URL = "SEARCH_URL"
ENV_OPENAI_API_KEY = "OPENAI_API_KEY"
if (
ENV_FUNDA_DB not in os.environ
or ENV_TELEGRAM_TOKEN not in os.environ
or ENV_CHAT_ID not in os.environ
or ENV_OPENAI_API_KEY not in os.environ
):
print(
f"ERROR: The following environment variables must be set: {ENV_FUNDA_DB}, {ENV_TELEGRAM_TOKEN}, {ENV_CHAT_ID}, {ENV_SEARCH_URL} and ENV_OPENAI_API_KEY",
file=sys.stderr,
)
exit(1)
sqlite_location = os.environ[ENV_FUNDA_DB]
telegram_token = os.environ[ENV_TELEGRAM_TOKEN]
chat_id = os.environ[ENV_CHAT_ID]
search_url = os.environ[ENV_SEARCH_URL]
openai_api_key = os.environ[ENV_OPENAI_API_KEY]
openai_client = OpenAI(api_key=openai_api_key)
funda = Funda(sqlite_location, openai_client, search_url)
telegramBot = TelegramBot(chat_id, telegram_token)
newListing = funda.fetchNew()
for listing in newListing:
print("scraper: sending message over telegram")
summary = (
listing.summary.replace("**", "*")
.replace(".", "\\.")
.replace(">", "\\>")
.replace("<", "\\<")
.replace("_", "\\_")
.replace("-", "\\-")
.replace("(", "\\(")
.replace(")", "\\)")
.replace("k\\.k\\.", "")
.replace("!", "\\!")
)
message = MESSAGE_TEMPLATE % (summary, listing.link)
telegramBot.postMessage(message)