-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.php
174 lines (140 loc) · 4.55 KB
/
app.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
// check for username
if (false === getenv('username')) {
echo json_encode([
'items' => [[
"arg" => "https://www.alfredapp.com/help/workflows/advanced/variables/",
"title" => "Please set your GitHub username first",
"subtitle" => "Hit enter to open an introduction to variables."
],],
]);
exit(1);
}
$cache_path = getenv('alfred_workflow_cache');
$cache_response = $cache_path . '/cache.json';
$cache_icons = $cache_path . '/icons/';
// check first if caching directory exists.
if (!is_dir($cache_path)) {
mkdir($cache_path);
}
if (!is_dir($cache_icons )) {
mkdir($cache_icons);
}
$username = trim(getenv('username')); // set inside workflow variables
$token = trim(getenv('token')); // set inside workflow variables
$starred_url = sprintf('https://api.github.com/users/%s/starred', $username);
$cache_ttl = (false === getenv('cache_ttl')) ? 3600 * 24 : (int) getenv('cache_ttl'); // in seconds
$query = trim($argv[1]); // optional text search
$http_status = 200; // default status code, so when using cache it doesn't run into error handling
// check if we hafe a cache
// if not load stars from github API
if (file_exists($cache_response) && filemtime($cache_response) > (time() - $cache_ttl)) {
$resp_json = json_decode(file_get_contents($cache_response), true);
} else {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $starred_url);
curl_setopt($curl, CURLOPT_ENCODING, "");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'GitHub Stars Alfred workflow for: ' . $username );
if (!empty($token)) {
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $token);
}
$resp = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($resp, 0, $header_size);
$resp = substr($resp, $header_size);
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$resp_json = json_decode($resp, true);
// check if there are headers indication pagination
// => make multiple requests to fetch ALL the stars.
if (preg_match('/Link: .*?page=([0-9]+)>; rel="last"/i', $header, $m)) {
$last_page = (int) $m[1];
for ($i = 2; $i <= $last_page; $i++) {
$page_url = $starred_url . '?page=' . $i;
curl_setopt($curl, CURLOPT_URL, $page_url);
$resp = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($resp, 0, $header_size);
$loop = substr($resp, $header_size);
$loop_json = json_decode($loop, true);
$resp_json = array_merge($resp_json, $loop_json);
}
}
curl_close($curl);
// cache response
if ($http_status == 200) {
file_put_contents($cache_response, json_encode($resp_json, JSON_PRETTY_PRINT));
}
}
$items = [];
// Github API returened some sort of error.
// Also check for presence of `message` key, if HTTP Status
// code was not set to an error.
if (200 !== (int) $http_status OR isset($resp_json['message'])) {
echo json_encode([
'items' => [
[
"arg" => $resp_json['documentation_url'],
"title" => sprintf("GitHub Response Error (%s)", $http_status),
"subtitle" => $resp_json['message'],
],
],
]);
exit(1);
}
// Search through the results.
foreach ($resp_json as $i => $star){
$url = $star['html_url'];
$title = $star['name'];
$subtitle = $star['description'];
if ($query) {
$search_string = $star["full_name"] . ' ' . $star['description'];
$query_matched = stripos($search_string, $query);
if ($query_matched === false) {
continue;
}
}
$icon_url = $star['owner']['avatar_url'];
$icon = $cache_icons . $star['id'] . '.png';
if (!is_file($icon)) {
$fp = fopen ($icon, 'w+');
$ch = curl_init($icon_url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo json_encode([
'rerun' => .1,
'variables' => [
//'resp_json' => $resp_json,
'amount' => count($resp_json),
'current' => $i,
],
'items' => [
[
"arg" => null,
"title" => sprintf("Downloading GitHub avatars…"),
"subtitle" => sprintf('Downloading image %s of %s', $i + 1, count($resp_json)),
],
],
]);
exit(0);
}
$items['items'][] = [
'arg' => $url,
'quicklookurl' => $url,
'title' => $title,
'subtitle' => $subtitle,
'text' => [
'largetype' => $title,
],
'icon' => [
'path' => $icon
],
];
}
echo json_encode($items);