Douglas Andreani 4 年之前
当前提交
d0290962b0
共有 6 个文件被更改,包括 205 次插入0 次删除
  1. 1 0
      .gitignore
  2. 103 0
      amazon.py
  3. 37 0
      bot_buy.py
  4. 52 0
      kabum.py
  5. 7 0
      list-amazon.txt
  6. 5 0
      list.txt

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+__pycache__/

+ 103 - 0
amazon.py

@@ -0,0 +1,103 @@
+from bs4 import BeautifulSoup
+import requests
+import time
+from bot_buy import Messenger
+import random
+### lists.txt format
+### url,expected price,product name
+
+
+def get_user_agent():
+    agents = []
+    agents.append('Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148')
+    agents.append('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36')
+    agents.append('Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148')
+    agents.append('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)')
+    agents.append('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134')
+    agents.append('Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko')
+    agents.append('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko)')
+    agents.append('Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1')
+    return random.choice(agents)
+
+
+url = ""
+m = Messenger()
+while (1):
+    with open('list-amazon.txt', 'r') as f:
+        lines = f.readlines()
+        for l in lines:
+            url = l.split(',')[0].rstrip()
+            price = float(l.split(',')[1].rstrip())
+            prod = l.split(',')[2].rstrip()
+            
+            headers = {
+                        "User-Agent": get_user_agent(),
+                        "Connection": "keep-alive",
+                        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
+                        "Accept-Encoding": "gzip, deflate, br",
+                        "Accept-Language": "en-US,en;q=0.5",
+                        "Host": "www.amazon.com.br"
+            }
+
+            try:
+                s = requests.Session()
+                amaz = s.get("https://www.amazon.com.br", headers=headers)
+                soup = BeautifulSoup(amaz.text, 'html.parser')
+            except:
+                print('failed to request')
+                continue
+
+            a_last = soup.findAll('p', class_="a-last")
+            skip = False
+            for s in a_last:
+
+                text = s.get_text()
+                print(text)
+                if text > "Our servers are gettin":
+                    print("Skipping try")
+                    skip = True
+            if skip:
+                continue
+
+
+            try:
+                r = s.get(url, headers=headers, cookies=amaz.cookies)
+            except:
+                print('failed to request')
+                continue
+            
+            soup = BeautifulSoup(r.text, 'html.parser')
+
+            #<p class="a-last">
+            a_last = soup.findAll('p', class_="a-last")
+            skip = False
+            for s in a_last:
+
+                text = s.get_text()
+                print(text)
+                if text > "Our servers are gettin":
+                    print("Skipping try")
+                    skip = True
+            if skip:
+                continue
+                    
+
+            
+            spans = []
+            price_normal = soup.find(id="priceblock_ourprice")
+            price_offer = soup.find(id="priceblock_dealprice")
+            spans.append(price_normal)
+            spans.append(price_offer)
+            
+            for s in spans:
+                if s:
+                    new_price = float(s.get_text().split('$')[1].replace(".","").replace(',', '.'))
+                    print(f"Produto: {prod} -> Esperado: {price} -> Agora: {new_price}")
+                    if new_price <= price:
+                        m.sendMessage(f"Got it! {prod} -> {new_price}")
+            time.sleep(10)
+    time.sleep(120)
+
+
+
+

+ 37 - 0
bot_buy.py

@@ -0,0 +1,37 @@
+import os
+api_id = os.environ['TELEGRAM_API']
+
+import telegram
+import time
+from telegram.error import NetworkError, Unauthorized
+from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
+
+def start(update, context):
+        update.message.reply_text(context['chat']['id'])
+
+def help(update, context):
+        update.message.reply_text('Help!')
+def error(update, context):
+        """Log Errors caused by Updates."""
+        print(f'Update {update} caused error {context.error}')
+
+
+class Messenger:
+
+    def __init__(self):
+        self.bot = telegram.Bot(api_id)
+        
+    def sendMessage(self, messageText):
+        self.bot.send_message(chat_id="839816588", text=messageText)
+
+    def listener(self):
+        self.updater = Updater(api_id, use_context=True)
+        self.dp = self.updater.dispatcher
+        self.dp.add_handler(CommandHandler("start", start))
+        self.dp.add_handler(CommandHandler("help", help))
+        self.dp.add_error_handler(error)
+        self.updater.start_polling()
+        self.updater.idle()
+
+
+

