Skip to content

Commit

Permalink
Updated OneUpdater (thx@ vitorgalvao), replaced NSAppleScript by a no…
Browse files Browse the repository at this point in the history
…rmal RunScript(JS) that handles the latest installed Alfred (no matter if we switch to Alfred 4..5..6 in future), fixed connection issues by changing file_put_contents to curl and extending url_get_contents().
  • Loading branch information
eusi committed Oct 4, 2016
1 parent 9d2d535 commit 2e3c2fd
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 68 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
TwitchStreamer Workflow for [Alfred](http://www.alfredapp.com)
==============================

## In Short
## Summary
* Search Twitch.tv streams and watch in [VLC](http://www.videolan.org/vlc/index.html) or [mpv](https://mpv.io/) ([Livestreamer](https://github.com/chrippa/livestreamer) required)

* keywords: `tw`, `twtop`, `twgames`, optional: `twbygame`, `twcover`
Expand Down Expand Up @@ -86,6 +86,10 @@ GNU General Public License version 3

#Changelog

## 1.8

* Updated OneUpdater (thx@ vitorgalvao), replaced NSAppleScript by a normal RunScript(JS) that handles the latest installed Alfred (no matter if we switch to Alfred 4..5..6 in future), fixed connection issues by changing file_put_contents to curl and extending url_get_contents().

## 1.75

* Added [`OneUpdater`](https://github.com/vitorgalvao/alfred-workflows/tree/master/OneUpdater) ([forum post](http://www.alfredforum.com/topic/9224-oneupdater-—-update-workflows-with-a-single-node/)) that allows you to update the workflow out of the box (it checks for updates automatically every 15 days and downloads/opens new versions). Changed the usage of Terminal to Run Script (no opened terminal any longer during watching stream). Thx@ vitorgalvao.
Expand Down
2 changes: 1 addition & 1 deletion current-version.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 1.75,
"version": 1.8,
"download_url": "https://raw.githubusercontent.com/eusi/alfred2-twitch-streamer/master/workflow/TwitchStreamer.alfredworkflow",
"description": "TwitchStreamer"
}
60 changes: 24 additions & 36 deletions src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<key>disabled</key>
<false/>
<key>name</key>
<string>TwitchStreamer v1.75</string>
<string>TwitchStreamer v1.8</string>
<key>objects</key>
<array>
<dict>
Expand Down Expand Up @@ -344,9 +344,9 @@ if [[ $(find "${local_file}" -mtime +"${frequency_check}") ]]; then
abort "'workflow_url' ("${workflow_url}") appears to not be reachable."
fi # url_exists
fi # workflow_type
else
touch "${local_file}" # Reset timer by touching local file
fi # diff
else
touch "${local_file}" # Reset timer by touching local file
fi #find</string>
<key>scriptargtype</key>
<integer>0</integer>
Expand Down Expand Up @@ -439,45 +439,33 @@ echo $w-&gt;toxml();</string>
<dict>
<key>config</key>
<dict>
<key>applescript</key>
<string>--on alfred_script(q)
--try
--do shell script "osascript -e 'id of app \"Alfred 3\"'"
--tell application "Alfred 3" to search "twbygame " &amp; q
--end try
--end alfred_script
on alfred_script(q)
-- use Alfred 3 if exists, otherwise Alfred 2
set isAlfred3 to false
try
do shell script "osascript -e 'id of app \"Alfred 3\"'"
set isAlfred3 to true
end try
if isAlfred3 then
tell application "Alfred 3" to search "twbygame " &amp; q
else
tell application "Alfred 2" to search "twbygame " &amp; q
end if
<key>concurrently</key>
<false/>
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>ObjC.import('stdlib');
function run(argv)
{
// Get major version number
var v = $.getenv('alfred_version').split('.')[0];
end alfred_script</string>
<key>cachescript</key>
<false/>
Application('Alfred ' + v).search('twbygame ' + argv[0]);
}</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>type</key>
<integer>7</integer>
</dict>
<key>type</key>
<string>alfred.workflow.action.applescript</string>
<string>alfred.workflow.action.script</string>
<key>uid</key>
<string>93749124-28BB-4E7E-AD32-8B1CD790BE08</string>
<key>version</key>
<integer>1</integer>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
Expand Down Expand Up @@ -733,7 +721,7 @@ examples:
</dict>
</dict>
<key>version</key>
<string>1.75</string>
<string>1.8</string>
<key>webaddress</key>
<string>https://github.com/eusi/alfred2-twitch-streamer</string>
</dict>
Expand Down
104 changes: 75 additions & 29 deletions src/search.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
//twitch api now requires a specific client id per application/developer
//Twitch API now requires a specific client id per application/developer
$clientId = "&client_id=pq1763hfd37807lw0z8pqum3isqr4hd";

/**
Expand All @@ -8,6 +8,7 @@
* @param int $limit - maximum number of streams sorted by number of viewers descending
*/
function getTwitchStreams( $limit ) {

global $clientId;
//https://github.com/justintv/Twitch-API/blob/master/v3_resources/streams.md
$url = 'https://api.twitch.tv/kraken/streams?limit=' . $limit . $clientId;
Expand Down Expand Up @@ -38,6 +39,7 @@ function getTwitchStreams( $limit ) {
*
*/
function getTopGames( $limit ) {

global $clientId;
$url = 'https://api.twitch.tv/kraken/games/top?limit=' . $limit . $clientId;
$json = url_get_contents($url);
Expand All @@ -46,7 +48,7 @@ function getTopGames( $limit ) {
return false;
}

$data = json_decode($json)->top;
$data = json_decode($json, false)->top;
$games = array();

if($data != null) {
Expand All @@ -66,6 +68,7 @@ function getTopGames( $limit ) {
* @param int $limit
*/
function searchStream( $q, $limit ) {

global $clientId;
//https://github.com/justintv/Twitch-API/blob/master/v3_resources/search.md
$url = 'https://api.twitch.tv/kraken/search/streams?q=' . $q . '&limit=' . $limit . $clientId;
Expand All @@ -75,7 +78,7 @@ function searchStream( $q, $limit ) {
return false;
}

$data = json_decode($json)->streams;
$data = json_decode($json, false)->streams;
$streams = array();

if($data != null) {
Expand All @@ -97,6 +100,7 @@ function searchStream( $q, $limit ) {
* @param int $limit
*/
function searchGame( $q, $limit ) {

global $clientId;
//https://github.com/justintv/Twitch-API/blob/master/v3_resources/search.md
$url = 'https://api.twitch.tv/kraken/streams?game=' . $q . '&limit=' . $limit . '&live=true' . $clientId;
Expand All @@ -106,7 +110,7 @@ function searchGame( $q, $limit ) {
return false;
}

$data = json_decode($json)->streams;
$data = json_decode($json, false)->streams;
$streams = array();

if($data != null) {
Expand All @@ -126,6 +130,7 @@ function searchGame( $q, $limit ) {
*
*/
function getTwitchGames() {

global $clientId;
//https://github.com/justintv/Twitch-API/blob/master/v3_resources/games.md
$url = 'https://api.twitch.tv/kraken/games/top?limit=100' . $clientId;
Expand All @@ -135,7 +140,7 @@ function getTwitchGames() {
return false;
}

$data = json_decode($json)->top;
$data = json_decode($json, false)->top;
$games = array();

if($data != null) {
Expand All @@ -147,7 +152,7 @@ function getTwitchGames() {
}

/**
* functions checks if cover already exists
* Functions checks if cover already exists.
*
* @param String $game - game name
*/
Expand All @@ -168,7 +173,7 @@ function checkCover ( $game ) {
}

/**
* functions checks if cover already exists (twcover only)
* Functions checks if cover already exists (twcover only).
*
* @param String $game - game name
*/
Expand All @@ -183,7 +188,7 @@ function checkingCovers ( $game ) {
}

/**
* functions downloads cover from twitch.tv
* Functions downloads cover from Twitch.tv.
*
* @param String $game - game name
*/
Expand All @@ -192,21 +197,48 @@ function downloadCover ( $game ) {
$ok = false;
$url = "http://static-cdn.jtvnw.net/ttv-boxart/" . $game . "-92x128.jpg";

if( urlExists( $url ) ) {
$temp = "images/". $game .".jpg";
file_put_contents( $temp, fopen( $url , 'r'));
$ok = $game;
try
{
if( urlExists( $url ) ) {

/*$arrContextOptions=array(
"ssl"=>array(
"cafile" => "/bundle/cacert.pem", //https://curl.haxx.se/docs/caextract.html
"verify_peer"=> true,
"verify_peer_name"=> true,
),
);*/

$temp = "images/". $game .".jpg";
//file_put_contents( $temp, fopen( $url , 'r'));

//using curl instead now
$ch = curl_init($url);
$fp = fopen($temp, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

$ok = $game;
}
}
catch (Exception $e)
{
echo "Error: " . $e->getMessage();
}

return $ok;
}

/**
* functions checks if url exists
* Functions checks if url exists.
*
* @param String $game - game name
*/
function urlExists($file) {

$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
return false;
Expand All @@ -217,25 +249,39 @@ function urlExists($file) {
}

/**
* functions replaces file_get_contents (get content of url)
* Functions replaces file_get_contents (get content of url).
*
* @param String $url
*/
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec($ch);

curl_close($ch);
function url_get_contents ($url) {

return $output;
}
if (function_exists('curl_exec'))
{
$conn = curl_init($url);
//curl_setopt($ch, CURLOPT_HEADER, false);
//curl_setopt($conn, CURLOPT_SSLVERSION,3);
//curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($conn, CURLOPT_FRESH_CONNECT, true);
curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1);
$url_get_contents_data = (curl_exec($conn));
curl_close($conn);
}
elseif(function_exists('file_get_contents'))
{
$url_get_contents_data = file_get_contents($url);
}
elseif(function_exists('fopen') && function_exists('stream_get_contents'))
{
$handle = fopen ($url, "r");
$url_get_contents_data = stream_get_contents($handle);
}
else
{
$url_get_contents_data = false;
}

return $url_get_contents_data;
}

?>
2 changes: 1 addition & 1 deletion src/update.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": 1.75,
"version": 1.8,
"remote_json": "https://raw.githubusercontent.com/eusi/alfred2-twitch-streamer/master/current-version.json"
}
Binary file modified workflow/TwitchStreamer.alfredworkflow
Binary file not shown.

0 comments on commit 2e3c2fd

Please sign in to comment.