This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pin.php
88 lines (77 loc) · 2.26 KB
/
pin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
require('telegram.php');
$list = trim(file_get_contents('now-playing'));
$queue = explode("\n", $list);
$count = count($queue);
/* Warm up */
if ($argv[1] == 'warm') {
[$vid, $title] = explode(' ', $list, 2);
TG('sendMessage', [
'chat_id' => ChatID,
'text' => "歌單空了,從點歌記錄抓一首來聽聽吧 😆\n\n<a href='https://www.youtube.com/watch?v=$vid'>$title</a>",
'parse_mode' => 'HTML',
]);
$result = TG('getChat', [
'chat_id' => ChatID
]);
if ($result['result']['pinned_message'])
TG('unpinChatMessage', [
'chat_id' => ChatID,
]);
}
/* Metadata of current playing list */
$payload = json_encode([
'command' => [
'get_property',
'playlist-pos'
]
]);
$data = json_decode(shell_exec("echo '$payload' | socat - ./socket 2> /dev/null"), true) ?? 0;
$pos = $data['data']; // Current position on playlist
$payload = json_encode([
'command' => [
'get_property',
'percent-pos'
]
]);
$data = json_decode(shell_exec("echo '$payload' | socat - ./socket"), true);
$percent_pos = (int) $data['data']; // Position in current file (percentage)
if ($pos == 0 && $percent_pos < 30) // Just sent
$text = "準備播放下一輪歌曲 (共 $count 首)";
else
$text = "正在播放 $count 首歌曲";
/* Generate list message */
foreach ($queue as $i => $line) {
if (!($i % 5)) // Every 5 songs
$text .= "\n"; // New paragraph
[$vid, $title] = explode(' ', $line, 2);
if ($i == $pos) { // Now Playing
$text .= "\n$i. <a href='https://youtube.com/watch?v=$vid#" . microtime() % 100 . "'>" . enHTML("$title") . '</a>';
if ($percent_pos <= 95) // No playing progress for last 5% of song
$text .= " ($percent_pos%)";
} else
$text .= enHTML("\n$i. $title");
}
if ($argv[1] == 'send') {
$result = TG('sendMessage', [
'chat_id' => ChatID,
'text' => mb_substr($text, 0, 4000),
'parse_mode' => 'HTML',
]);
$msg_id = $result['result']['message_id'];
TG('pinChatMessage', [
'chat_id' => ChatID,
'message_id' => $msg_id,
'disable_notification' => true
]);
file_put_contents('pin-id', $msg_id);
} else if ($argv[1] == 'edit') {
$msg_id = file_get_contents('pin-id');
if ($msg_id)
$result = TG('editMessageText', [
'chat_id' => ChatID,
'message_id' => $msg_id,
'text' => mb_substr($text, 0, 4000),
'parse_mode' => 'HTML',
]);
}