-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.py
71 lines (61 loc) · 1.95 KB
/
media.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
# filename : media.py
# description : Holds important functions for the project
# author : LikeToAccess
# email : liketoaccess@protonmail.com
# date : 05-04-2021
# version : v1.0
# usage : python main.py
# notes :
# license : MIT
# py version : 3.8.2 (must run on 3.6 or higher)
#==============================================================================
import os
def log(msg):
append_file("log.txt", msg)
def credit(author, filename, resolution, file_size):
msg = f"{filename}|{resolution}|{file_size}"
append_file(f"{author}.txt", msg)
def format_title(filename):
if "/" in filename: filename = filename.split("/")[::-1][0]
filename = " ".join([word.capitalize() for word in filename.split(".")[0].split()])
return filename
def remove_file(filename):
try:
os.remove(filename)
return True
except OSError:
return False
def rename(filename_old, filename_new):
filename = filename_new.split(".")
filename_new = f"{filename[0].strip()}.{filename[1]}"
try: os.rename(filename_old, filename_new)
except FileExistsError:
remove_file(filename_new)
msg = f"Removed old version of show to be replaced with new version, {filename_new}"
print(msg)
log(msg)
rename(filename_old, filename_new)
return f"RENAME: {filename_old} -> {filename_new}"
def read_file(filename, directory=None, filter=False):
if directory:
os.chdir(f"{os.getcwd()}/{directory}")
with open(filename, "r") as file:
lines = file.read().split("\n")
if filter:
lines = filter_list(lines)
return lines
def write_file(filename, msg):
with open(filename, "w") as file:
file.write(msg)
def append_file(filename, msg):
with open(filename, "a") as file:
file.write(f"{msg}\n")
def filter_list(lines, filename=False):
if filename:
lines = read_file(filename)
data = []
for line in lines:
if line[:1] != "#" and line != "":
data.append(line)
return data