aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Wang2023-05-31 16:53:55 -0500
committerAnthony Wang2023-05-31 16:53:55 -0500
commita1998c647aa51ceb0e7a9f83d364176d9074f9e8 (patch)
tree72140ba4fa659b9b9ebdad623dbfcfcb7458d847
parent30934b6326fded3db0c8181164522b4661ca4731 (diff)
Fun stuff
-rwxr-xr-x[-rw-r--r--]main.py18
1 files changed, 14 insertions, 4 deletions
diff --git a/main.py b/main.py
index 6ce8d21..ba771e8 100644..100755
--- a/main.py
+++ b/main.py
@@ -1,3 +1,5 @@
+#!/usr/bin/python
+
import gzip
import re
import subprocess
@@ -6,31 +8,39 @@ 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
# Make sure mpv doesn't take in any input
# Also enable subtitles because why not
- proc = subprocess.Popen(['mpv', '--no-video', '--no-input-default-bindings', '--slang=zh-CN', 'https://www.bilibili.com/video/' + cur])
+ proc = subprocess.Popen(['mpv', '--no-video', '--no-input-default-bindings', '--slang=zh-CN', url + cur])
+ # The try finally block is to ensure that proc gets killed no matter what
try:
# I could use requests...
# Nope, external dependencies suck, so enjoy this abomination
- body = gzip.decompress(urllib.request.urlopen('https://www.bilibili.com/video/' + cur).read()).decode('utf-8')
+ 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(cur, re.search('<title data-vue-meta="true">(.*?)<', body).group(1))
+ print('NOW PLAYING', cur, re.search('<title data-vue-meta="true">(.*?)<', body).group(1))
# Get recommended stuff
rec = [
(re.search('/video/(.*?)/', line).group(1), re.search('title="(.*?)"', line).group(1))
for line in body.split('\n') if 'recommend_more' in line
]
# Print it out the smart way!
+ # Who needs loops smh
print(*enumerate(rec), sep='\n')
# Choose a recommended video
# If you don't like any, just stop the app, burn your computer, and try again
cur = rec[int(input())][0]
- print(cur)
finally:
# Kill mpv with fire because sometimes it likes the music too much and refuses to stop if you ask nicely
proc.kill()
+ # 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???