-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstocks.php
135 lines (105 loc) · 3.69 KB
/
stocks.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
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
127
128
129
130
131
132
133
134
<?php
namespace NesStocks;
require_once("vendor/autoload.php");
$config = require("config/config.php");
// files to store store availability
$stocks_file = $config['stocks_file'];
$avails_file = $config['avails_file'];
// list of stores to check
$input_file = $config['stores_file'];
// resume file from a previous session
$resume_file = null;
// input file from command line
if (isset($_SERVER['argv'][1])) {
$input_file = $_SERVER['argv'][1];
}
// resume file from command line
if (isset($_SERVER['argv'][2])) {
$resume_file = $_SERVER['argv'][2];
}
// list of checked store ids and zip codes
$used_ids = $used_zips = array();
// list of store checker objects
$checkers = array();
// if resume file specified, load used_ids and used_zips with checked stores
if ($resume_file) {
$stocks = file($resume_file, FILE_IGNORE_NEW_LINES);
foreach ($stocks as $stock) {
list($zipcode, $id, $s, $c, $zip, $retailer) = explode(' | ', $stock);
print "loading " . $retailer . " " . $id . " " . $zipcode . "\n";
$used_zips[$retailer][$zipcode] = $zipcode;
$used_zips[$retailer][$zip] = $zip;
$used_ids[$retailer][$id] = $id;
}
}
// load the list of stores
$stores = new Stores($input_file);
// loop through all retailer zipcodes
foreach ($stores->get() as $zip_retailer) {
list($retailer, $zipcode, $storeid) = $zip_retailer;
print "=== " . $retailer . " " . $zipcode . " ===\n";
// skip if storeid already checked
if (isset($used_ids[$retailer]) && isset($used_ids[$retailer][$storeid])) {
print "already searched store id: " . $storeid . "\n";
continue;
}
// skip if zipcode already checked
if (isset($used_zips[$retailer]) && isset($used_zips[$retailer][$zipcode])) {
print "already searched zip: " . $zipcode . "\n";
continue;
}
// create checker object if not already created
if (!isset($checkers[$retailer]) && isset($config['retailers'][$retailer])) {
$cfg = $config['retailers'][$retailer];
$cls = "\\NesStocks\\" . $cfg['checker'];
$checkers[$retailer] = new $cls($cfg);
}
// skip if no checker defined for retailer
if (!isset($checkers[$retailer])) {
print "no checker for: " . $retailer . "\n";
continue;
}
// check the store's availability
$result = $checkers[$retailer]->check($zipcode);
// loop through store results and save to avails/stocks files and used_ids/zips
foreach ($result as $item) {
$line = join(' | ', array(
$item['state'],
$item['city'],
$item['zip'],
$retailer,
$item['address'],
$item['phone'],
$item['avail'],
$item['onhand_quantity'],
$item['saleable_quantity'],
$item['price'],
)) . "\n";
// already checked this store's id
if (isset($used_ids[$retailer][$item['id']])) {
print "EXISTING STORE ID: ";
}
// already checked this store's zip code
else if (isset($used_zips[$retailer][$item['zip']])) {
print "EXISTING ZIP: ";
}
// show available if not out of stock and sold in store
if ($item['avail'] != 'Out of Stock' && $item['avail'] != 'Not sold in this store') {
print "AVAILABLE: ";
}
// print availability
print $item['id'] . ' | ' . $line;
// save to avails file if available
if (!isset($used_ids[$retailer][$item['id']]) && !isset($used_zips[$retailer][$item['zip']]) && $item['avail'] != 'Out of Stock' && $item['avail'] != 'Not sold in this store') {
file_put_contents($avails_file, $line, FILE_APPEND);
}
// save to stocks file
file_put_contents($stocks_file, $zipcode . ' | ' . $item['id'] . ' | ' . $line, FILE_APPEND);
// mark the returned store's id as checked
$used_ids[$retailer][$item['id']] = $item['id'];
}
// mark the search id as checked
$used_ids[$retailer][$storeid] = $storeid;
// mark the search zip code as checked
$used_zips[$retailer][$zipcode] = $zipcode;
}