Skip to content

Commit

Permalink
edit mode fix
Browse files Browse the repository at this point in the history
fix for torrent no path
  • Loading branch information
datawhores committed Feb 26, 2023
1 parent ea13cda commit e55b1ae
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 72 deletions.
Binary file added bin/gifsicle/gifdiff.exe
Binary file not shown.
21 changes: 12 additions & 9 deletions empupload/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ def create_images(mediafiles,picdir):
console.console.print("Creating screens",style="yellow")
mtn=mtnHelper()
for count,file in enumerate(mediafiles):
t=subprocess.run([mtn,'-c','3','-r','3','-w','2880','-k','060237','-j','92','-b','2','-f',settings.font,file,"-P",'-O',picdir],stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
key=os.urandom(4).hex()
t=subprocess.run([mtn,'-c','3','-r','3','-w','2880','-k','060237','-j','92','-b','2','-f',settings.font,file,"-P",'-O',picdir,"-o",f"_{key}.jpg"],stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if t.returncode==0 or t.returncode==1:
console.console.print(f"{count+1}. Image created for {file}",style="yellow")
else:

console.console.print(t.stdout,style="red")
console.console.print(t.returncode,style="red")
console.console.print(f"{t.stdout.decode()}\nreturncode:{t.returncode}\nError with mtn",style="red")
Expand Down Expand Up @@ -128,29 +130,30 @@ def upload_images(imageList):
"""
Zip images or create directory or photo storage
:param inputFolder:path to store generated photo storage
:param inputPath:path to store generated photo storage
:param picdir: directory used to store images
:param output: basename for output zip or directory
:returns None:
"""
def zip_images(inputFolder,picdir):
def zip_images(inputPath,picdir,output="screens"):
#zip or just move images to directory being uploaded to EMP
files=list(Path(picdir).iterdir())
count=len(files)
if(count>=100):
zipLocation=os.path.join(inputFolder,"thumbnail.zip")
zipLocation=os.path.join(inputPath,f"{output}.zip")
console.console.print(f"Creating Zip: {zipLocation}")
with zipfile.ZipFile(zipLocation, mode="w") as archive:
with zipfile.ZipFile(zipLocation, mode="a") as archive:
for filename in files:
archive.write(filename)
return [zipLocation],zipLocation
elif count>=10:
photos=os.path.join(inputFolder,"screens")
photos=os.path.join(inputPath,f"{output}")
console.console.print(f"Creating screens folder: {photos}")
shutil.rmtree(photos,
ignore_errors=True)
shutil.copytree(picdir, photos)
return paths.search(photos,".*",recursive=False),picdir
return paths.search(photos,".*",recursive=False),photos
return [],None


Expand Down Expand Up @@ -195,7 +198,7 @@ def createcovergif(picdir,maxfile):
"""
finds the Larget File in Directory
:param inputFolder: Directory to scan for video files
:param inputPath: Directory to scan for video files
:returns: path to selected video file
"""

Expand All @@ -209,7 +212,7 @@ def find_maxfile(media):
"""
Generates a dictionary for static images
:param inputFolder: Directory to scan for video files
:param inputPath: Directory to scan for video files
:returns: path to selected video file
"""
def createStaticImagesDict(input):
Expand Down
101 changes: 59 additions & 42 deletions empupload/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,50 +48,50 @@ def upload(ymlpath):
:returns None:
"""

def process_yml(inputFolder,ymlpath):
def process_yml(inputPath,ymlpath):
p=paths.NamedTemporaryFile(suffix=".yml")
fp=open(p,"w")
tempPicDir=args.prepare.picdir or tempfile.mkdtemp(dir=settings.tmpdir)
try:
video=None
audio=None
console.console.print(f"\nAttempting to Create yaml at {ymlpath}",style="yellow")
if os.path.isfile(ymlpath) and selection.singleoptions("File Exist Do you want to overwrite?",["Yes","No"])=="No":
return
files=None
if os.path.isdir(inputFolder):
files=paths.search(inputFolder,".*",recursive=True,exclude=[os.path.join(inputFolder,"thumbnail.zip"),os.path.join(inputFolder,"screens")]+args.prepare.exclude)
if os.path.isdir(inputPath):
files=paths.search(inputPath,".*",recursive=True,exclude=[os.path.join(inputPath,"thumbnail.zip"),os.path.join(inputPath,"screens")]+args.prepare.exclude)
if args.prepare.manual:
files=selection.multioptions("Select Files from folder to upload",files,transformer=lambda result: f"Number of files selected: {len(result)}")
else:
files=[inputFolder]
files=[inputPath]
maxfile=media.find_maxfile(files)
if maxfile:
video,audio=media.metadata(maxfile)
basename=paths.get_upload_name(inputFolder)
picdir=args.prepare.picdir or tempfile.mkdtemp(dir=settings.tmpdir)
Path(picdir ).mkdir( parents=True, exist_ok=True )
basename=paths.get_upload_name(inputPath)
Path(tempPicDir ).mkdir( parents=True, exist_ok=True )
emp_dict={}
sug=re.sub("\."," ",basename)
sug=string.capwords(sug)
emp_dict["inputFolder"]=inputFolder
emp_dict["inputPath"]=inputPath
emp_dict["title"]=selection.strinput("Enter Title For Upload:",default=sug)
emp_dict["category"]=paths.getcat()[selection.singleoptions("Enter Category:",paths.getcat().keys())]
emp_dict["taglist"]=_tagfixer(selection.strinput("Enter Tags Seperated By Space:"))
emp_dict["desc"]=selection.strinput("Enter Description:",multiline=True)
emp_dict["staticimg"]=media.createStaticImagesDict(args.prepare.images)
emp_dict["staticimg"]=media.createStaticImagesDict(args.prepare.images) or {}
emp_dict["mediaInfo"]={}
emp_dict["mediaInfo"]["audio"]=audio
emp_dict["mediaInfo"]["video"]=video
emp_dict["mediaInfo"]["audio"]=audio or {}
emp_dict["mediaInfo"]["video"]=video or {}
emp_dict["tracker"]=args.prepare.tracker
emp_dict["exclude"]=args.prepare.exclude
emp_dict["cover"]=media.createcovergif(picdir,maxfile)
media.create_images(files,picdir)
imgfiles,imglocation=media.zip_images(inputFolder,picdir)
emp_dict["cover"]=media.createcovergif(tempPicDir,maxfile)
media.create_images(files,tempPicDir)
imgfiles,imglocation=media.zip_images(inputPath,tempPicDir)
files.extend(imgfiles)
emp_dict["screensDir"]=imglocation

emp_dict["screens"]=media.upload_images(media.imagesorter(picdir))
emp_dict["torrent"]=torrent.create_torrent(os.path.join(args.prepare.torrent,f"{basename}.torrent"),inputFolder,files,tracker=args.prepare.tracker)
emp_dict["screens"]=media.upload_images(media.imagesorter(tempPicDir))
emp_dict["torrent"]=torrent.create_torrent(os.path.join(args.prepare.torrent,f"{basename}.torrent"),inputPath,files,tracker=args.prepare.tracker)
if selection.singleoptions("Manually Edit the upload page 'Description' Box",choices=["Yes","No"])=="Yes":
emp_dict["template"]=selection.strinput(msg="",default=getPostStr(emp_dict),multiline=True)

Expand All @@ -104,6 +104,7 @@ def process_yml(inputFolder,ymlpath):
console.console.print(E)
console.console.print(traceback.format_exc())
finally:
paths.rm( tempPicDir)
fp.close()
paths.remove(p)

Expand All @@ -123,53 +124,69 @@ def process_yml(inputFolder,ymlpath):
:returns:returns path to updated yml
"""
def update_yml(ymlpath):
mirrorDir=None
try:
f=open(ymlpath,"r")
emp_dict= yaml.safe_load(f)
f.close()
baseName=Path(emp_dict["inputPath"]).name
mirrorDir=str(Path(tempfile.mkdtemp(dir=settings.tmpdir),baseName))
Path(mirrorDir).mkdir()
os.chdir(mirrorDir)

if Path(emp_dict["inputPath"]).is_file():
Path(emp_dict["inputPath"]).symlink_to(Path(baseName),target_is_directory=True)
else:
for p in Path(emp_dict["inputPath"]).iterdir() :
if p.name=="screens.zip" or p.name=="screens":
continue
Path(p.name).symlink_to(p,target_is_directory=p.is_dir())


emp_dict["title"]=selection.strinput("Enter Title For Upload:",default=emp_dict["title"])
emp_dict["taglist"]=re.sub(","," ",selection.strinput("Enter Tags Seperated By Space:",default=emp_dict['taglist']))
emp_dict["desc"]=selection.strinput("Enter Description for upload: ",multiline=True,default=emp_dict["desc"])
if selection.singleoptions("Change Category",choices=["Yes","No"])=="Yes":
emp_dict["category"]=paths.getcat()[selection.singleoptions("Update Category: ",paths.getcat().keys())]
# temptorrent=paths.NamedTemporaryFile(suffix=".torrent")
# tempPicDir=tempfile.mkdtemp(dir=settings.tmpdir)
#make a copy of the current picdir
# shutil.copytree(emp_dict["screens"],f'{emp_dict["screens"]}2')
# if selection.singleoptions("Update file selection\nNote This will recreate screens",choices=["Yes","No"])=="Yes":
# allFiles=paths.search(emp_dict["inputFolder"],".*",recursive=True,exclude=[os.path.join(emp_dict["inputFolder"],"thumbnail.zip"),os.path.join(emp_dict["inputFolder"],"screens")]+emp_dict["exclude"])
# files=selection.multioptions("Select files from folder to upload",allFiles,transformer=lambda result: f"Number of files selected: {len(result)}")
# if selection.singleoptions("Generate a new cover gif?",choices=["Yes","No"])=="Yes":
# maxfile=media.find_maxfile(files)
# emp_dict["cover"]=media.createcovergif(tempPicDir,maxfile)
# #Return the final destination of pics, appends to
# screens=media.zip_images(emp_dict["inputFolder"],media.create_images(files,emp_dict["inputFolder"],tempPicDir))
# files.extend()
# #Uses temp pic dir
# emp_dict["screens"]=media.upload_images(media.imagesorter(tempPicDir))
# torrent.create_torrent(temptorrent,emp_dict["inputFolder"],files,tracker=emp_dict["tracker"])
temptorrent=paths.NamedTemporaryFile(suffix=".torrent")
tempPicDir=tempfile.mkdtemp(dir=settings.tmpdir)
newScreensDir=None
if selection.singleoptions("Update file selection\nNote This will recreate screens",choices=["Yes","No"])=="Yes":
allFiles=paths.search(".",".*",recursive=True,exclude=emp_dict["exclude"])
files=selection.multioptions("Select files from folder to upload",allFiles,transformer=lambda result: f"Number of files selected: {len(result)}")
if selection.singleoptions("Generate a new cover gif?",choices=["Yes","No"])=="Yes":
maxfile=media.find_maxfile(files)
emp_dict["cover"]=media.createcovergif(tempPicDir,maxfile)
media.create_images(files,tempPicDir)
imgfiles,newScreensDir=media.zip_images(mirrorDir,tempPicDir)
emp_dict["screens"]=media.upload_images(media.imagesorter(tempPicDir))
# files.extend(imgfiles)
files=[files[0]]
torrent.create_torrent(temptorrent,mirrorDir,files,tracker=emp_dict["tracker"])
if selection.singleoptions("Manually Edit the upload page 'Description' Box",choices=["Yes","No"])=="Yes":

emp_dict["template"]=selection.strinput(msg="",default=getPostStr(emp_dict),multiline=True)
else:
emp_dict["template"]=getPostStr(emp_dict)

console.console.print(emp_dict,style="yellow")
if selection.singleoptions("Do you want to save your changes?",choices=["Yes","No"])=="Yes":
# shutil.move(temptorrent,emp_dict["torrent"])
paths.move(temptorrent,emp_dict["torrent"])
paths.rm(emp_dict["screensDir"])
paths.move(newScreensDir,emp_dict["inputPath"])
emp_dict["screensDir"]=newScreensDir
fp=open(ymlpath,"w")
yaml.dump(emp_dict,fp, default_flow_style=False)
fp.close()
else:
None
# shutil.rmtree(f'{emp_dict["screensDir"]}',ignore_errors=True)
# shutil.move(f'{emp_dict["screensDir"]}2',emp_dict["screens"])

except Exception as E:
console.console.print(E)
console.console.print(traceback.format_exc())
finally:
None
# shutil.rmtree(f'{emp_dict["screensDir"]}2',ignore_errors=True)
# Path(temptorrent).unlink(missing_ok=True)
#force removal
paths.rm( tempPicDir)
paths.rm(temptorrent)
paths.rm(mirrorDir)

"""
Retrive Post string that is used for torrent description
Expand All @@ -188,7 +205,7 @@ def getPostStr(emp_dict):
}
nameSpace.update(emp_dict["staticimg"])
nameSpace.update(templateMediaInfoHelper(emp_dict["mediaInfo"]["audio"],emp_dict["mediaInfo"]["video"]))
nameSpace.update({"torrent":emp_dict["torrent"],"args":args,"inputFolder":emp_dict["inputFolder"]})
nameSpace.update({"torrent":emp_dict["torrent"],"args":args,"inputPath":emp_dict["inputPath"]})
t = Template((emp_dict.get("template") or templateHelper() or ""),searchList=[nameSpace])
return str(t)

Expand All @@ -199,7 +216,7 @@ def getPostStr(emp_dict):
"""
:param video: video key value of config yml
:param audio: audio key value of config yml
:inputFolderStr: current template string
:inputPathStr: current template string
:returns templatestr: str with subsititutions
"""
Expand Down
2 changes: 1 addition & 1 deletion empupload/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def create_chrome():
os.remove(chrome)
os.chdir(os.listdir()[0])
for element in os.scandir():
shutil.move(element.name, chromeDir)
paths.move(element.name, chromeDir)
os.chdir(settings.workingdir)
shutil.rmtree(tempchrome)
os.chmod(chromepath, 0o775)
Expand Down
48 changes: 32 additions & 16 deletions general/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import yaml
import tempfile
import glob
import shutil
import natsort
import general.arguments as arguments
Expand All @@ -21,7 +22,7 @@
:return: list of matching directories
"""
def search(path,filterStr,recursive=False,dir=False,exclude=None):
def search(path,filterStr,recursive=False,dir=False,exclude=None,abs=False):
if exclude==None:
exclude=[]
exclude=list(map(lambda x: convertLinux(x),exclude))
Expand All @@ -30,15 +31,15 @@ def search(path,filterStr,recursive=False,dir=False,exclude=None):
if recursive==False:
search="*"

results=list(map(lambda x:convertLinux(str(x)),pathlib.Path(path).glob(search)))
results=list(map(lambda x:convertLinux(x),glob.glob(str(pathlib.Path(path,search)),recursive=recursive)))
filteredPaths=list(filter(lambda x:os.path.isdir(x)==dir,results))
filteredPaths=list(filter(lambda x:not any(re.search(ele,x) for ele in exclude),filteredPaths))
sortedPaths=natsort.natsorted(filteredPaths)
return list(filter(lambda x:re.search(filterStr,x)!=None,sortedPaths))


def getmediaFiles(inputFolder,recursive=True):
return search(inputFolder,"\.mkv|\.mp4",recursive=True)
def getmediaFiles(inputPath,recursive=True):
return search(inputPath,"\.mkv|\.mp4",recursive=True)


"""
Expand All @@ -61,32 +62,32 @@ def get_choices():
"""
Generates basename of path
:param inputFolder: path chosen by user
:param inputPath: path chosen by user
:returns string: basename
"""
def get_upload_name(inputFolder):
return pathlib.Path(inputFolder).parts[-1]
def get_upload_name(inputPath):
return pathlib.Path(inputPath).parts[-1]

"""
Generates a yml file name
:param inputFolder: path chosen by user
:param inputPath: path chosen by user
:returns str: a yml file path
"""
def generate_yaml(inputFolder):
if inputFolder.endswith(".yml") or inputFolder.endswith(".yaml"):
return inputFolder
basename=get_upload_name(inputFolder)
def generate_yaml(inputPath):
if inputPath.endswith(".yml") or inputPath.endswith(".yaml"):
return inputPath
basename=get_upload_name(inputPath)
return convertLinux(os.path.join(args.output,f"{basename}.yml"))

"""
Gets all yml files inside directory, Non Recursively
:param inputFolder: path chosen by user
:param inputPath: path chosen by user
:returns array: a array of yml files
"""
def retrive_yaml(inputFolder):
return list(filter(lambda x:re.search(".yml$|\.yaml$",x)!=None,search(inputFolder,".*",recursive=True,dir=False)))
def retrive_yaml(inputPath):
return list(filter(lambda x:re.search(".yml$|\.yaml$",x)!=None,search(inputPath,".*",recursive=True,dir=False)))

"""
Gets empornium categories
Expand Down Expand Up @@ -124,4 +125,19 @@ def remove(input):
:returns path:converted path
"""
def convertLinux(path):
return re.sub(re.escape("\\"), "/", str(pathlib.PurePosixPath(path)))
return re.sub(re.escape("\\"), "/", str(pathlib.PurePosixPath(path)))

def rm(input):
if not input:
return
elif pathlib.Path(input).is_dir():
shutil.rmtree(input,ignore_errors=True)
else:
pathlib.Path(input).unlink(missing_ok=True)

def move(send,target):
if not send or not target:
return
if not pathlib.Path(send).exists() or not pathlib.Path(target).parent.exists():
return
shutil.move(send,target)
1 change: 1 addition & 0 deletions general/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def multioptions(msg,choices,mandatory =True,default=None,instruction=None,valid
:returns any: choice selected
"""
def strinput(msg,default="",multiline=False,instructions=None,sync=True):

if multiline and not instructions:
instructions="Press Enter to Move to new line\nESC + Enter to Finish"
result=inquirer.text(message=msg,default=default,multiline=multiline,instruction=instructions)
Expand Down
Loading

0 comments on commit e55b1ae

Please sign in to comment.