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
|
|
|
public static function action() |
15
|
|
|
{ |
16
|
|
|
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
|
1 |
|
public static function download($torrentPageLink = '') |
34
|
|
|
{ |
35
|
1 |
|
$magnetLink = self::findMagnetLinkOn($torrentPageLink); |
36
|
|
|
|
37
|
1 |
|
system("open $magnetLink 2>&1", $result); |
38
|
|
|
|
39
|
1 |
|
return $result === 0; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function copy($torrentPageLink = '') |
43
|
|
|
{ |
44
|
|
|
$magnetLink = self::findMagnetLinkOn($torrentPageLink); |
45
|
|
|
|
46
|
|
|
system("echo '$magnetLink' | pbcopy", $result); |
47
|
|
|
|
48
|
|
|
return $result === 0; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
protected static function findMagnetLinkOn($torrentPageLink = '') |
52
|
|
|
{ |
53
|
1 |
|
$crawler = (new Client())->request('GET', getenv('url') . $torrentPageLink); |
54
|
|
|
|
55
|
1 |
|
return $crawler->filter('#tab-technical a.siteButton.giantButton')->attr('href'); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public static function notifyDownload($torrentName = '') |
59
|
|
|
{ |
60
|
1 |
|
return '"' . $torrentName . '" will soon be at home!'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function notifyCopy($torrentName = '') |
64
|
|
|
{ |
65
|
|
|
return 'Magnet link for "' . substr($torrentName, 0, 30) . '..." has been copied to clipboard!'; |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
protected static function searchOnlineFor($term) |
69
|
|
|
{ |
70
|
3 |
|
$crawler = (new Client())->request('GET', getenv('url') . '/usearch/' . urlencode($term)); |
71
|
|
|
|
72
|
3 |
|
return $crawler->filter('.frontPageWidget tr'); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
protected static function menuFor(Crawler $torrents) |
76
|
|
|
{ |
77
|
3 |
|
if ($torrents->count()) { |
78
|
|
|
$torrents->nextAll()->each(function ($row) { |
79
|
2 |
|
ScriptFilter::add( |
80
|
2 |
|
Item::create() |
81
|
2 |
|
->title(TorrentMenuItemBuilder::title($row)) |
82
|
2 |
|
->subtitle(TorrentMenuItemBuilder::subtitle($row)) |
83
|
2 |
|
->icon(Icon::create("resources/icons/magnet.png")) |
84
|
2 |
|
->arg('download') |
85
|
2 |
|
->variable('torrent_page_link', TorrentMenuItemBuilder::pageLink($row)) |
86
|
2 |
|
->variable('torrent_name', TorrentMenuItemBuilder::subtitle($row)) |
87
|
2 |
|
->mod( |
88
|
2 |
|
Cmd::create() |
89
|
2 |
|
->arg('copy') |
90
|
2 |
|
->subtitle('Copy magnet link') |
91
|
2 |
|
->variable('torrent_page_link', TorrentMenuItemBuilder::pageLink($row)) |
92
|
2 |
|
->variable('torrent_name', TorrentMenuItemBuilder::subtitle($row)) |
93
|
|
|
) |
94
|
|
|
); |
95
|
2 |
|
}); |
96
|
|
|
} else { |
97
|
1 |
|
ScriptFilter::add( |
98
|
1 |
|
Item::create() |
99
|
1 |
|
->title('404 for ' . self::userInput() . ' ☹️') |
100
|
1 |
|
->subtitle('Try some other terms maybe?') |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
return ScriptFilter::output(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|