-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
74 lines (69 loc) · 2.13 KB
/
run.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import time
import datetime
from BeautifulSoup import BeautifulSoup
from ConfigParser import SafeConfigParser
from pushbullet import Pushbullet
# DEFINE CONSTANTS
UNAVAILABLE = 0
PREORDER = 1
AVAILABLE = 2
# LOAD CONFIGURATION
global config
config = SafeConfigParser()
config.read('config.ini')
phones_links = json.loads(config.get("scrapper","phones"))
base_url = config.get("system","base_url")
phones = []
for phone_link in phones_links:
phone = {
"url": base_url + phone_link,
"name": phone_link.replace("-", " "),
"old_state": UNAVAILABLE,
"current_state": UNAVAILABLE
}
phones.append(phone)
# DEFINE HANDLE FUNCTIONS
def notify(phone):
date = datetime.datetime.now().strftime("%d %b %Y %H:%M")
message = '[%s]%s is %s (was %s)' % (date, phone["name"], get_status_string(phone["current_state"]), get_status_string(phone["old_state"]))
push_bullet_notify(message, phone["url"])
def get_status_string(state):
if state == AVAILABLE:
status = 'available'
elif state == UNAVAILABLE:
status = 'unavailable'
else:
status = 'in pre-order'
return status
#DEFINE NOTIFICATION FUNCTIONS
def push_bullet_notify(message, link):
pb = Pushbullet(config.get("notification","pushbullet_token"))
try:
pb.push_link(message, link)
except Exception as ex:
print ex.message
# CHECK
starttime=time.time()
interval = config.getfloat("scrapper","interval")
while True:
for phone in phones:
response = requests.get(phone["url"])
html = response.content
soup = BeautifulSoup(html)
stock = soup.find('span', attrs={'id': 'SPAN_Stock'})
if stock == None:
state = UNAVAILABLE
else:
if "En stock" in stock.text:
state = AVAILABLE
else:
state = PREORDER
phone["old_state"] = phone["current_state"]
phone["current_state"] = state
if phone["current_state"] != phone["old_state"]:
notify(phone)
time.sleep(interval - ((time.time() - starttime) % interval))