-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.py
118 lines (96 loc) · 3.15 KB
/
function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import requests
from bs4 import BeautifulSoup
import json
import os
import re
import pandas as pd
def getItem(link):
URL = link
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0",
"X-Requested-With": "XMLHttpRequest",
"Referer": link,
}
page = requests.get(URL, headers=headers)
if page.status_code == 200:
soup = BeautifulSoup(page.content, "html.parser")
link = soup.find("meta", {"property": "og:url"}).get("content")
img_link = soup.find("meta", {"property": "twitter:image"}).get("content")
desc = soup.find("meta", {"property": "og:description"}).get("content")
item = soup.find_all("div", {"class": "item-title"})
for each in item:
name = each.find("meta", {"itemprop": "name"}).get("content")
currencey = each.find("meta", {"itemprop": "priceCurrency"}).get("content")
price = each.find("meta", {"itemprop": "price"}).get("content")
fullprice = "{} {}".format(currencey, price)
return {
"product_name": name,
"price": fullprice,
"link": link,
"img_link": img_link,
"desc": desc,
}
def getPayloadEmpty():
payload = {
"blocks": [
{
"type": "section",
"block_id": "section567",
"text": {
"type": "mrkdwn",
"text": "No available link, please add/check",
},
}
]
}
return payload
def getPayload(itemRes):
product_name = itemRes["product_name"]
price = itemRes["price"]
link = itemRes["link"]
image = itemRes["img_link"]
desc = itemRes["desc"]
try:
percent = re.findall("[0-9][0-9]%", desc)[0]
except IndexError:
percent = re.findall("[0-9]%", desc)[0]
payload = {
"blocks": [
{
"type": "section",
"block_id": "section567",
"text": {
"type": "mrkdwn",
"text": "*DISCOUNT {}* <{}|{}> \n\n :mag: {}".format(
percent, link, product_name, price
),
},
"accessory": {
"type": "image",
"image_url": image,
"alt_text": product_name,
},
}
]
}
return payload
def sendToSlack(itemRes, no_data=False):
if no_data:
payload = getPayloadEmpty()
else:
payload = getPayload(itemRes)
slack_webhook = os.environ.get("SLACK_URL")
requests.post(slack_webhook, json=payload)
def getMinMax(df):
max_discount = df[df["discount"] == df["discount"].min()].discount.values.tolist()[
0
]
max_price = df[
df["current_price"] == df["current_price"].min()
].current_price.values.tolist()[0]
return max_discount, max_price
def getEarliestRecord(df):
# df['created_at'] = pd.to_datetime(df.created_at)
df = df.sort_values("created_at")
earliest_entry = df.id.values.tolist()[:5]
return earliest_entry