Skip to content

Commit

Permalink
Add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Fazzani committed Dec 1, 2024
1 parent 32ce9cc commit a389aa6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,5 @@ test-reports/

.idea/
log/
tmp
tmp
cache_*.json
8 changes: 4 additions & 4 deletions data/config_playlist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ vodfilter:
regex:
- "\\.(mkv|avi|mp4)$"
hidden_groups:
- "\\b(برامج)|(ليبيا)|(مغربية)|(أجنبية)|(آسيوية)|(هندية)|(مدبلجة)|(رعب)|(ألمانيه)|(مترجمة)|(تركية)|(اغاني)(رياضه\\sمتنوعه)\\b"
- "\\b(منوعات)|(برامج)|(ليبيا)|(مغربية)|(أجنبية)|(آسيوية)|(هندية)|(مدبلجة)|(رعب)|(ألمانيه)|(مترجمة)|(تركية)|(اغاني)|(رياضه\\sمتنوعه)\\b"
- "\\b(HANDBALL)|(F1)|(MOTO)|(FOOTBALL)|(wwe)|(CYCLISME)|(GOLF)|(BASKETBALL)|(WRC)\\b"

displaynamefilter:
priority: 2
regex:
- "(\\w{2,3})\\s[:|\\-]\\s*(.*)$"
- "^(\\w{2,3})\\s*[:|\\-]\\s*(.*)$"

qualityfilter:
priority: 3
Expand Down Expand Up @@ -45,7 +46,7 @@ cleannamefilter:
priority: 19
regex:
- "\\b(hevc)|(h265)|(XTRA)|(film)|(اغاني)||(طرب)|(مسلسل(?:ات)?(?:ال)?)|(الفلم|فلم|الفيلم|الفیلم|فيلم)|(بودكاست?(ال)?)|(برنامج?(ال)?)|(مسرحية)\\b"
- \\b(محمد\sسعد)\\b"
- "\\b(عادل\\sإمام)|(اسماعيل\\sياسين)|(محمد\\sسعد)|(أحمد\\sزكي)\\b"
- "[!$%&'\"()\\*\\+,\\.:/;<=>?@\\[\\]\\^_`{|}~◉]"

groupingfilter:
Expand All @@ -57,7 +58,6 @@ groupingfilter:
Mbc: "mbc"
Osn: "osn|pehla"
Tunisia: "tunisi(e|a)?"
Sports: "sports?"

piconfilter:
priority: 25
Expand Down
64 changes: 50 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import multiprocessing
import os
import re
from typing import Any
from datetime import datetime
from typing import Any, Optional, cast

import click
import jsonpickle
from tqdm import tqdm

from pliptv.cli_questions import ask_information, log
Expand All @@ -28,9 +30,12 @@

LOG: logging.Logger = logging.getLogger(__name__)
STRM_EXT = ".strm"
TV_SERIES_REGEX = re.compile(r"^(\b.+[\s])*(s\d{1,4})[^\w](e\d{1,4})$", flags=re.IGNORECASE)
TV_SERIES_REGEX = re.compile(r"(.+[\s])*(s\d{1,4})[^\w](e\d{1,4})", flags=re.IGNORECASE)
IS_ARABIC_REGEX = re.compile(r"[\u0600-\u06FF]")

# TODO: combine using click with env variables and remove 'ask_information'
# TODO: notification when processing finished


