Skip to content

Commit

Permalink
playlist view
Browse files Browse the repository at this point in the history
  • Loading branch information
citelao committed Jun 4, 2017
1 parent 2337e66 commit 1e93ae1
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ You must install dependencies manually. Install
proceed with development! (if you do not want to do a global install, run
`php composer.phar install` from this directory)

Command line development for Alfred 3 is currently not working, since we
depend on several environment variables. Eventually, if you are developing for
Alfred version 3 and want to run these files from
the command line (`php -f main.php -- "args"`), you will need to run in debug
mode : `DEBUG=true php -f main.php -- "args"`.

## TODO ##

- Allow ``, `^`, and `` to function as modifiers (Open in Spotify, other things?).
Expand Down
2 changes: 1 addition & 1 deletion src/citelao/OhAlfred/OhAlfred.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function workflow()
// Get the cache directory
public function cache() {
if($this->cache == null) {
if(isset($_ENV['alfred_workflow_data'])) {
if(isset($_ENV['alfred_workflow_cache'])) {
$this->cache = $_ENV['alfred_workflow_cache'] . "/";
} else {
$this->cache = $this->home() . "/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/" . $this->name() . "/";
Expand Down
13 changes: 10 additions & 3 deletions src/citelao/Spotifious/Menus/Detail.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
namespace Spotifious\Menus;

use OhAlfred\Exceptions\StatefulException;
use Spotifious\Menus\Menu;
use Spotifious\Menus\DetailArtist;
use Spotifious\Menus\DetailAlbum;
use Spotifious\Menus\DetailPlaylist;

class Detail {
protected $submenu;
Expand All @@ -13,20 +15,25 @@ public function __construct($options, $alfred, $api) {

$this->currentURI = $options['URIs'][$options['depth'] - 1];
$explodedURI = explode(":", $this->currentURI);
$this->type = $explodedURI[1];
$this->type = $explodedURI[count($explodedURI) - 2];

$constructedOptions = array(
'currentURI' => $this->currentURI,
'search' => $options['search'],
'id' => $explodedURI[2],
'id' => $explodedURI[count($explodedURI) - 1],
'originalQuery' => $options['query'],
'query' => implode("", $options['args'])
);

if($this->type == "artist") {
$this->submenu = new DetailArtist($constructedOptions, $alfred, $api);
} else {
} else if($this->type == "album") {
$this->submenu = new DetailAlbum($constructedOptions, $alfred, $api);
} else if($this->type == "playlist") {
$this->submenu = new DetailPlaylist($constructedOptions, $alfred, $api);
} else {
throw new StatefulException("Unknown detail type: '${$this->type}'");

}
}

Expand Down
127 changes: 127 additions & 0 deletions src/citelao/Spotifious/Menus/DetailPlaylist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
namespace Spotifious\Menus;

use OhAlfred\Exceptions\StatefulException;
use OhAlfred\HTTP\JsonFetcher;
use OhAlfred\OhAlfred;

class DetailPlaylist {
protected $alfred;

protected $currentURI;
protected $query;
protected $originalQuery;
protected $search;

protected $name;
protected $type;
protected $tracks;

public function __construct($options, $alfred, $api) {
$this->alfred = $alfred;
$locale = $this->alfred->options('country');

$this->currentURI = $options['currentURI'];
$this->query = $options['query'];
$this->originalQuery = $options['originalQuery'];
$this->search = $options['search'];

$explodedURI = explode(":", $this->currentURI);
$user = $explodedURI[count($explodedURI) - 3];

if(!$api) {
throw new StatefulException("You should have an API at this point");
} else {
$json = $api->getUserPlaylist($user, $options['id']);
}

$this->name = $json->name;
$this->type = $json->type;

$this->tracks = array();
foreach ($json->tracks->items as $key => $value) {
$this->tracks[] = array(
'uri' => $value->track->uri,
'name' => $value->track->name,
'type' => $value->track->type,
'number' => $key + 1,
'duration' => $value->track->duration_ms,
'explicit' => ($value->track->explicit == 'true')
);
}

if($json->tracks->total > $json->tracks->limit) {
$this->overflow = true;
$this->overflow_count = $json->tracks->total - $json->tracks->limit;
}
}

public function output() {
$results = array();

foreach ($this->tracks as $key => $current) {
$explicit = $current['explicit'] ? " (explicit)" : "";

$currentResult = array(
'title' => "{$current['number']}. {$current['name']}",
'subtitle' => $this->prettifyTime($current['duration']) . $explicit,
'valid' => true,
'arg' => "spotify⟩play track \"{$current['uri']}\" in context \"{$this->currentURI}\"",
'copy' => $current['uri'],
'icon' => array('path' => "include/images/track.png")
);

if($this->search != '' && !mb_stristr($currentResult['title'], $this->search))
continue;

$results[] = $currentResult;
}

if($this->overflow) {
$overflow = array(
'title' => 'This is a large playlist',
'subtitle' => ($this->overflow_count === 1)
? "1 song could not be displayed; you can view it in Spotify."
: "$this->overflow_count songs could not be displayed; you can view them in Spotify.",
'arg' => "spotify⟩activate (open location \"{$this->currentURI}\")",
'autocomplete' => $this->originalQuery,
'copy' => $this->currentURI,
'icon' => array('path' => "include/images/info.png")
);
}

$scope['title'] = $this->name;
$scope['subtitle'] = "Browse this {$this->type} in Spotify";
$scope['arg'] = "spotify⟩activate (open location \"{$this->currentURI}\")";
$scope['autocomplete'] = $this->originalQuery;
$scope['copy'] = $this->currentURI;
$scope['icon'] = array('path' => "include/images/{$this->type}.png");

if ($this->search == null) {
if($this->overflow) {
array_unshift($results, $overflow);
}
array_unshift($results, $scope);
} else {
array_push($results, $scope);
if($this->overflow) {
array_push($results, $overflow);
}
}

return $results;
}

protected function prettifyTime($time_ms) {
$secondsForm = $time_ms / 1000;

$seconds = $secondsForm % 60;
$minutes = floor($secondsForm / 60);

$seconds_string = ($seconds < 10)
? "0" . $seconds
: $seconds;

return $minutes . ":" . $seconds_string;
}
}
9 changes: 4 additions & 5 deletions src/citelao/Spotifious/Menus/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Spotifious\Menus\Helper;
use OhAlfred\HTTP\JsonFetcher;
use OhAlfred\HTTP\JsonParser;
use OhAlfred\Exceptions\StatefulException;
use OhAlfred\OhAlfred;

class Search implements Menu {
Expand Down Expand Up @@ -152,10 +151,10 @@ public function output() {
$valid = true;
$arg = "spotify⟩play track \"{$current['uri']}\"";
$autocomplete = '';
} else if($current['type'] == 'playlist') {
$valid = true;
$arg = "spotify⟩play track \"{$current['uri']}\"";
$autocomplete = '';
// } else if($current['type'] == 'playlist') {
// $valid = true;
// $arg = "spotify⟩play track \"{$current['uri']}\"";
// $autocomplete = '';
} else {
$valid = false;
$arg = '';
Expand Down

0 comments on commit 1e93ae1

Please sign in to comment.