Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Commit

Permalink
feat(yt-dl): automatically gather youtube cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Nov 21, 2023
1 parent 3180e9c commit c7f7e05
Show file tree
Hide file tree
Showing 8 changed files with 521 additions and 16 deletions.
15 changes: 15 additions & 0 deletions Contents/Code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

# standard imports
import inspect
import os
import re
import sys

# plex debugging
try:
Expand Down Expand Up @@ -40,6 +42,19 @@
except Exception as e:
Log.Exception("Failed to bypass RestrictedPython: {}".format(e))

# change priority of Contents/Libraries/Shared to be first
# this is required to import versions of libraries that we specify in our requirements.txt file
# instead of what Plex provides
try:
# sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'Libraries', 'Shared'))
import selenium
getattr(selenium, "__file__")
# todo - determine the best way to move our libraries to the top of the path
for path in sys.path:
Log.Debug("sys.path: {}".format(path))
except Exception as e:
Log.Exception("Failed to add Contents/Libraries/Shared to sys.path: {}".format(e))

Check warning on line 56 in Contents/Code/__init__.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/__init__.py#L55-L56

Added lines #L55 - L56 were not covered by tests

# local imports
from default_prefs import default_prefs
from constants import contributes_to, version
Expand Down
112 changes: 101 additions & 11 deletions Contents/Code/general_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
from plexhints.log_kit import Log # log kit
from plexhints.prefs_kit import Prefs # prefs kit

# imports from Libraries\Shared
from plexapi.base import PlexPartialObject
import requests
from typing import Optional

# local imports
from constants import metadata_base_directory, metadata_type_map, themerr_data_directory
Expand All @@ -27,15 +31,15 @@


def get_media_upload_path(item, media_type):
# type: (any, str) -> str
# type: (PlexPartialObject, str) -> str
"""
Get the path to the theme upload directory.
Get the hashed path of the theme upload directory for the item specified by the ``item``.
Parameters
----------
item : any
item : PlexPartialObject
The item to get the theme upload path for.
media_type : str
The media type to get the theme upload path for. Must be one of 'art', 'posters', or 'themes'.
Expand Down Expand Up @@ -75,15 +79,15 @@ def get_media_upload_path(item, media_type):


def get_themerr_json_path(item):
# type: (any) -> str
# type: (PlexPartialObject) -> str
"""
Get the path to the Themerr data file.
Get the path to the Themerr data file for the item specified by the ``item``.
Parameters
----------
item : any
item : PlexPartialObject
The item to get the Themerr data file path for.
Returns
Expand All @@ -102,7 +106,7 @@ def get_themerr_json_path(item):


def get_themerr_json_data(item):
# type: (any) -> dict
# type: (PlexPartialObject) -> dict
"""
Get the Themerr data for the specified item.
Expand All @@ -111,7 +115,7 @@ def get_themerr_json_data(item):
Parameters
----------
item : any
item : PlexPartialObject
The item to get the Themerr data for.
Returns
Expand Down Expand Up @@ -153,16 +157,102 @@ def get_themerr_settings_hash():
return settings_hash


def get_user_country_code(ip_address=None):
# type: (Optional[str]) -> str
"""
Get the country code for the user with the given IP address.
Parameters
----------
ip_address : Optional[str]
The IP address of the user.
Returns
-------
str
The `ALPHA-2 <https://www.iban.com/country-codes>`__ country code for the user with the given IP address.
Examples
--------
>>> get_user_country_code()
'US'
"""
api_url = 'https://ipinfo.io/json'
if ip_address:
api_url = 'https://ipinfo.io/{}/json'.format(ip_address)

Check warning on line 182 in Contents/Code/general_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/general_helper.py#L180-L182

Added lines #L180 - L182 were not covered by tests

try:
response = requests.get(api_url)
data = response.json()
return data.get('country').encode('utf-8')
except requests.RequestException as e:
Log.Error("Could not determine user country: {}".format(e))

Check warning on line 189 in Contents/Code/general_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/general_helper.py#L184-L189

Added lines #L184 - L189 were not covered by tests


def is_user_in_eu(ip_address=None):
# type: (Optional[str]) -> bool
"""
Check if the user with the given IP address is in the European Union.
Parameters
----------
ip_address : Optional[str]
The IP address of the user.
Returns
-------
bool
True if the user with the given IP address is in the European Union, False otherwise.
Examples
--------
>>> is_user_in_eu()
False
"""
eu_countries = [

Check warning on line 212 in Contents/Code/general_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/general_helper.py#L212

Added line #L212 was not covered by tests
'AT', # Austria
'BE', # Belgium
'BG', # Bulgaria
'CY', # Cyprus
'CZ', # Czech Republic
'DE', # Germany
'DK', # Denmark
'EE', # Estonia
'ES', # Spain
'FI', # Finland
'FR', # France
'GR', # Greece
'HR', # Croatia
'HU', # Hungary
'IE', # Ireland
'IT', # Italy
'LT', # Lithuania
'LU', # Luxembourg
'LV', # Latvia
'MT', # Malta
'NL', # Netherlands
'PL', # Poland
'PT', # Portugal
'RO', # Romania
'SE', # Sweden
'SI', # Slovenia
'SK', # Slovakia
]

country_code = get_user_country_code(ip_address=ip_address)
return country_code in eu_countries

Check warning on line 243 in Contents/Code/general_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/general_helper.py#L242-L243

Added lines #L242 - L243 were not covered by tests


def remove_uploaded_media(item, media_type):
# type: (any, str) -> None
# type: (PlexPartialObject, str) -> None
"""
Remove themes for the specified item.
Deletes the themes upload directory for the item specified by the ``item``.
Parameters
----------
item : any
item : PlexPartialObject
The item to remove the themes from.
media_type : str
The media type to remove the themes from. Must be one of 'art', 'posters', or 'themes'.
Expand Down Expand Up @@ -193,7 +283,7 @@ def remove_uploaded_media_error_handler(func, path, exc_info):
----------
func : any
The function that caused the error.
path : str
path : any
The path that caused the error.
exc_info : any
The exception information.
Expand All @@ -202,15 +292,15 @@ def remove_uploaded_media_error_handler(func, path, exc_info):


