diff --git a/.env b/.env new file mode 100644 index 0000000..47549a8 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +TELEGRAM_CHANNEL_ID=@example +TELEGRAM_TOKEN= \ No newline at end of file diff --git a/alert.py b/alert.py new file mode 100644 index 0000000..94445d9 --- /dev/null +++ b/alert.py @@ -0,0 +1,68 @@ +# Wallamonitor +# 10/02/2021 + +import time +import requests +import json +import telegram +import argparse +from dotenv import load_dotenv +import os +load_dotenv() + +TELEGRAM_CHANNEL_ID = os.getenv("TELEGRAM_CHANNEL_ID") +TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN") +SLEEP_TIME=120 + +def request(product_name, n_articles, latitude, longitude, condition, min_price, max_price): + url = (f"https://api.wallapop.com/api/v3/general/search?keywords={product_name}" + f"&order_by=newest&latitude={latitude}" + f"&longitude={longitude}" + f"&min_sale_price={min_price}" + f"&max_sale_price={max_price}" + f"&filters_source=quick_filters&language=es_ES") + + if condition != "all": + url = url + f"&condition={condition}" # new, as_good_as_new, good, fair, has_given_it_all + + response = requests.get(url) + if response.status_code != 200: + print(f"Wallapop returned status {response.status_code}. Illegal parameters or Wallapop service is down") + exit + + json_data=json.loads(response.text) + return json_data['search_objects'] + +def main(args): + bot = telegram.Bot(token = TELEGRAM_TOKEN) + list = [] + + while True: + articles = request(args.name, 0, args.latitude, args.longitude, args.condition, args.min_price, args.max_price) + for article in articles: + if not article['id'] in list: + bot.send_photo(TELEGRAM_CHANNEL_ID, article['images'][0]['original']) + bot.send_message(TELEGRAM_CHANNEL_ID, f"*Artículo*: {article['title']}\n" + f"*Descripción*: {article['description']}\n" + f"*Precio*: {article['price']} {article['currency']}\n" + f"[Ir al anuncio](https://es.wallapop.com/item/{article['web_slug']})" + , "MARKDOWN") + list.insert(0, article['id']) + time.sleep(5) # Avoid Telegram flood restrictions + time.sleep(SLEEP_TIME) + +def argument_handler(): + parser = argparse.ArgumentParser(description='Arguments') + parser.add_argument('--name', dest='name', type=str, required=True, help='Article name') + parser.add_argument('--latitude', dest='latitude', type=str, default='40.4165', help='Latitude') + parser.add_argument('--longitude', dest='longitude', type=str, default='-3.70256', help='Longitude') + parser.add_argument('--condition', dest='condition', type=str, default='all', help='Item condition: all, new, as_good_as_new, good, fair, has_given_it_all') + parser.add_argument('--min', dest='min_price', type=str, default=0, help='Min price') + parser.add_argument('--max', dest='max_price', type=str, default=10000000, help='Max price') + + args = parser.parse_args() + print(f"Wallapop monitor running. Checking for new items containing: \'{args.name}\' with given parameters periodically") + return args + +args = argument_handler() +main(args) \ No newline at end of file