1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Godbout\Alfred\Kat; |
4
|
|
|
|
5
|
|
|
use Goutte\Client; |
6
|
|
|
use Godbout\Alfred\Workflow\BaseWorkflow; |
7
|
|
|
|
8
|
|
|
class Workflow extends BaseWorkflow |
9
|
|
|
{ |
10
|
9 |
|
public static function do() |
11
|
|
|
{ |
12
|
9 |
|
$action = getenv('action'); |
13
|
|
|
|
14
|
9 |
|
if (method_exists(static::class, $action)) { |
15
|
6 |
|
return static::$action(getenv('torrent_page_link')); |
16
|
|
|
} |
17
|
|
|
|
18
|
3 |
|
return false; |
19
|
|
|
} |
20
|
|
|
|
21
|
12 |
|
public static function notify($result = false) |
22
|
|
|
{ |
23
|
12 |
|
$action = getenv('action'); |
24
|
12 |
|
$torrentName = getenv('torrent_name'); |
25
|
|
|
|
26
|
12 |
|
if ($action === 'download') { |
27
|
6 |
|
return self::notifyDownload($torrentName); |
28
|
|
|
} |
29
|
|
|
|
30
|
9 |
|
if ($action === 'copy') { |
31
|
6 |
|
return self::notifyCopy($torrentName); |
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
return 'huh. what did you do?!'; |
35
|
|
|
} |
36
|
|
|
|
37
|
6 |
|
protected static function download($torrentPageLink = '') |
38
|
|
|
{ |
39
|
6 |
|
$magnetLink = self::findMagnetLinkOn($torrentPageLink); |
40
|
|
|
|
41
|
6 |
|
if (getenv('cli') !== false) { |
42
|
3 |
|
return self::downloadThroughCliCommand($magnetLink); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
return self::downloadThroughDefaultApplication($magnetLink); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
protected static function downloadThroughCliCommand($magnetLink = '') |
49
|
|
|
{ |
50
|
3 |
|
system(str_replace('{magnet}', $magnetLink, getenv('cli')), $result); |
51
|
|
|
|
52
|
3 |
|
return $result === 0; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
protected static function downloadThroughDefaultApplication($magnetLink = '') |
56
|
|
|
{ |
57
|
3 |
|
system("open $magnetLink 2>&1", $result); |
58
|
|
|
|
59
|
3 |
|
return $result === 0; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected static function copy($torrentPageLink = '') |
63
|
|
|
{ |
64
|
|
|
$magnetLink = self::findMagnetLinkOn($torrentPageLink); |
65
|
|
|
|
66
|
|
|
system("echo '$magnetLink' | pbcopy 2>&1", $result); |
67
|
|
|
|
68
|
|
|
return $result === 0; |
69
|
|
|
} |
70
|
|
|
|
71
|
6 |
|
protected static function findMagnetLinkOn($torrentPageLink = '') |
72
|
|
|
{ |
73
|
6 |
|
$crawler = (new Client())->request('GET', getenv('url') . $torrentPageLink); |
74
|
|
|
|
75
|
6 |
|
return $crawler->filter('#tab-technical a.siteButton.giantButton')->attr('href'); |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
protected static function notifyDownload($torrentName = '') |
79
|
|
|
{ |
80
|
6 |
|
return '"' . $torrentName . '" will soon be at home!'; |
81
|
|
|
} |
82
|
|
|
|
83
|
6 |
|
protected static function notifyCopy($torrentName = '') |
84
|
|
|
{ |
85
|
6 |
|
return 'Magnet link for "' . substr($torrentName, 0, 30) . '..." has been copied to clipboard!'; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|