|
| 1 | +#!/usr/bin/env python |
| 2 | +# pylint: disable=W0613, C0116 |
| 3 | +# type: ignore[union-attr] |
| 4 | + |
| 5 | +import logging |
| 6 | +import json # for config |
| 7 | +import requests |
| 8 | + |
| 9 | +from telegram import Update |
| 10 | +from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext |
| 11 | + |
| 12 | + |
| 13 | +# Amazon Stuff |
| 14 | +from amazon.tools import get_asin # Get asin from url |
| 15 | +from utils.create_message import amazon_message # Create HTML template for amazon |
| 16 | +from utils.product_amazon import Product |
| 17 | +from utils.tools import check_domain |
| 18 | + |
| 19 | +with open('config/credentials.json') as config_file: |
| 20 | + config = json.load(config_file) |
| 21 | + |
| 22 | +# Enable logging |
| 23 | +logging.basicConfig( |
| 24 | + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO |
| 25 | +) |
| 26 | + |
| 27 | +logger = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +def start(update: Update, context: CallbackContext) -> None: |
| 31 | + update.message.reply_text('Send me links from Amazon! I will give you back a nice post.') |
| 32 | + |
| 33 | + |
| 34 | +def message_url(update: Update, context: CallbackContext) -> None: |
| 35 | + |
| 36 | + amazon_valid_urls = ['www.amzn.to', 'amzn.to', |
| 37 | + 'www.amazon.', 'amazon.'] |
| 38 | + |
| 39 | + url = update.message.text |
| 40 | + |
| 41 | + domain = check_domain(update.message.text) |
| 42 | + |
| 43 | + if domain.startswith(tuple(amazon_valid_urls)): |
| 44 | + |
| 45 | + if 'amzn.to/' in domain: |
| 46 | + url = requests.get(url).url |
| 47 | + |
| 48 | + product = Product(get_asin(url)) |
| 49 | + message = amazon_message(product) |
| 50 | + |
| 51 | + context.bot.send_message(update.message.chat_id, message[0] , reply_markup=message[1], parse_mode='HTML') |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +def main(): |
| 56 | + updater = Updater(config['BOT-TOKEN'], use_context=True) |
| 57 | + |
| 58 | + dispatcher = updater.dispatcher |
| 59 | + |
| 60 | + dispatcher.add_handler(CommandHandler("start", start)) |
| 61 | + |
| 62 | + #UPDATE This regex need an update |
| 63 | + dispatcher.add_handler(MessageHandler(Filters.regex('(?i)((?:https?://|www\d{0,3}[.])?[a-z0-9.\-]+[.](?:(?:international)|(?:construction)|(?:contractors)|(?:enterprises)|(?:photography)|(?:immobilien)|(?:management)|(?:technology)|(?:directory)|(?:education)|(?:equipment)|(?:institute)|(?:marketing)|(?:solutions)|(?:builders)|(?:clothing)|(?:computer)|(?:democrat)|(?:diamonds)|(?:graphics)|(?:holdings)|(?:lighting)|(?:plumbing)|(?:training)|(?:ventures)|(?:academy)|(?:careers)|(?:company)|(?:domains)|(?:florist)|(?:gallery)|(?:guitars)|(?:holiday)|(?:kitchen)|(?:recipes)|(?:shiksha)|(?:singles)|(?:support)|(?:systems)|(?:agency)|(?:berlin)|(?:camera)|(?:center)|(?:coffee)|(?:estate)|(?:kaufen)|(?:luxury)|(?:monash)|(?:museum)|(?:photos)|(?:repair)|(?:social)|(?:tattoo)|(?:travel)|(?:viajes)|(?:voyage)|(?:build)|(?:cheap)|(?:codes)|(?:dance)|(?:email)|(?:glass)|(?:house)|(?:ninja)|(?:photo)|(?:shoes)|(?:solar)|(?:today)|(?:aero)|(?:arpa)|(?:asia)|(?:bike)|(?:buzz)|(?:camp)|(?:club)|(?:coop)|(?:farm)|(?:gift)|(?:guru)|(?:info)|(?:jobs)|(?:kiwi)|(?:land)|(?:limo)|(?:link)|(?:menu)|(?:mobi)|(?:moda)|(?:name)|(?:pics)|(?:pink)|(?:post)|(?:rich)|(?:ruhr)|(?:sexy)|(?:tips)|(?:wang)|(?:wien)|(?:zone)|(?:biz)|(?:cab)|(?:cat)|(?:ceo)|(?:com)|(?:edu)|(?:gov)|(?:int)|(?:mil)|(?:net)|(?:onl)|(?:org)|(?:pro)|(?:red)|(?:tel)|(?:uno)|(?:xxx)|(?:ac)|(?:ad)|(?:ae)|(?:af)|(?:ag)|(?:ai)|(?:al)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:mz)|(?:na)|(?:nc)|(?:ne)|(?:nf)|(?:ng)|(?:ni)|(?:nl)|(?:no)|(?:np)|(?:nr)|(?:nu)|(?:nz)|(?:om)|(?:pa)|(?:pe)|(?:pf)|(?:pg)|(?:ph)|(?:pk)|(?:pl)|(?:pm)|(?:pn)|(?:pr)|(?:ps)|(?:pt)|(?:pw)|(?:py)|(?:qa)|(?:re)|(?:ro)|(?:rs)|(?:ru)|(?:rw)|(?:sa)|(?:sb)|(?:sc)|(?:sd)|(?:se)|(?:sg)|(?:sh)|(?:si)|(?:sj)|(?:sk)|(?:sl)|(?:sm)|(?:sn)|(?:so)|(?:sr)|(?:st)|(?:su)|(?:sv)|(?:sx)|(?:sy)|(?:sz)|(?:tc)|(?:td)|(?:tf)|(?:tg)|(?:th)|(?:tj)|(?:tk)|(?:tl)|(?:tm)|(?:tn)|(?:to)|(?:tp)|(?:tr)|(?:tt)|(?:tv)|(?:tw)|(?:tz)|(?:ua)|(?:ug)|(?:uk)|(?:us)|(?:uy)|(?:uz)|(?:va)|(?:vc)|(?:ve)|(?:vg)|(?:vi)|(?:vn)|(?:vu)|(?:wf)|(?:ws)|(?:ye)|(?:yt)|(?:za)|(?:zm)|(?:zw))(?:/[^\s()<>]+[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019])?)'), message_url)) |
| 64 | + |
| 65 | + updater.start_polling() |
| 66 | + updater.idle() |
| 67 | + |
| 68 | +if __name__ == '__main__': |
| 69 | + main() |
0 commit comments