Skip to content

Commit 97d00e6

Browse files
committed
Initial commit
0 parents  commit 97d00e6

8 files changed

Lines changed: 188 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__pycache__/
2+
config/credentials.json

README.MD

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# AmazonBot
2+
3+
🤖Fast and simple bot to transform links from Amazon into a nice post with your referral link in Telegram 🛒
4+
5+
### Prerequisites
6+
* You need Python 3.6 or higher
7+
8+
```
9+
pip3 install requeriments.txt
10+
```
11+
12+
### Setup
13+
In order to setup the bot you need to copy `example_credentials.json` to `credentials.json` in `config/example_credentials.json`.
14+
Then add Bot Token (get it from [@BotFather](https://t.me/Botfather)) and Amazon configuration to the file `credentials.json`
15+
16+
### Running
17+
```
18+
#If you want just run the bot
19+
python3 main.py
20+
21+
# Or maybe run forever in to a VPS in linux
22+
nohup python3 main.py &
23+
```
24+
25+
### Changelog
26+
```
27+
Version 1.0.0
28+
- Initial Release
29+
```
30+
31+
## Built With
32+
* [python-amazon-paapi](https://github.com/sergioteula/python-amazon-paapi)
33+
* [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot)
34+
35+
## License
36+
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

config/example_credentials.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"BOT-TOKEN": "YOUR_TOKEN_BOT_HERE",
3+
4+
"amazon": {
5+
"KEY": "KEY_AMAZON",
6+
"SECRET": "SECRET_KEY_AMAZON",
7+
"TAG": "TAG_AMAZON",
8+
"COUNTRY": "COUNTRY"}
9+
10+
}

main.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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)|(?:am)|(?:an)|(?:ao)|(?:aq)|(?:ar)|(?:as)|(?:at)|(?:au)|(?:aw)|(?:ax)|(?:az)|(?:ba)|(?:bb)|(?:bd)|(?:be)|(?:bf)|(?:bg)|(?:bh)|(?:bi)|(?:bj)|(?:bm)|(?:bn)|(?:bo)|(?:br)|(?:bs)|(?:bt)|(?:bv)|(?:bw)|(?:by)|(?:bz)|(?:ca)|(?:cc)|(?:cd)|(?:cf)|(?:cg)|(?:ch)|(?:ci)|(?:ck)|(?:cl)|(?:cm)|(?:cn)|(?:co)|(?:cr)|(?:cu)|(?:cv)|(?:cw)|(?:cx)|(?:cy)|(?:cz)|(?:de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(?: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()

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
python-amazon-paapi==3.3.2
2+
amightygirl.paapi5-python-sdk==1.0.0
3+
python-telegram-bot==13.4.1
4+
requests==2.25.1

utils/create_message.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
2+
3+
def amazon_message(product):
4+
5+
6+
message = ""
7+
8+
message += f"<a href='{product.get_image()}'>​​​​​​​​​​</a>\n"
9+
10+
message += f"📌{product.get_title()}\n\n"
11+
12+
if product.get_price().pvp.value != None:
13+
message += f"💰Price: ❌{product.get_price().pvp.value}{product.get_price().price.value}\n\n"
14+
15+
elif product.get_price().price.value != None:
16+
message += f"💰Price: {product.get_price().price.value}\n\n"
17+
18+
else:
19+
message += "💰Price: Not available\n\n"
20+
21+
message += f"🔗Link: <a href=\"{product.return_url()}\">Click Here</a>\n\n"
22+
23+
buttons = InlineKeyboardMarkup([[InlineKeyboardButton(text='🛒AMAZON🛒', url=product.return_url())]])
24+
25+
return [message, buttons]

utils/product_amazon.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
import json
3+
from amazon.paapi import AmazonAPI
4+
5+
from .tools import check_domain # To return url with asin
6+
7+
with open('config/credentials.json') as config_file:
8+
config = json.load(config_file)
9+
10+
amz_config = config['amazon']
11+
amazon = AmazonAPI(amz_config['KEY'], amz_config['SECRET'], amz_config['TAG'], amz_config['COUNTRY'])
12+
13+
14+
class Product():
15+
16+
def __init__(self, asin):
17+
self.asin = asin
18+
self.product = amazon.get_product(asin)
19+
20+
def get_title(self):
21+
return self.product.title
22+
23+
def get_price(self):
24+
25+
return self.product.prices
26+
27+
def get_image(self):
28+
return self.product.images.large
29+
30+
#def get_description(self):
31+
#return self.product.raw_info.item_info.features.display_values[0]
32+
33+
def get_info(self):
34+
return self.product.raw_info
35+
36+
def return_url(self):
37+
url = self.product.raw_info.detail_page_url
38+
return 'https://' + check_domain(url) + 'dp/' + self.asin + '/?tag=' + amz_config['TAG']

utils/tools.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import re
2+
3+
def check_domain(url: str) -> str:
4+
return re.search('(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9][/]', url).group(0)

0 commit comments

Comments
 (0)