Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Ocaña authored and Fernando Ocaña committed Jun 17, 2021
2 parents 311ecea + 8644488 commit c798975
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__pycache__
.idea/
venv/
db.sqlite
*.log
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# syntax=docker/dockerfile:1

FROM python:3.8-slim-buster

RUN apt-get update && \
apt-get install -y locales && \
sed -i -e 's/# es_ES.UTF-8 UTF-8/es_ES.UTF-8 UTF-8/' /etc/locale.gen && \
dpkg-reconfigure --frontend=noninteractive locales

ENV LANG es_ES.UTF-8
ENV LC_ALL es_ES.UTF-8
ENV TZ Europe/Madrid

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY ssbo.py .
COPY dbhelper.py .

RUN mkdir /data
RUN mkdir /logs

CMD [ "python3", "./ssbo.py"]
6 changes: 6 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- [x] Definir dependencias
- [x] Probar dependencias
- [x] Externalizar TOKEN
- [x] Preparar directorio /data
- [x] Logging /log
- [x] Docker
9 changes: 5 additions & 4 deletions dbhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, item_id, chat_id, title, price, url, publish_date, observacio


class DBHelper:
def __init__(self, dbname="db.sqlite"):
def __init__(self, dbname="/data/db.sqlite"):
self.dbname = dbname
self.conn = sqlite3.connect(dbname, check_same_thread=False)

Expand All @@ -54,6 +54,7 @@ def setup(self):
"title text, " \
"price text, " \
"url text, " \
"user text, " \
"publishDate integer, " \
"observaciones text, " \
"item text, " \
Expand Down Expand Up @@ -129,10 +130,10 @@ def add_search(self, chat_search):
except sqlite3.IntegrityError as e:
print(e)

def add_item(self, item_id, chat_id, title, price, url, publish_date, observaciones=None, user=None):
stmt = "insert into item (itemId, chatId, title, price, url, publishDate, observaciones, user) " \
def add_item(self, item_id, chat_id, title, price, url, user, publish_date=None, observaciones=None):
stmt = "insert into item (itemId, chatId, title, price, url, user, publishDate, observaciones) " \
"values (?, ?, ?, ?, ?, ?, ?, ?)"
args = (item_id, chat_id, title, price, url, publish_date, observaciones, user)
args = (item_id, chat_id, title, price, url, user, publish_date, observaciones)
try:
self.conn.execute(stmt, args)
self.conn.commit()
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests==2.25.1
PyTelegramBotAPI==3.7.9
2 changes: 2 additions & 0 deletions sqlite3_instructions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.schema
.exit
105 changes: 63 additions & 42 deletions ssbo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@
from re import sub
from decimal import Decimal
import logging
from logging.handlers import RotatingFileHandler
import sys
import threading
import os
import locale

TOKEN = "SUSTITUIR-POR-EL-TOKEN-DEL-BOT-DE-TELEGRAM"
TOKEN = os.getenv("BOT_TOKEN", "Bot Token does not exist")
URL = "https://api.telegram.org/bot{}/".format(TOKEN)
URL_ITEMS = "https://es.wallapop.com/rest/items"
URL_ITEMS = "https://api.wallapop.com/api/v3/general/search"
PROFILE = os.getenv("PROFILE")

if PROFILE is None:
db = DBHelper()
else:
db = DBHelper("db.sqlite")

db = DBHelper()

ICON_VIDEO_GAMES = u'\U0001F3AE' # 🎮
ICON_WARNING____ = u'\U000026A0' # ⚠️
Expand All @@ -35,29 +43,29 @@ def notel(chat_id, price, title, url_item, obs=None):
text += '\n'
if obs is not None:
text += ICON_COLLISION__ + ' '
text += price
text += locale.currency(price, grouping=True)
if obs is not None:
text += obs
text += ' ' + ICON_COLLISION__
text += '\n'
text += 'https://es.wallapop.com/item/'
text += url_item
urlz0rb0t = URL+"sendMessage?chat_id=%s&parse_mode=markdown&text=%s" % (chat_id, text)
urlz0rb0t = URL + "sendMessage?chat_id=%s&parse_mode=markdown&text=%s" % (chat_id, text)
requests.get(url=urlz0rb0t)


