1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Data Analytics |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the LICENSE.md file. |
7
|
|
|
* |
8
|
|
|
* @author Marcel Scherello <[email protected]> |
9
|
|
|
* @copyright 2019 Marcel Scherello |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Datasource; |
13
|
|
|
|
14
|
|
|
use OCP\ILogger; |
15
|
|
|
|
16
|
|
|
class GithubService |
17
|
|
|
{ |
18
|
|
|
private $logger; |
19
|
|
|
|
20
|
|
|
public function __construct( |
21
|
|
|
ILogger $logger |
22
|
|
|
) |
23
|
|
|
{ |
24
|
|
|
$this->logger = $logger; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the items for the selected category |
29
|
|
|
* |
30
|
|
|
* @NoAdminRequired |
31
|
|
|
* @param array $option |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function read($option) |
35
|
|
|
{ |
36
|
|
|
if (isset($option['link'])) $string = 'https://api.github.com/repos/' . $option['link'] . '/releases'; |
37
|
|
|
else $string = 'https://api.github.com/repos/' . $option['user'] . '/' . $option['repository'] . '/releases'; |
38
|
|
|
|
39
|
|
|
$ch = curl_init(); |
40
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
|
|
|
|
41
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
42
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
43
|
|
|
curl_setopt($ch, CURLOPT_URL, $string); |
44
|
|
|
curl_setopt($ch, CURLOPT_REFERER, $string); |
45
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); |
46
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); |
47
|
|
|
$result = curl_exec($ch); |
|
|
|
|
48
|
|
|
curl_close($ch); |
|
|
|
|
49
|
|
|
#echo $result; |
50
|
|
|
|
51
|
|
|
$jason_a = json_decode($result, true); |
52
|
|
|
$i = 0; |
53
|
|
|
|
54
|
|
|
$data = array(); |
55
|
|
|
foreach ($jason_a as $item) { |
56
|
|
|
if (isset($option['limit'])) { |
57
|
|
|
if ($i === (int)$option['limit']) break; |
58
|
|
|
} |
59
|
|
|
$nc_value = 0; |
60
|
|
|
foreach ($item['assets'] as $asset) { |
61
|
|
|
if (substr($asset['name'], -2) == 'gz') $nc_value = $asset['download_count']; |
62
|
|
|
} |
63
|
|
|
array_push($data, ['dimension1' => '', 'dimension2' => $item['tag_name'], 'dimension3' => $nc_value]); |
64
|
|
|
$i++; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$header = array(); |
68
|
|
|
$header['dimension1'] = ''; |
69
|
|
|
$header['dimension2'] = 'Version'; |
70
|
|
|
$header['dimension3'] = 'Count'; |
71
|
|
|
|
72
|
|
|
$result = [ |
73
|
|
|
'header' => $header, |
74
|
|
|
'data' => $data, |
75
|
|
|
'error' => 0, |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* template for options & settings |
83
|
|
|
* |
84
|
|
|
* @NoAdminRequired |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getTemplate() |
88
|
|
|
{ |
89
|
|
|
$template = array(); |
90
|
|
|
array_push($template, ['id' => 'user', 'name' => 'GitHub Username', 'placeholder' => 'GitHub user']); |
91
|
|
|
array_push($template, ['id' => 'repository', 'name' => 'Repository', 'placeholder' => 'GitHub repository']); |
92
|
|
|
array_push($template, ['id' => 'limit', 'name' => 'Limit', 'placeholder' => 'Number of records']); |
93
|
|
|
array_push($template, ['id' => 'timestamp', 'name' => 'Timestamp of dataload', 'placeholder' => 'true/false']); |
94
|
|
|
return $template; |
95
|
|
|
} |
96
|
|
|
} |