Skip to content

Commit

Permalink
Refactor code to include send_message function
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-yz committed Feb 9, 2024
1 parent cc3e1c2 commit 485fbd4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
40 changes: 23 additions & 17 deletions assets/simplebot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import os
import requests
import json
import logging
from utils import scrape_webpage, event_exists, store_event, generate_event_hash
from utils import (
scrape_webpage,
event_exists,
store_event,
generate_event_hash,
send_message
)

# Set up logging
logger = logging.getLogger()
Expand All @@ -11,12 +16,6 @@
TOKEN = os.environ.get('BOT_TOKEN')
BASE_URL = f"https://api.telegram.org/bot{TOKEN}"

def send_message(chat_id, text):
url = BASE_URL + "/sendMessage"
data = {"text": text.encode("utf8"),
"chat_id": chat_id,
"parse_mode": "html"}
requests.post(url, data)

def lambda_handler(event, context):
try:
Expand All @@ -36,16 +35,17 @@ def lambda_handler(event, context):
# Respond to the message based on the command
if message == "/start":
response = f"Hello {first_name}"
send_message(chat_id, response, BASE_URL)
logger.info(response)

elif message == "/scrape":
counter = 0
response = scrape_webpage()
for result in response:
event_hash = generate_event_hash(result)
if not result.done and not event_exists(event_hash):
counter += 1
send_message(chat_id, str(result))
send_message(chat_id, str(result), BASE_URL)
store_event(event_hash)
logger.info(f"Stored {result.title}")

Expand All @@ -54,22 +54,28 @@ def lambda_handler(event, context):
elif message == "/scrape all":
response = scrape_webpage()
for result in response:
send_message(chat_id, str(result))
send_message(chat_id, str(result), BASE_URL)
response = f"Sent {len(response)} events!"
send_message(chat_id, response)
send_message(chat_id, response, BASE_URL)
logger.info(response)

elif message == "/info":
response = f"Your Chat ID is {chat_id}"
send_message(chat_id, response)
send_message(chat_id, response, BASE_URL)
logger.info(response)

elif message == "/testHTML":
response = "<b><a href='https://eventro.ir/events/44543'>سی و نهمین جشنواره بین المللی موسیقی فجر تهران بهمن 1402</a></b>" \
"📅 23 بهمن 1402 | 12 February 2024" \
"<a href='https://eventro.ir/events/44543'>🎟️ Buy Tickets</a>" \
"<a href='https://eventro.ir/images/events/logos/44543.jpg'>🖼️ Event Image</a>"
send_message(chat_id, response, BASE_URL)
logger.info(response)

else:
response = "Please use /start or /scrape"
send_message(chat_id, response)
send_message(chat_id, response, BASE_URL)
logger.info(response)


except Exception as e:
print(e)

Expand Down
35 changes: 28 additions & 7 deletions assets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ['EVENTS_TABLE'])


class Event(BaseModel):
title: str
title_link: str
Expand All @@ -22,11 +23,14 @@ class Event(BaseModel):
done: bool

def __str__(self):
return f"<a href='{self.title_link}'>{self.title}</a>\n{self.date_iran}\n{self.date_world}"
return f"<a href='{self.title_link}'>{self.title}</a>\n" \
f"{self.date_iran}\n" \
f"{self.date_world}"


def scrape_webpage():
url = 'https://www.darkoob.ir/tc/Concert/tehran' # Replace with the URL of the web page you want to scrape
# Replace with the URL of the web page you want to scrape
url = 'https://www.darkoob.ir/tc/Concert/tehran'
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
Expand All @@ -37,30 +41,47 @@ def scrape_webpage():
title = event.find('h4', class_='allmode_title').find('a').text.strip()
title_link = event.find('h4', class_='allmode_title').find('a')['href']
image = event.find('div', class_='allmode_img').find('img')['src']
date = event.find('div', class_='allmode_text').find('div', class_='clearb').find('div', class_="event_detail").find('span', 'dt1').text
date = event.find('div', class_='allmode_text') \
.find('div', class_='clearb') \
.find('div', class_="event_detail") \
.find('span', 'dt1').text
date_iran = date.strip().split('\n')[0].strip()
date_world = date.strip().split('\n')[-1].strip()
done = False
for detail in event.find('div', class_='allmode_text').find_all('div', class_='event_detail'):
data = detail.find('span', 'dt1').find('font', attrs={'color': 'red'})
for detail in event.find('div', class_='allmode_text') \
.find_all('div', class_='event_detail'):
data = detail.find('span', 'dt1') \
.find('font', attrs={'color': 'red'})
if data:
if data.text.strip() == "برگزار شده":
done = True

result = Event(title=title, title_link=title_link, image=image, date_iran=date_iran, date_world=date_world, done=done)
result = Event(title=title, title_link=title_link, image=image,
date_iran=date_iran, date_world=date_world, done=done)
results.append(result)

logger.info(results)
return results


def event_exists(event_hash):
response = table.get_item(Key={'event_id': event_hash})
return 'Item' in response


def store_event(event_hash):
response = table.put_item(Item={'event_id': event_hash})
logger.info(response)


def generate_event_hash(event):
unique_string = event.title + event.date_world
return hashlib.md5(unique_string.encode()).hexdigest()


def send_message(chat_id, text, BASE_URL, parse_mode="html"):
url = BASE_URL + "/sendMessage"
data = {"text": text.encode("utf8"),
"chat_id": chat_id,
"parse_mode": parse_mode}
requests.post(url, data)
7 changes: 4 additions & 3 deletions stacks/bot_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
allow_methods=apigateway.Cors.ALL_METHODS,
),
deploy_options=apigateway.StageOptions(
throttling_rate_limit=2, # Requests per second
throttling_burst_limit=5 # Max concurrent requests
throttling_rate_limit=2, # Requests per second
throttling_burst_limit=5 # Max concurrent requests
)
)

Expand Down Expand Up @@ -101,4 +101,5 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
})
}

rule.add_target(targets.LambdaFunction(fastapi_cdk_function, event=events.RuleTargetInput.from_object(default_event)))
rule.add_target(targets.LambdaFunction(fastapi_cdk_function,
event=events.RuleTargetInput.from_object(default_event)))

0 comments on commit 485fbd4

Please sign in to comment.