Spaces:
Runtime error
Runtime error
Download parser.py from GungnirAP/Youtube-Comments: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/spaces/GungnirAP/Youtube-Comments/resolve/main/parser.py
- Command line
-
hf download hf://spaces/GungnirAP/Youtube-Comments/parser.py
-
curl -L -o parser.py https://huggingface.co/spaces/GungnirAP/Youtube-Comments/resolve/main/parser.py
1.38 kB
| import requests | |
| from requests.exceptions import MissingSchema | |
| def extract_titles(url = "https://www.youtube.com/watch?v=CuSxk_DNau8&ab_channel=PeppaPigParodies"): | |
| try: | |
| page = requests.get(url) | |
| except MissingSchema as e: | |
| message = e.args[0] | |
| substr = '''No scheme supplied. ''' | |
| message = message[:message.find(substr)] + message[message.find(substr) + len(substr):] | |
| raise RuntimeError(message) | |
| except Exception as e: | |
| message = f'''Invalid URL '{url}'. Please, try another one.''' | |
| raise RuntimeError(message) | |
| if not page.ok: | |
| message = '''This video isn't available.''' | |
| raise RuntimeError(message) | |
| try: | |
| txt = page.text | |
| title_start = '''meta name="title" content="''' | |
| pos = txt.find(title_start) | |
| if pos == -1: | |
| raise RuntimeError | |
| title = txt[pos + len(title_start):] | |
| title = title[:title.find('"')] | |
| channel_start = '''<link itemprop="name" content="''' | |
| pos = txt.find(channel_start) | |
| if pos == -1: | |
| raise RuntimeError | |
| channel = txt[pos + len(channel_start):] | |
| channel = channel[:channel.find('"')] | |
| except Exception as e: | |
| message = '''Can't detect channel or video title.\nPlease, try another link.''' | |
| raise RuntimeError(message) | |
| return channel, title |