Skip to content

Commit 2b2c201

Browse files
committed
Added new libraries, removed old ones
Started on creating auto-updater
1 parent 7b35bc0 commit 2b2c201

File tree

6 files changed

+156
-49
lines changed

6 files changed

+156
-49
lines changed

autoload.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
function __autoload($class)
4+
{
5+
require 'libs/' . $class . '.php';
6+
}

incl/functions.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

libs/Releases.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
class Releases {
4+
5+
6+
public static function get($repo, $release, $user='phpfunk', $max_releases=5)
7+
{
8+
$releases = json_decode(Tools::fetch('https://api.github.com/repos/' . $user . '/' . $repo . '/tags'));
9+
$arr = array();
10+
11+
// If good-to-go, do it
12+
if (json_last_error() == JSON_ERROR_NONE && ! empty($releases) && ! isset($releases->message)) {
13+
foreach ($releases as $k => $obj) {
14+
$version = str_replace('.', '', $obj->name);
15+
if (is_numeric($version)) {
16+
$arr[$version] = $obj->name;
17+
}
18+
}
19+
krsort($arr);
20+
21+
$releases = array();
22+
$x = 1;
23+
$current = str_replace('.', '', $release);
24+
25+
foreach ($arr as $n => $release) {
26+
if ($x < $max_releases) {
27+
28+
$title = 'Version ' . $release;
29+
if ($current > $n) {
30+
$subtitle = 'Downgrade to version ' . $release . '. (Ctrl+Enter to view release)';
31+
}
32+
elseif ($current < $n) {
33+
$subtitle = 'Upgrade to version ' . $release . '. (Ctrl+Enter to view release)';
34+
}
35+
else {
36+
$title .= ' (Current Version)';
37+
$subtitle = 'This is the version you are currently running.';
38+
}
39+
40+
array_push($releases, array(
41+
'arg' => $release,
42+
'title' => $title,
43+
'subtitle' => $subtitle,
44+
'icon' => 'icon.png',
45+
'autocomplete' => $release
46+
));
47+
$x += 1;
48+
}
49+
else {
50+
break;
51+
}
52+
}
53+
}
54+
else {
55+
$release = array();
56+
array_push($releases, array(
57+
'arg' => 'error',
58+
'title' => 'Error',
59+
'subtitle' => 'There was an error extracting the releases from Github.',
60+
'icon' => 'icon.png',
61+
'autocomplete' => 'error'
62+
));
63+
}
64+
65+
if (! empty($releases)) {
66+
print Tools::arrayToXML($releases);
67+
}
68+
}
69+
}

libs/Spotify.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
class Spotify {
4+
5+
private static $max_releases = 5;
6+
private static $releases = '2.2.2';
7+
8+
public function _construct()
9+
{
10+
11+
}
12+
13+
public static function getReleases()
14+
{
15+
$releases = json_decode(file_get_contents('https://api.github.com/repos/phpfunk/alfred-spotify-controls/tags'));
16+
17+
// If good-to-go, do it
18+
if (json_last_error() == JSON_ERROR_NONE) {
19+
print '<pre>';
20+
print_r($releases);
21+
}
22+
}
23+
}

libs/Tools.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
class Tools {
4+
5+
public static function arrayToXML($a)
6+
{
7+
if (! is_array($a)) {
8+
return false;
9+
}
10+
11+
$items = new SimpleXMLElement("<items></items>");
12+
13+
foreach($a as $b) {
14+
$c = $items->addChild('item');
15+
$c_keys = array_keys($b);
16+
foreach($c_keys as $key) {
17+
if ($key == 'uid') {
18+
$c->addAttribute('uid', $b[$key]);
19+
}
20+
elseif ($key == 'arg') {
21+
$c->addAttribute('arg', $b[$key]);
22+
}
23+
else {
24+
$c->addChild($key, $b[$key]);
25+
}
26+
}
27+
}
28+
29+
return $items->asXML();
30+
}
31+
32+
public static function fetch($url)
33+
{
34+
$ch = curl_init($url);
35+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
36+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
37+
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
38+
curl_setopt($ch, CURLOPT_HTTPGET, 1);
39+
curl_setopt($ch, CURLOPT_USERAGENT, 'Easy Fetch by Jeff Johns @phpfunk');
40+
$page = curl_exec($ch);
41+
$info = curl_getinfo($ch);
42+
curl_close($ch);
43+
return ($info['http_code'] == '200') ? $page : null;
44+
}
45+
46+
public static function getTrackArtwork($type, $id)
47+
{
48+
$html = fetch('http://open.spotify.com/' . $type . '/' . $id);
49+
if (! empty($html)) {
50+
preg_match_all('/.*?og:image.*?content="(.*?)">.*?/is', $html, $m);
51+
return (isset($m[1][0])) ? $m[1][0] : '';
52+
}
53+
return '';
54+
}
55+
}

releases.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
include 'autoload.php';
3+
Releases::get('alfred-spotify-controls', '2.2.2');

0 commit comments

Comments
 (0)