def get_url_list(search):
url = URL_ITEMS
url += '?kws='
url += '?keywords='
url += "+".join(search.kws.split(" "))
if search.cat_ids is not None:
url += '&catIds='
url += '&category_ids='
url += search.cat_ids
if search.min_price is not None:
url += '&minPrice='
url += '&min_sale_price='
url += search.min_price
if search.max_price is not None:
url += '&maxPrice='
url += '&max_sale_price='
url += search.max_price
if search.dist is not None:
url += '&dist='
Expand All @@ -75,32 +83,33 @@ def get_items(url, chat_id):
try:
resp = requests.get(url=url)
data = resp.json()
for x in data['items']:
# print(data)
for x in data['search_objects']:
# print('\t'.join((datetime.datetime.today().strftime('%Y-%m-%d %H:%M'),
# str(x['itemId']), x['price'], x['title'])))
i = db.search_item(x['itemId'], chat_id)
# str(x['id']), str(x['price']), x['title'], x['user']['id'])))
# logging.info('\t'.join((str(x['id']), str(x['price']), x['title'], x['user']['id'])))
logging.info('Encontrado: id=%s, price=%s, title=%s, user=%s',str(x['id']), locale.currency(x['price'], grouping=True), x['title'], x['user']['id'])
i = db.search_item(x['id'], chat_id)
if i is None:
db.add_item(x['itemId'], chat_id, x['title'], x['price'], x['url'], x['publishDate'])
notel(chat_id, x['price'], x['title'], x['url'])
print('\t'.join((datetime.datetime.today().strftime('%Y-%m-%d %H:%M'),
'NEW ', str(x['itemId']), x['price'], x['title'])))
db.add_item(x['id'], chat_id, x['title'], x['price'], x['web_slug'], x['user']['id'])
notel(chat_id, x['price'], x['title'], x['web_slug'])
logging.info('New: id=%s, price=%s, title=%s', str(x['id']), locale.currency(x['price'], grouping=True), x['title'])
else:
# Si está comparar precio...
money = x['price']
money = str(x['price'])
value_json = Decimal(sub(r'[^\d.]', '', money))
value_db = Decimal(sub(r'[^\d.]', '', i.price))
if value_json < value_db:
new_obs = i.price
if i.observaciones is not None:
new_obs += ' < '
new_obs += i.observaciones
db.update_item(x['itemId'], money, new_obs)
db.update_item(x['id'], money, new_obs)
obs = ' < ' + new_obs
notel(chat_id, x['price'], x['title'], x['url'], obs)
print('\t'.join((datetime.datetime.today().strftime('%Y-%m-%d %H:%M'),
'BAJA', str(x['itemId']), x['price'], x['title'])))
notel(chat_id, x['price'], x['title'], x['web_slug'], obs)
logging.info('Baja: id=%s, price=%s, title=%s', str(x['id']), locale.currency(x['price'], grouping=True), x['title'])
except Exception as e:
print(e)
logging.error(e)


# INI Actualización de db a partir de la librería de Telegram
Expand Down Expand Up @@ -140,8 +149,8 @@ def get_searchs(message):
text += chat_search.kws
text += '|'
if chat_search.min_price is not None:
text += chat_search.min_price
text += '-'
text += chat_search.min_price
text += '-'
if chat_search.max_price is not None:
text += chat_search.max_price
if chat_search.cat_ids is not None:
Expand Down Expand Up @@ -177,7 +186,7 @@ def add_search(message):
cs.username = message.from_user.username
cs.name = message.from_user.first_name
cs.active = 1
print(cs)
logging.info('%s', cs)
db.add_search(cs)


Expand All @@ -186,13 +195,25 @@ def add_search(message):
# print('echo: "' + message.text + '"')
# bot.reply_to(message, message.text)

logger = telebot.logger
formatter = logging.Formatter('[%(asctime)s] %(thread)d {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
'%m-%d %H:%M:%S')
ch = logging.StreamHandler(sys.stdout)
logger.addHandler(ch)
logger.setLevel(logging.ERROR) # or use logging.INFO
ch.setFormatter(formatter)
pathlog = 'wallbot.log'
if PROFILE is None:
pathlog = '/logs/' + pathlog

logging.basicConfig(
handlers=[RotatingFileHandler(pathlog, maxBytes=1000000, backupCount=10)],
# filename='wallbot.log',
level=logging.INFO,
format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S')

locale.setlocale(locale.LC_ALL, 'es_ES.UTF-8')

#logger = telebot.logger
#formatter = logging.Formatter('[%(asctime)s] %(thread)d {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
# '%m-%d %H:%M:%S')
#ch = logging.StreamHandler(sys.stdout)
#logger.addHandler(ch)
#logger.setLevel(logging.INFO) # or use logging.INFO
#ch.setFormatter(formatter)


# FIN
Expand All @@ -214,26 +235,26 @@ def wallapop():
continue


#def recovery(times):
#try:
# def recovery(times):
# try:
# bot.polling(none_stop=True, timeout=600)
#except Exception as e:
# print("¡¡¡ERROR!!! %s intento" % (times, ))
# print(times)
# print(datetime.datetime.today().strftime('%Y-%m-%d %H:%M'))
# print(e)
# recovery(times+1)
# except Exception as e:
# print("¡¡¡ERROR!!! %s intento" % (times, ))
# print(times)
# print(datetime.datetime.today().strftime('%Y-%m-%d %H:%M'))
# print(e)
# recovery(times+1)


def main():
print("JanJanJan starting...")
logging.info("JanJanJan starting...")
db.setup()

threading.Thread(target=wallapop).start()

#recovery(1)
# recovery(1)
bot.polling(none_stop=True, timeout=3000)


if __name__ == '__main__':
main()

0 comments on commit c798975

Please sign in to comment.