Skip to content

Commit

Permalink
Merge pull request #109 from luigi311/dev
Browse files Browse the repository at this point in the history
Update Deps, Add max_threads
  • Loading branch information
luigi311 authored Sep 29, 2023
2 parents ac7f389 + bf5d875 commit 3cd73e5
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ LOGFILE = "log.log"
## Timeout for requests for jellyfin
REQUEST_TIMEOUT = 300

## Max threads for processing
MAX_THREADS = 32

## Map usernames between servers in the event that they are different, order does not matter
## Comma separated for multiple options
#USER_MAPPING = { "testuser2": "testuser3", "testuser1":"testuser4" }
Expand Down
3 changes: 2 additions & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ A clear and concise description of what you expected to happen.
If applicable, add logs to help explain your problem ideally with DEBUG set to true, be sure to remove sensitive information

**Type:**
- [ ] Docker
- [ ] Docker Compose
- [ ] Docker
- [ ] Native

**Additional context**
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PlexAPI==4.13.4
PlexAPI==4.15.2
requests==2.31.0
python-dotenv==1.0.0
aiohttp==3.8.5
40 changes: 1 addition & 39 deletions src/black_white.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def setup_black_white_lists(
whitelist_users,
)


def setup_x_lists(
xlist_library,
xlist_library_type,
Expand Down Expand Up @@ -89,42 +90,3 @@ def setup_x_lists(
logger(f"{xlist_type}list Users: {xlist_users}", 1)

return xlist_library, xlist_library_type, xlist_users







































5 changes: 2 additions & 3 deletions src/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ def search_mapping(dictionary: dict, key_value: str):
return None


def future_thread_executor(args: list, workers: int = -1):
def future_thread_executor(args: list, threads: int = 32):
futures_list = []
results = []

if workers == -1:
workers = min(32, os.cpu_count() * 2)
workers = min(int(os.getenv("MAX_THREADS", 32)), os.cpu_count() * 2, threads)

with ThreadPoolExecutor(max_workers=workers) as executor:
for arg in args:
Expand Down
23 changes: 21 additions & 2 deletions src/jellyfin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(self, baseurl, token):
self.baseurl = baseurl
self.token = token
self.timeout = aiohttp.ClientTimeout(
total = int(os.getenv("REQUEST_TIMEOUT", 300)),
total=int(os.getenv("REQUEST_TIMEOUT", 300)),
connect=None,
sock_connect=None,
sock_read=None,
Expand All @@ -88,8 +88,12 @@ def __init__(self, baseurl, token):

self.users = asyncio.run(self.get_users())

async def query(self, query, query_type, session, identifiers=None):
async def query(self, query, query_type, session=None, identifiers=None):
try:
if not session:
async with aiohttp.ClientSession(timeout=self.timeout) as session:
return await self.query(query, query_type, session, identifiers)

results = None
headers = {"Accept": "application/json", "X-Emby-Token": self.token}
authorization = (
Expand Down Expand Up @@ -134,6 +138,21 @@ async def query(self, query, query_type, session, identifiers=None):
logger(f"Jellyfin: Query {query_type} {query}\nResults {results}\n{e}", 2)
raise Exception(e)

def info(self) -> str:
try:
query_string = "/System/Info/Public"

response = asyncio.run(self.query(query_string, "get"))

if response:
return f"{response['ServerName']}: {response['Version']}"
else:
return None

except Exception as e:
logger(f"Jellyfin: Get server name failed {e}", 2)
raise Exception(e)

async def get_users(self):
try:
users = {}
Expand Down
3 changes: 3 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ def main_loop():

# Start server_2 at the next server in the list
for server_2 in servers[servers.index(server_1) + 1 :]:
logger(f"Server 1: {server_1[0].capitalize()}: {server_1[1].info()}", 0)
logger(f"Server 2: {server_2[0].capitalize()}: {server_2[1].info()}", 0)

# Create users list
logger("Creating users list", 1)
server_1_users, server_2_users = setup_users(
Expand Down
9 changes: 5 additions & 4 deletions src/plex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import re, requests, os, traceback
import re, requests, traceback
from urllib3.poolmanager import PoolManager
from math import floor

Expand Down Expand Up @@ -173,9 +173,7 @@ def get_user_library_watched(user, user_plex, library):
for show in library_videos.search(inProgress=True):
args.append([get_user_library_watched_show, show])

for show_guids, episode_guids in future_thread_executor(
args, workers=min(os.cpu_count(), 4)
):
for show_guids, episode_guids in future_thread_executor(args, threads=4):
if show_guids and episode_guids:
# append show, season, episode
if show_guids not in user_watched[user_name][library.title]:
Expand Down Expand Up @@ -414,6 +412,9 @@ def login(self, baseurl, token):
logger(f"Plex: Failed to login, Error: {e}", 2)
raise Exception(e)

def info(self) -> str:
return f"{self.plex.friendlyName}: {self.plex.version}"

def get_users(self):
try:
users = self.plex.myPlexAccount().users()
Expand Down

0 comments on commit 3cd73e5

Please sign in to comment.