인공지능 공부/남박사의 파이썬 실전

(인프런) 파이썬 실전 핫딜이 뜨면 카톡으로 알려주는 프로그램 만들기

import re
import requests
from bs4 import BeautifulSoup
import json
import time
def send_kakao(text):
    KAKAO_TOKEN = ""
    header = {"Authorization" : "Bearer " + KAKAO_TOKEN}
    url = "https://kapi.kakao.com/v2/api/talk/memo/default/send"
    post = { 
        "object_type": "text",
            "text": text,
            "link": {
                "web_url": "https://developers.kakao.com",
                "mobile_web_url": "https://developers.kakao.com"
            },
            "button_title": "바로 확인"
        }

    data ={"template_object" : json.dumps(post)}
    r= requests.post(url, headers=header, data=data)
    print(r.text)

def get_hotdeal(keyword): 
    url = "https://slickdeals.net/newsearch.php?q={}".format(keyword)


    r=requests.get(url)
    bs = BeautifulSoup(r.text, "lxml")
    rows = bs.select("div.resultRow")
    results = []
    for r in rows:
        link  = r.select("a.dealTitle.bp-p-dealLink.bp-c-link")[0]
        href = link.get("href")
        if href is None:
            continue
        href = "https://slickdeals.net" + href
        title = link.text
        
        price = r.select("span.price")[0].text.replace("$", "").replace("from", "").strip()
        if price.find("/")>=0 or price=="":
            continue
        price = float(price)
        hot = len(r.select("span.icon-fire"))
        results.append((title, href, price, hot))
    return results

send_lists = []

def main():
    keyword = "ipad"
    max_price = 300.0

    while True:
        results = get_hotdeal(keyword)
        if results is not None:
            for r in results:
                title, herf, price, hot = r
                if price < max_price:
                    if title not in send_lists:
                        msg = "{} {} {} {}".format(title, price, hot, herf)
                        send_kakao(msg)
                        send_lists.append(title)
        time.sleep(60*5)
# print(get_hotdeal("ipad"))

main()