def update_themerr_data_file(item, new_themerr_data):
# type: (any, dict) -> None
# type: (PlexPartialObject, dict) -> None
"""
Update the Themerr data file for the specified item.
This updates the themerr data file after uploading media to the Plex server.
Parameters
----------
item : any
item : PlexPartialObject
The item to update the Themerr data file for.
new_themerr_data : dict
The Themerr data to update the Themerr data file with.
Expand Down
46 changes: 46 additions & 0 deletions Contents/Code/platform_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-

# standard imports
import os
import platform

# plex debugging
try:
import plexhints # noqa: F401
except ImportError:
pass

Check warning on line 11 in Contents/Code/platform_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/platform_helper.py#L10-L11

Added lines #L10 - L11 were not covered by tests
else: # the code is running outside of Plex
from plexhints.log_kit import Log # log kit


def get_os_architecture():
# Getting architecture using platform module
machine = platform.machine()

# For more detailed check, especially for Windows OS
if os.name == 'nt':
# Possible values: '32bit', '64bit'
# This will tell us if the OS is 64-bit or 32-bit
architecture = platform.architecture()

Check warning on line 24 in Contents/Code/platform_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/platform_helper.py#L24

Added line #L24 was not covered by tests

if architecture[0] == '64bit':
return 'x86_64'
elif architecture[0] == '32bit':
return 'x86'

Check warning on line 29 in Contents/Code/platform_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/platform_helper.py#L26-L29

Added lines #L26 - L29 were not covered by tests
else:
return 'Unknown architecture'

Check warning on line 31 in Contents/Code/platform_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/platform_helper.py#L31

Added line #L31 was not covered by tests
else:
# For Unix/Linux systems, we can rely more on platform.machine()
if machine in ['x86_64', 'AMD64']:
return 'x86_64'
elif machine in ['i386', 'i686', 'x86']:
return 'x86'
elif machine in ['aarch64', 'arm64']:
return 'aarch64'

Check warning on line 39 in Contents/Code/platform_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/platform_helper.py#L36-L39

Added lines #L36 - L39 were not covered by tests
else:
return 'Unknown architecture'

Check warning on line 41 in Contents/Code/platform_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/platform_helper.py#L41

Added line #L41 was not covered by tests


# constants
architecture = get_os_architecture()
os_system = platform.system().lower()
5 changes: 5 additions & 0 deletions Contents/Code/scheduled_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# local imports
from constants import plugin_identifier
from plex_api_helper import scheduled_update
from selenium_helper import install_driver
from webapp import cache_data

# setup logging for schedule
Expand Down Expand Up @@ -109,6 +110,10 @@ def setup_scheduling():
plex_api_helper.scheduled_update : Scheduled function to update the themes.
"""
if Prefs['bool_auto_update_items']:
schedule.every(max(1, int(Prefs['int_update_browser_driver_interval']))).hours.do(

Check warning on line 113 in Contents/Code/scheduled_tasks.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/scheduled_tasks.py#L113

Added line #L113 was not covered by tests
job_func=run_threaded,
target=install_driver
)
schedule.every(max(15, int(Prefs['int_update_themes_interval']))).minutes.do(
job_func=run_threaded,
target=scheduled_update
Expand Down
Loading

0 comments on commit c7f7e05

Please sign in to comment.