Created
March 22, 2020 15:17
-
-
Save Kilerd/37838abe7ccbfc62ed0be7aca32a25cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
from pprint import pprint | |
import requests | |
import shutil | |
MEDIA_EXTENSION = ["mp4", "flv", "mkv"] | |
# replace folder for yourself | |
RAW = "/Volumes/media/rrys" | |
ROOT = "/Volumes/media/dramas" | |
get_session = requests.get("http://192.168.123.240:3001/api/unlock?passwd=123456") | |
session = get_session.cookies.get("session") | |
cookies = {"session": session} | |
working_tasks = requests.get("http://192.168.123.240:3001/api/workingtask", cookies=cookies) | |
response = [task["file_name"] for task in working_tasks.json()["tasks"]] | |
files = os.listdir(RAW) | |
files = filter(lambda f: f.split(".")[-1] in MEDIA_EXTENSION, files) | |
files = filter(lambda f: f not in response, files) | |
tree = {} | |
for file in files: | |
match = re.search("((S(\d+)E(\d+))|(E\d+)|(Ep\d+))", file) | |
season = match.group(3) or "NO" | |
episode = match.group(4) or match.group(5) or match.group(6) | |
drama_name = file[:match.start()-1] | |
if drama_name not in tree: | |
tree[drama_name] = {} | |
if season not in tree[drama_name]: | |
tree[drama_name][season] = [] | |
tree[drama_name][season].append(file) | |
for drama, seasons in tree.items(): | |
drama_folder = os.path.join(ROOT, drama) | |
if not os.path.exists(drama_folder): | |
os.mkdir(drama_folder) | |
for season, episodes in seasons.items(): | |
season_folder = os.path.join(drama_folder, f"Season {season}") | |
if not os.path.exists(season_folder): | |
os.mkdir(season_folder) | |
for episode in episodes: | |
episode_file = os.path.join(season_folder, episode) | |
if not os.path.exists(episode_file): | |
print(f"moving {episode}") | |
shutil.move( | |
os.path.join(RAW, episode), | |
os.path.join(season_folder, episode) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment