-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigikey-stock-watcher.php
96 lines (73 loc) · 2.71 KB
/
digikey-stock-watcher.php
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
<?php
/**
* A PHP parser/watcher/notifier of DoggyKey stock status for P/N list
* @package digikey-stock-watcher
* @author Dmitry Murzinov ([email protected])
* @link : https://github.com/iDoka/digikey-cool-stuff
* @version 1.0
*/
$file = "partnumber.list";
include_once('simplehtmldom/simple_html_dom.php');
define('__ROOT__', dirname(__FILE__));
require_once(__ROOT__.'/pushover.php');
ini_set('auto_detect_line_endings',TRUE);
ini_set('mbstring.internal_encoding','UTF-8');
$url_array = array();
$url_array = file($file);
//$url_array = file_get_contents($file);
// print_r($url_array);
//var_dump($url_array);
foreach ($url_array as $url) {
$url = trim($url," ");
$url = trim($url,"\n");
$url = trim($url,"\r");
//echo $url.PHP_EOL;
//var_dump($url);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_REFERER, "https://www.digikey.com/");
//curl_setopt($curl, CURLOPT_VERBOSE, true); // debug
//curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); // save cookie
//curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); // read cookie
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 3);
//curl_setopt($curl, CURLOPT_POST, 1); // do not use!
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$str = curl_exec($curl);
curl_close($curl);
$html = new simple_html_dom(); // Create a DOM object
$html->load($str); // Load HTML from a string
//$html = file_get_html($url);
foreach($html->find('h1[itemprop=model]') as $k)
$pn = strval(trim($k->plaintext," "));
//$pn = mb_convert_encoding(trim($k->plaintext," "), "UTF-8");
foreach($html->find('span#dkQty') as $k)
$qty = intval(trim($k->plaintext," \xC2\xA0"));
if (!isset($qty)) $qty = 0;
foreach($html->find('span[itemprop=price]') as $k)
$price = floatval(trim($k->plaintext," \xC2\xA0"));
$html->clear();
unset($html);
//echo $pn."\t".$qty."\t".$price.PHP_EOL;
if ($qty > 0) {
$qty = "\e[7m".$qty."\e[0m";
$pn = "\e[1m".$pn."\e[0m";
$msg = $pn." is available now (".$qty." pcs) at $".$price;
/////////////////// Debug print:
echo $msg.PHP_EOL;
/////////////////// PushOver notification:
//pushover($msg);
/////////////////// to mail:
//mail("me<[email protected]>","$pn",$msg,"From: DigiKey<[email protected]>","\r\n");
}
unset($qty);
unset($pn);
unset($price);
usleep(100000); //100 ms
}
?>