@click.command()
@click.option(
Expand All @@ -49,12 +54,21 @@
)
@click.option(
"--vod",
"is_generation_vod_enabled",
default=False,
type=bool,
is_flag=True,
help="Generate VOD 'strm' file",
)
def main(auto: bool, export: bool, vod: bool) -> None:
@click.option(
"--cache",
"is_cache_enabled",
default=False,
type=bool,
is_flag=True,
help="Persist generated playlist",
)
def main(auto: bool, export: bool, is_generation_vod_enabled: bool, is_cache_enabled: bool) -> None:
"""
Extensible CLI m3u playlist manager
many default filters was provided for:
Expand Down Expand Up @@ -105,9 +119,10 @@ def main(auto: bool, export: bool, vod: bool) -> None:
else:
with open(pl_url, "r", encoding=ENCODING_UTF8) as f:
lines = get_lines(f.readlines())

log(f"{len(lines)} streams retrieved", "green")

m3u = M3u.from_list("playlist", lines)
m3u: M3u = M3u.from_list("playlist", lines)

log("Loading filters", "white")
filters = load_filters()
Expand All @@ -127,11 +142,20 @@ def main(auto: bool, export: bool, vod: bool) -> None:
file_result = save_pl_to_path(m3u, str(pl_info.get("playlist_output_path")))
log(f"Generated playlist for {m3u.name.capitalize()}: {file_result}", "white")

if is_cache_enabled:
cache_playlist_generation(m3u)

if export:
url = save_pl(m3u)
log(f"Generated playlist url for {m3u.name.capitalize()}: {url}", "white")
url: str = save_pl(m3u)
LOG.debug(f"Playlist Exported to: {url}", "white")

created_streams = vod_processing(is_generation_vod_enabled, pl_info, playlist_config, m3u)

vod_processing(vod, pl_info, playlist_config, m3u, True)
if is_cache_enabled:
cache_playlist_generation(m3u, "_final")

if created_streams:
cleanup(created_streams, str(pl_info.get("strm_output_path")))

# Display report
# get_report(m3u)
Expand All @@ -143,7 +167,15 @@ def main(auto: bool, export: bool, vod: bool) -> None:
log("The end.", "green")


def vod_processing(vod: bool, pl_info: dict[str, Any], playlist_config: PlaylistConfig, m3u: M3u, cleanup_folder: bool = False) -> None:
def cache_playlist_generation(m3u, suffix: str = ""):
log("Cache generation...", "green")

generation_data: str = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
with open(f"cache_{generation_data}{suffix}.json", "w", encoding="utf-8") as f:
f.write(cast(str, jsonpickle.encode(m3u, indent=4)))


def vod_processing(vod: bool, pl_info: dict[str, Any], playlist_config: PlaylistConfig, m3u: M3u) -> Optional[list[str]]:
"""STRM VOD files generator.
Args:
Expand Down Expand Up @@ -175,7 +207,7 @@ def vod_processing(vod: bool, pl_info: dict[str, Any], playlist_config: Playlist
desc="VOD strm processing",
):
match_tv_series = TV_SERIES_REGEX.search(stream.meta.display_name)
output_path = os.path.join(tv_series_output_path, match_tv_series.group(1).strip()) if match_tv_series else videos_output_path
output_path: str = os.path.join(tv_series_output_path, match_tv_series.group(1).strip()) if match_tv_series else videos_output_path
strm_output_fullpath: str = os.path.join(output_path, stream.meta.display_name.strip() + STRM_EXT)
try:
created_streams.append(strm_output_fullpath)
Expand All @@ -187,23 +219,27 @@ def vod_processing(vod: bool, pl_info: dict[str, Any], playlist_config: Playlist
log(f"Error while generating VOD stream: {e} {stream.url}", "red")
continue

if cleanup_folder:
cleanup(created_streams, strm_output_path)

log(
f"Generated VOD strm files for playlist: {m3u.name.capitalize()} into {strm_output_path}",
"green",
)
return created_streams


def cleanup(created_streams: list[str], strm_output_path: str):
"""Removes all streams that weren't yet existed in the playlist from output path.
Args:
created_streams (list[str]): all streams that were created
strm_output_path (str): streams output path (root directory)
"""
import glob

LOG.debug(f"Cleanup: {strm_output_path}")
for file_path in tqdm(list(glob.iglob(f"{strm_output_path}/**/*{STRM_EXT}", recursive=True)), desc="Cleaning up VOD folder"):
for file_path in tqdm(glob.iglob(f"{strm_output_path}/**/*{STRM_EXT}", recursive=True), desc="Cleaning up VOD folder"):
if file_path not in created_streams:
os.remove(file_path)
dir_name = os.path.dirname(file_path)
dir_name: str = os.path.dirname(file_path)
if len(os.listdir(dir_name)) == 0:
os.rmdir(dir_name)

Expand Down
6 changes: 6 additions & 0 deletions pliptv/m3u_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import string
from io import StringIO
from json import JSONEncoder
from typing import List, Tuple
from urllib.parse import urlparse

Expand Down Expand Up @@ -143,3 +144,8 @@ def save_pl_to_path(pl: M3u, output_path: str) -> str:

def translate_channel_name(name: str) -> str:
return name.translate(str.maketrans(dict.fromkeys(string.punctuation))).title()


class CustomJSONEncoder(JSONEncoder):
def default(self, o):
return o.__dict__
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ termcolor~=2.5.0
coloredlogs==15.0
validators~=0.34.0
tqdm~=4.67.0
jsonpickle~=4.0.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="xpl",
version="0.1.1",
version="0.1.3",
author="Heni Fazzani",
author_email="[email protected]",
description="Simple extensible m3u playlist manager cli",
Expand Down

0 comments on commit a389aa6

Please sign in to comment.