Skip to content

Commit

Permalink
Windows compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherdoyle committed Apr 15, 2022
1 parent ee76183 commit 5ad6c19
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 28 deletions.
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,38 @@ internal subscription system that requires a privacy violating Google account.
## Dependencies

YouTube\_RSS is developed and tested on Ubuntu Linux. It will probably work just fine in
similar Unix-like operating systems, but probably not on Windows (at least not without
a little bit of pain).

The following python modules are used and may need to be installed, e.g. using pip:

```
feedparser
urllib3
pysocks
```

If you want to get thumbnails for videos, you will additionally need to install the
module `ueberzug`, e.g. using pip.

The program also assumes that [mpv](https://github.com/mpv-player/mpv) is
installed in the environment. In, for example, Ubuntu, this can be accomplished
by running `sudo apt-get install mpv`. [youtube-dl](https://github.com/ytdl-org) also
needs to be installed (I use the latest version on their [official website](https://youtube-dl.org/), and
at the time of this writing, the version in the Ubuntu rebository seems too old to work
the way this project uses it).
### MPV & YouTube-DL

MPV and YouTube-DL are used to stream the video, ao. 2022 YT-DLP is a superior
choice to YouTube-DL, which may suffer buffering issues. MPV will automatically
use YT-DLP if it is installed, otherwise falling back to YT-DL.

#### Ubuntu

```commandline
sudo apt-get install mpv
```

[Install yt-dlp](https://github.com/yt-dlp/yt-dlp#installation=)

#### Windows

```commandline
choco install mpv
choco install yt-dlp
```


## Usage

Expand Down
22 changes: 21 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ authors = []
python = "^3.9"
feedparser = "^6.0.8"
requests = "^2.27.1"
windows-curses = "^2.3.0"

[tool.poetry.dev-dependencies]
pre-commit = "^2.18.1"
Expand Down
3 changes: 0 additions & 3 deletions youtube_rss/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ def main():
if not flag.treated:
raise command_line_parser.CommandLineParseError

CONFIG.YOUTUBE_RSS_DIR.mkdir(parents=True, exist_ok=True)
CONFIG.THUMBNAIL_DIR.mkdir(parents=True, exist_ok=True)

if not CONFIG.DATABASE_PATH.is_file():
logger.info("Initializing new database")
database = db.initialize_database()
Expand Down
20 changes: 18 additions & 2 deletions youtube_rss/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@
from pathlib import Path


def get_app_data_directory() -> Path:
if os.name == "nt":
app_data_dir = os.environ.get("APPDATA")
else:
app_data_dir = os.environ.get("XDG_DATA_HOME")

if app_data_dir is None:
raise OSError("Could not find app data directory")

return Path(app_data_dir)


class Config:
HOME = Path(os.environ.get("HOME"))
YOUTUBE_RSS_DIR = HOME / ".youtube_rss"
YOUTUBE_RSS_DIR = get_app_data_directory() / "youtube_rss"
THUMBNAIL_DIR = YOUTUBE_RSS_DIR / "thumbnails"
THUMBNAIL_SEARCH_DIR = THUMBNAIL_DIR / "search"
DATABASE_PATH = YOUTUBE_RSS_DIR / "database"
Expand All @@ -17,5 +28,10 @@ class Config:

USE_THUMBNAILS = False

def __init__(self) -> None:
self.YOUTUBE_RSS_DIR.mkdir(parents=True, exist_ok=True)
self.THUMBNAIL_DIR.mkdir(parents=True, exist_ok=True)
self.THUMBNAIL_SEARCH_DIR.mkdir(parents=True, exist_ok=True)


CONFIG = Config()
5 changes: 2 additions & 3 deletions youtube_rss/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os
import shutil
import signal
import subprocess
import threading
from typing import Optional

Expand Down Expand Up @@ -46,5 +46,4 @@ def get_thread_id(self):


def is_mpv_installed() -> bool:
proc = subprocess.run(["which", "mpv"])
return proc.returncode == 0
return shutil.which("mpv") is not None
24 changes: 15 additions & 9 deletions youtube_rss/youtube_rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ def handle_data(self, data):
self.is_script_tag = False
if "var ytInitialData" in data:
pattern = re.compile(
'"channelRenderer":{"channel_id":"([^"]+)",'
+ '"title":{"simpleText":"([^"]+)"'
# fmt: off
r'"channelRenderer":\{'
r'"channelId":"([^"]+)",'
r'"title":\{"simpleText":"([^"]+)"'
# fmt: on
)
tuple_list = pattern.findall(data)
result_list = []
Expand All @@ -80,6 +83,7 @@ def handle_data(self, data):
if self.is_script_tag:
self.is_script_tag = False
if "var ytInitialData" in data:
# fmt: off
pattern = re.compile(
r'videoId":"([^"]+)",'
r'"thumbnail":'
Expand All @@ -92,6 +96,7 @@ def handle_data(self, data):
r'"accessibility":\{"accessibilityData":\{"label":"([^"]+)"'
r'\}'
)
# fmt: on
tuple_list = pattern.findall(data)
result_list = []
for tup in tuple_list:
Expand Down Expand Up @@ -285,9 +290,9 @@ def get_channel_query_results(query, circuit_manager=None):
+ urllib.parse.quote(query)
+ "&sp=EgIQAg%253D%253D"
)
html_ceontent = get_http_content(url, circuit_manager=circuit_manager).text
html_content = get_http_content(url, circuit_manager=circuit_manager).text
parser = ChannelQueryParser()
parser.feed(html_ceontent)
parser.feed(html_content)
return parser.result_list


Expand Down Expand Up @@ -453,17 +458,18 @@ def refresh_subscription_by_channel_id(channel_id, local_feed, circuit_manager=N

# use this function to open a YouTube video url in mpv
def open_url_in_mpv(url, max_resolution=1080, circuit_manager=None):
command = []
command += [
command = [
"mpv",
f"--ytdl-format=bestvideo[height=?{max_resolution}]+bestaudio/best",
url,
]
command.append(url)

mpv_process = None
try:
# it is important to pipe outputs to NULL here otherwise output buffer will fill
# up
mpv_process = subprocess.Popen(
command, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT
command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
mpv_process.wait()
result = mpv_process.poll()
Expand Down Expand Up @@ -786,7 +792,7 @@ def play_video(video_url, circuit_manager=None):
circuit_manager=circuit_manager,
)
if result or not tui.yes_no_query(
"Something went wrong when playing the " + "video. Try again?"
"Something went wrong when playing the video. Try again?"
):
break
return result
Expand Down

0 comments on commit 5ad6c19

Please sign in to comment.