|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Godbout\Alfred\Kat; |
|
4
|
|
|
|
|
5
|
|
|
use Goutte\Client; |
|
6
|
|
|
use Godbout\Alfred\Workflow\Icon; |
|
7
|
|
|
use Godbout\Alfred\Workflow\Item; |
|
8
|
|
|
use Godbout\Alfred\Workflow\Mods\Cmd; |
|
9
|
|
|
use Godbout\Alfred\Workflow\ScriptFilter; |
|
10
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
11
|
|
|
|
|
12
|
|
|
class Workflow |
|
13
|
|
|
{ |
|
14
|
1 |
|
public static function action() |
|
15
|
|
|
{ |
|
16
|
1 |
|
return getenv('action'); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
1 |
|
public static function userInput() |
|
20
|
|
|
{ |
|
21
|
1 |
|
global $argv; |
|
22
|
|
|
|
|
23
|
1 |
|
return trim($argv[1] ?? ''); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
3 |
|
public static function resultsFor($term = '') |
|
27
|
|
|
{ |
|
28
|
3 |
|
$torrents = self::searchOnlineFor($term); |
|
29
|
|
|
|
|
30
|
3 |
|
return self::menuFor($torrents); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
public static function download($torrentPageLink = '') |
|
34
|
|
|
{ |
|
35
|
2 |
|
$magnetLink = self::findMagnetLinkOn($torrentPageLink); |
|
36
|
|
|
|
|
37
|
2 |
|
if (getenv('cli') !== false) { |
|
38
|
1 |
|
return self::downloadThroughCliCommand($magnetLink); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
return self::downloadThroughDefaultApplication($magnetLink); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
protected static function downloadThroughCliCommand($magnetLink = '') |
|
45
|
|
|
{ |
|
46
|
1 |
|
system(str_replace('{magnet}', $magnetLink, getenv('cli')), $result); |
|
47
|
|
|
|
|
48
|
1 |
|
return $result === 0; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
protected static function downloadThroughDefaultApplication($magnetLink = '') |
|
52
|
|
|
{ |
|
53
|
1 |
|
system("open $magnetLink 2>&1", $result); |
|
54
|
|
|
|
|
55
|
1 |
|
return $result === 0; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function copy($torrentPageLink = '') |
|
59
|
|
|
{ |
|
60
|
|
|
$magnetLink = self::findMagnetLinkOn($torrentPageLink); |
|
61
|
|
|
|
|
62
|
|
|
system("echo '$magnetLink' | pbcopy 2>&1", $result); |
|
63
|
|
|
|
|
64
|
|
|
return $result === 0; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
protected static function findMagnetLinkOn($torrentPageLink = '') |
|
68
|
|
|
{ |
|
69
|
2 |
|
$crawler = (new Client())->request('GET', getenv('url') . $torrentPageLink); |
|
70
|
|
|
|
|
71
|
2 |
|
return $crawler->filter('#tab-technical a.siteButton.giantButton')->attr('href'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public static function notifyDownload($torrentName = '') |
|
75
|
|
|
{ |
|
76
|
1 |
|
return '"' . $torrentName . '" will soon be at home!'; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
public static function notifyCopy($torrentName = '') |
|
80
|
|
|
{ |
|
81
|
1 |
|
return 'Magnet link for "' . substr($torrentName, 0, 30) . '..." has been copied to clipboard!'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
protected static function searchOnlineFor($term) |
|
85
|
|
|
{ |
|
86
|
3 |
|
$crawler = (new Client())->request('GET', getenv('url') . '/usearch/' . urlencode($term)); |
|
87
|
|
|
|
|
88
|
3 |
|
return $crawler->filter('.frontPageWidget tr'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
protected static function menuFor(Crawler $torrents) |
|
92
|
|
|
{ |
|
93
|
3 |
|
if ($torrents->count()) { |
|
94
|
|
|
$torrents->nextAll()->each(function ($row) { |
|
95
|
2 |
|
ScriptFilter::add( |
|
96
|
2 |
|
Item::create() |
|
97
|
2 |
|
->title(TorrentMenuItemBuilder::title($row)) |
|
98
|
2 |
|
->subtitle(TorrentMenuItemBuilder::subtitle($row)) |
|
99
|
2 |
|
->icon(Icon::create("resources/icons/magnet.png")) |
|
100
|
2 |
|
->arg('download') |
|
101
|
2 |
|
->variable('torrent_page_link', TorrentMenuItemBuilder::pageLink($row)) |
|
102
|
2 |
|
->variable('torrent_name', TorrentMenuItemBuilder::subtitle($row)) |
|
103
|
2 |
|
->mod( |
|
104
|
2 |
|
Cmd::create() |
|
105
|
2 |
|
->arg('copy') |
|
106
|
2 |
|
->subtitle('Copy magnet link') |
|
107
|
2 |
|
->variable('torrent_page_link', TorrentMenuItemBuilder::pageLink($row)) |
|
108
|
2 |
|
->variable('torrent_name', TorrentMenuItemBuilder::subtitle($row)) |
|
109
|
|
|
) |
|
110
|
|
|
); |
|
111
|
2 |
|
}); |
|
112
|
|
|
} else { |
|
113
|
1 |
|
ScriptFilter::add( |
|
114
|
1 |
|
Item::create() |
|
115
|
1 |
|
->title('404 for ' . self::userInput() . ' ☹️') |
|
116
|
1 |
|
->subtitle('Try some other terms maybe?') |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
3 |
|
return ScriptFilter::output(); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|