aboutsummaryrefslogtreecommitdiff
path: root/main.py
blob: b8008c859127e72f11c984df8ec07e482ef209d3 (plain)
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
#!/usr/bin/python

import gzip
import re
import subprocess
import sys
import urllib.request
# NO EXTERNAL DEPENDENCIES YAY
# Wait does mpv count???

# I hate this website
url = 'https://www.bilibili.com/video/'

# Gimme a video ID
cur = sys.argv[1]
while True:
    # First start mpv in the background
    # Also enable subtitles because why not
    proc = subprocess.Popen(['mpv', '--no-video', '--slang=zh-CN', url + cur])
    # The try finally block is to ensure that proc gets killed no matter what
    try:
        # I could use the Python requests library...
        # Nope, external dependencies suck, so enjoy this abomination
        body = gzip.decompress(urllib.request.urlopen(url + cur).read()).decode('utf-8')
        # Sometimes I forget what I'm even listening to
        # I WILL parse HTML with regex, you can't stop me
        print('NOW PLAYING', url + cur, re.search('"true">(.*?)<', body).group(1))
        # Get recommended stuff
        # I figured out the regexes by playing around with curl --compressed and then grepping 
        lines = body.split('\n')
        rec = [
            (
                re.search('/video/(.*?)/', line).group(1), # ID
                re.search('title="(.*?)"', line).group(1), # Video name
                re.search('"name">(.*?)<', line).group(1), # up主 name
                lines[i + 1].strip() # View count
            )
            for i, line in enumerate(lines) if 'recommend_more' in line
        ]
        # Print it out the smart way!
        # Who needs loops smh
        print(*enumerate(rec), sep='\n')
        proc.wait()
    finally:
        # Kill mpv with fire because sometimes it likes the music too much and refuses to stop if you ask nicely
        proc.kill()
    # Choose a recommended video
    # If you don't like any, just stop the app, burn your computer, and try again
    cur = rec[int(input("Choose next: "))][0]
    # Give instant feedback so it doesn't look like the app is hanging
    print(cur)
    # I have more comments than code so I must be doing something right right?
    # Right???