Skip to content

Commit

Permalink
Changes Api v3
Browse files Browse the repository at this point in the history
  • Loading branch information
z0r3f committed Jun 13, 2021
1 parent 311ecea commit 2c29339
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/workspace.xml
.idea/shelf/
db.sqlite
venv/
6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/wallbot.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions dbhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 sqlite3_instructions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.schema
.exit
61 changes: 32 additions & 29 deletions ssbo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

TOKEN = "SUSTITUIR-POR-EL-TOKEN-DEL-BOT-DE-TELEGRAM"
URL = "https://api.telegram.org/bot{}/".format(TOKEN)
URL_ITEMS = "https://es.wallapop.com/rest/items"
# URL_ITEMS = "https://es.wallapop.com/rest/items"
URL_ITEMS = "https://api.wallapop.com/api/v3/general/search"

db = DBHelper()

Expand Down Expand Up @@ -42,22 +43,22 @@ def notel(chat_id, price, title, url_item, obs=None):
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,30 +76,32 @@ def get_items(url, chat_id):
try:
resp = requests.get(url=url)
data = resp.json()
for x in data['items']:
# 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)
# print(data)
for x in data['search_objects']:
print(x)
print('\t'.join((datetime.datetime.today().strftime('%Y-%m-%d %H:%M'),
str(x['id']), str(x['price']), 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'])
db.add_item(x['id'], chat_id, x['title'], x['price'], x['web_slug'], x['user']['id'])
notel(chat_id, str(x['price']), x['title'], x['web_slug'])
print('\t'.join((datetime.datetime.today().strftime('%Y-%m-%d %H:%M'),
'NEW ', str(x['itemId']), x['price'], x['title'])))
'NEW ', str(x['id']), str(x['price']), 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)
notel(chat_id, x['price'], x['title'], x['web_slug'], obs)
print('\t'.join((datetime.datetime.today().strftime('%Y-%m-%d %H:%M'),
'BAJA', str(x['itemId']), x['price'], x['title'])))
'BAJA', str(x['id']), x['price'], x['title'])))
except Exception as e:
print(e)

Expand Down Expand Up @@ -140,8 +143,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 @@ -188,7 +191,7 @@ def add_search(message):

logger = telebot.logger
formatter = logging.Formatter('[%(asctime)s] %(thread)d {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
'%m-%d %H:%M:%S')
'%m-%d %H:%M:%S')
ch = logging.StreamHandler(sys.stdout)
logger.addHandler(ch)
logger.setLevel(logging.ERROR) # or use logging.INFO
Expand All @@ -214,15 +217,15 @@ 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():
Expand All @@ -231,7 +234,7 @@ def main():

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

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


Expand Down

0 comments on commit 2c29339

Please sign in to comment.