This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwallapop_alert.py
executable file
·127 lines (100 loc) · 3.88 KB
/
wallapop_alert.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
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import demiurge, sys, getopt, os, pickle, tempfile
urlWallapop = 'http://es.wallapop.com'
urlWallapopMobile = 'http://p.wallapop.com/i/'
SAVE_LOCATION = os.path.join(tempfile.gettempdir(), 'alertWallapop.pkl')
data_save = True
push_bullet = False
pushToken = '<your token here>'
email = '<your email here>'
# Demiurge for get products in Wallapop
class Products(demiurge.Item):
title = demiurge.TextField(selector='a.product-info-title')
price = demiurge.TextField(selector='span.product-info-price')
url = demiurge.AttributeValueField(selector='div.card-product-product-info a.product-info-title', attr='href')
class Meta:
selector = 'div.card-product'
class ProductDetails(demiurge.Item):
description = demiurge.TextField(selector='p.card-product-detail-description')
location = demiurge.TextField(selector='div.card-product-detail-location')
class Meta:
selector = 'div.card-product-detail'
def sendPushBullet(pushToken, email, title, body, url):
command = "curl -X POST -H 'Access-Token: {pushToken}' -F 'type=link' -F 'title={title}' -F 'body={body}' -F 'url={url}' -F 'email={email}' 'https://api.pushbullet.com/v2/pushes'".format(pushToken = pushToken, email=email, title=title, body=body, url=url)
os.system(command)
def wallAlert(urlSearch):
# Load after data search
data_temp = []
try:
dataFile = open(SAVE_LOCATION, 'rb')
data_save = pickle.load(dataFile)
except:
data_save = open(SAVE_LOCATION, 'wb')
pickle.dump(data_temp, data_save)
pass
# Read web
results = Products.all(urlSearch)
for item in results:
data_temp.append({'title': item.title
, 'price': item.price
, 'relativeUrl': item.url })
# Check new items
list_news = []
if data_save and data_save != data_temp:
for item in data_temp:
if item not in data_save:
list_news.append(item)
for item in list_news:
# Get info from new items
title = item['title'] + " - " + item['price']
url = urlWallapop + item['relativeUrl']
itemDetails = ProductDetails.one(url)
body = itemDetails.description + "\n" + itemDetails.location
productID = url.split("-")[-1]
applink = urlWallapopMobile + productID
# Send Alert
print(title, body, url)
print('-' * 10)
if push_bullet:
sendPushBullet(pushToken, email, title, body, applink)
# Save data
data_save = open(SAVE_LOCATION, 'wb')
pickle.dump(data_temp, data_save)
def usage():
print ("Usage:", __file__," -k <keywords file or list separated by comma>")
def extractArguments(argv):
# Get variables
keywordList = []
try:
opts, args = getopt.getopt(argv, "k:", ["keywords="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-k", "--keyword"):
# See if is a file
try:
with open(arg) as f:
keywordList = [item.strip('\n') for item in f.readlines()]
# It wasn't a file
except:
argSplit = arg.split(',')
for item in argSplit:
# Remove leading spaces
item = item.strip()
keywordList.append(item)
if len(keywordList) < 1:
usage()
sys.exit()
return keywordList
def main(argv):
# Process command line arguments
keywordList = extractArguments(argv)
# Loop through keywords
for keyword in keywordList:
print ("Searching", keyword)
urlSearch = 'http://es.wallapop.com/search?kws=' + keyword + '&maxPrice=&dist=0_&order=creationData-des&lat=41.398077&lng=2.170432'
wallAlert(urlSearch)
if __name__ == "__main__":
main(sys.argv[1:])