+ 52 - 0
kabum.py

@@ -0,0 +1,52 @@
+from bs4 import BeautifulSoup
+import requests
+import time
+from bot_buy import Messenger
+
+### lists.txt format
+### url,expected price,product name
+
+
+url = ""
+m = Messenger()
+
+while (1):
+    with open('list.txt', 'r') as f:
+        lines = f.readlines()
+        for l in lines:
+            url = l.split(',')[0].rstrip()
+            price = float(l.split(',')[1].rstrip())
+            prod = l.split(',')[2].rstrip()
+            
+
+            try:
+                    r = requests.get(url)
+            except:
+                print('failed to request')
+                continue
+            
+            soup = BeautifulSoup(r.text, 'html.parser')
+
+            #print(soup.prettify())
+            price_normal = soup.find('span', class_="preco_desconto")
+            price_bf = soup.find('span', class_="preco_desconto_avista-cm")
+
+            if price_normal:
+                price_normal = price_normal.find('strong')
+
+            spans = [price_normal, price_bf]
+            #print(type(price_bf))
+            
+            for s in spans:
+                if s:
+                    try:
+                        new_price = float(s.get_text().split(' ')[1].replace(".","").replace(',', '.'))
+                        print(f"Produto: {prod} -> Esperado: {price} -> Agora: {new_price}")
+                        if new_price <= price:
+                            m.sendMessage(f"Got it! {prod} -> {new_price}\n{url}")
+                    except:
+                        print(f"failed to parse {new_price}")
+            time.sleep(10)
+    time.sleep(120)
+
+

+ 7 - 0
list-amazon.txt

@@ -0,0 +1,7 @@
+https://www.amazon.com.br/Carregador-Port%C3%A1til-Anker-11131978-Preto/dp/B07F6KM5C2,100,PowerBank Anker
+https://www.amazon.com.br/Notebook-Lenovo-Ideapad-S145-81S90005BR/dp/B07TSJ2RXY,1800,IdeaPad S145
+https://www.amazon.com.br/Nintendo-Switch-Azul-Vermelho-Neon/dp/B07VGRJDFY,1700,New Nintendo Switch
+https://www.amazon.com.br/Switch-HACSKABAA-Nintendo-Console-Neon/dp/B01MUAGZ49,1300,Nintendo Switch
+https://www.amazon.com.br/Fire-TV-Stick-Basic-Edition/dp/B01ETRGEIG,130,Fire Stick
+https://www.amazon.com.br/Amazfit-Gtr-47mm-Aço-Inoxidável/dp/B07WFW1YGR,700,Amazfit GTR
+https://www.amazon.com.br/Smartphone-Samsung-Galaxy-Note-Preto/dp/B07YBDXWRG,4000,Galaxy Note 10+

+ 5 - 0
list.txt

@@ -0,0 +1,5 @@
+https://www.kabum.com.br/produto/102690/placa-de-video-gigabyte-nvidia-geforce-rtx-2070-super-gaming-8gb-gddr6-gv-n207sgaming-oc-8gc/?tag=2070%20super,2500,Gigabyte RTX2070 Super
+https://www.kabum.com.br/produto/102721/placa-de-video-evga-geforce-rtx-2070-super-xc-ultra-gaming-8gb-gddr6-08g-p4-3173-kr-/?tag=2070%20super,2500,EVGA RTX2070 Super
+https://www.kabum.com.br/produto/102974/placa-de-video-galax-nvidia-geforce-rtx-2070-super-ex-1-click-oc-8gb-gddr6-27isl6mdu9ex/?tag=2070%20super,2500,Galax RTX2070 Super
+https://www.kabum.com.br/cgi-local/site/produtos/descricao_ofertas.cgi?codigo=85197,130,Kingston A400 SSD 240
+https://www.kabum.com.br/produto/102715/placa-de-video-msi-geforce-rtx-2060-super-gaming-x-8gb-gddr6/?tag=rtx%202060%20super,2300,MSI RTX2060 Super