-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwimm.sh
176 lines (157 loc) · 4.4 KB
/
twimm.sh
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
language="日本語" # 日本語, Русский, English, Español...
dir="$HOME/.local/share/twimm"
instance="${TWIMM_INSTANCE:-https://api.safetwitch.eu.projectsegfau.lt}"
#instance="https://tta.femboy.band"
resolution="480p" # 1080p60, 720p60, 480p, 360p, 160p, audio_only
tocheck="$dir/tocheck"
towatch="$dir/towatch"
watched="$dir/watched"
last_parsed_file="$dir/last_parsed"
streamerlist=$(mktemp)
toparse=$(mktemp)
days_ago=$(date -d "3 days ago" +%s)
mkdir -p "$dir"
touch "$last_parsed_file"
if [ -z "$game_orig" ] ; then
game_orig="Just Chatting"
fi
usage() {
echo "Usage: $0 [OPTION]..."
echo "Automated twitch video parser"
echo
echo "Options:"
echo "-l Language of the streamer (Default: 日本語)"
echo "-r Resolution of the video (Default: 480p)"
echo "-g Game name (Default: Just Chatting)"
echo "-s Tags to find"
echo "-w Watch videos"
echo "-h Display this help and exit"
echo
echo "Examples:"
echo "$0 -s 'Argentina'"
echo " (to get watchlist of current streamers with Argentina tag)"
echo "$0 -l English -r 1080p60 -g 'Dota 2' -w"
echo " (to watch broadcast of the Dota 2 game)"
exit 1
}
addedtags() {
if [ -z "$addtags" ] ; then
addtags="$language"
fi
echo "$addtags" | sed 's/|/\n/g' | while IFS= read -r line ; do
curl -Ls "$instance/api/search/?query=$line" | jq -r '.data.channelsWithTag[] | select(.followers > 1000) | .username' >> "$streamerlist"
done
}
process_parsing() {
local line=$1
listofstreamers=$(curl -Ls "$instance/api/users/$line" | jq -r --arg jqLanguage "$language" '.data | select(.stream.tags[] | contains($jqLanguage)) | [ .login, (.stream.tags | join(", "))] | @tsv')
if [ -z "$listofstreamers" ] ; then
true
else
printf "%s " "$line"
printf "%s\t%s\n" "$listofstreamers" "$language" >> "$tocheck"
fi
}
parsingstreamers() {
export -f process_parsing
export instance
export tocheck
export language
echo
echo "Parsing streamers..."
xargs -P 8 -I {} bash -c 'process_parsing "{}"' < "$streamerlist"
awk -i inplace -F"\t" '!x[$1]++' "$tocheck"
}
process_video() {
local all=$(grep "^$1[[:blank:]]" "$tocheck")
local line=$(echo "$all" | awk -F'\t' '{print $1}')
last_parsed=$(grep "^$line " "$last_parsed_file" | awk '{print $2}')
if [ -z "$last_parsed" ] ; then
last_parsed=0
fi
if [ "$last_parsed" -ge "$days_ago" ]; then
printf "%s " "-$line"
else
local tags=$(echo "$all"| awk -F'\t' '{print $2}')
local lang=$(echo "$all" | awk -F'\t' '{print $3}')
listofvids=$(curl -Ls "$instance/api/vods/shelve/$line" | jq -r '.data[] | select(.title == "Recent broadcasts") | .videos[] | select(.duration > 10000) | {id: .id, game: .game.name, login: .streamer.login} | [.id, .game, .login] | @tsv')
if [ -z "$listofvids" ] ; then
true
else
printf "%s " "$line"
echo "$listofvids" | sed "s/$/\t$tags\t$lang/" >> "$towatch"
fi
echo "$line $(date +%s)" >> "$last_parsed_file"
fi
}
addtowatch() {
export -f process_video
export last_parsed_file
export towatch
export tocheck
export instance
export days_ago
echo
echo "Adding streamer recordings"
comm -23 <(awk '{print $1}' "$tocheck" | sort) <(awk '{print $1}' "$last_parsed_file" | sort) >> "$toparse"
awk -v days_ago="$days_ago" '$2 < days_ago {print $1}' "$last_parsed_file" >> "$toparse"
sort -u "$toparse" -o "$toparse"
xargs -P 8 -I {} bash -c 'process_video "{}"' < "$toparse"
sort -rnk2,2 "$last_parsed_file" -o "$last_parsed_file"
awk -i inplace '!x[$1]++' "$last_parsed_file"
awk -i inplace -F"\t" '!x[$1]++' "$towatch"
}
watch() {
while true ; do
watchnow=$(cat "$towatch" | grep "${language}$" | grep -i "$addtags" | grep "$game_orig" | shuf -n1)
if [ -z "$watchnow" ] ; then
break
fi
if grep -q "$watchnow" "$watched" ; then
sed -i "/$watchnow/d" "$towatch"
continue
fi
video=$(echo "$watchnow" | awk -F'\t' '{print $1}')
link=$(curl -Ls "$instance/proxy/vod/$video/video.m3u8" | grep -A2 "NAME=.$resolution" | tail -n1)
if [ -z "$link" ] ; then
continue
fi
mpv --start=15:00 "$link"
sed -i "/^$video\t/d" "$towatch"
echo "$watchnow" >> "$watched"
echo "Ctrl-C to exit... 10 sec"
sleep 10
done
}
while getopts 'l:r:g:s:wh' OPTION; do
case "$OPTION" in
l)
language="$OPTARG"
;;
r)
resolution="$OPTARG"
;;
g)
game_orig="$OPTARG"
;;
s)
addtags="$OPTARG"
;;
w)
watch ;
exit
;;
h)
usage
exit
;;
*)
usage
exit
;;
esac
done
addedtags
parsingstreamers
addtowatch