Created
March 12, 2018 09:39
-
-
Save veirus/318ecb2f6417763839643ddd306bedfb to your computer and use it in GitHub Desktop.
Findmedia.py - generate a (play)list of specified filetypes
This file contains hidden or 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
#!/usr/bin/env python3 | |
'''Findmedia - generate a (play)list of specified filetypes''' | |
import os | |
__revision__ = '8' | |
vidia = ('3gp', 'AVI', 'VOB', 'WMV', 'wmv', 'avi', 'flv', 'mka', 'mkv', | |
'mov', 'mp4', 'mpeg', 'mpg', 'vob', 'ogm') | |
newformats = ('webm') | |
music = ('mp3', 'ogg', 'flac', 'aiff') | |
picas = ('jpg', 'jpeg', 'png', 'gif') | |
docas = ('doc', 'docx', 'xps', 'pdf', 'djvu') | |
def fetch(path, ext=vidia): | |
for p, _, f in os.walk(path): | |
for e in f: | |
if e.endswith(ext): | |
yield os.path.join(p, e) | |
def fetch2(path, ext=vidia): | |
return (os.path.join(p, e) for p, _, f in os.walk(path) | |
for e in f if e.endswith(ext)) | |
def vlc2(path, ext=vidia): | |
import xml.etree.ElementTree as ET | |
from urllib.parse import quote | |
playlist = ET.Element('playlist', { | |
'xmlns': 'http://xspf.org/ns/0/', | |
'xmlns:vlc': 'http://www.videolan.org/vlc/playlist/ns/0/', | |
'version': '1'}) | |
title = ET.SubElement(playlist, 'title') | |
title.text = 'VLC-2' | |
trackList = ET.SubElement(playlist, 'trackList') | |
for i, fp in enumerate(fetch2(path, ext)): | |
track = ET.SubElement(trackList, 'track') | |
location = ET.SubElement(track, 'location') | |
location.text = f'file:///{quote(fp)}' | |
duration = ET.SubElement(track, 'duration') | |
duration.text = '0' | |
extension = ET.SubElement( | |
track, 'extension', application='http://www.videolan.org/vlc/playlist/0') | |
vlcid = ET.SubElement(extension, 'vlc:id') | |
vlcid.text = f'{i}' | |
else: | |
extension2 = ET.SubElement( | |
playlist, 'extension', application='http://www.videolan.org/vlc/playlist/0') | |
for _ in range(i + 1): | |
ET.SubElement(extension2, 'vlc:item', tid=f'{_}') | |
else: | |
newtree = ET.tostring(playlist) | |
import xml.dom.minidom | |
xml = xml.dom.minidom.parseString(newtree) | |
return (xml.toprettyxml(encoding='utf-8'), f'xml{__revision__}.xspf') | |
def vlc(path, ext=vidia): | |
from urllib.parse import quote | |
tmpl = '''<?xml version="1.0" encoding="UTF-8"?> | |
<playlist xmlns="http://xspf.org/ns/0/" xmlns:vlc="http://www.videolan.org/vlc/playlist/ns/0/" version="1"> | |
<title>VLC-1</title> | |
<trackList>''' | |
for i, fp in enumerate(fetch(path, ext)): | |
tmpl += f''' | |
<track> | |
<location>file:///{quote(fp)}</location> | |
<duration>0</duration> | |
<extension application="http://www.videolan.org/vlc/playlist/0"> | |
<vlc:id>{i}</vlc:id> | |
</extension> | |
</track>''' | |
else: | |
tmpl += '''</trackList> | |
<extension application="http://www.videolan.org/vlc/playlist/0">''' | |
for _ in range(i+1): | |
tmpl += f'\n\t\t\t<vlc:item tid="{_}"/>' | |
else: | |
tmpl += '\n\t</extension>\n</playlist>' | |
return tmpl.encode('utf-8'), f'str{__revision__}.xspf' | |
def html(path, ext=vidia): | |
with open('%s.html' % os.path.basename(path), 'wb') as f: | |
for fp, ct, fl in ((os.path.join(p, e), p, e) for p, d, f in os.walk(path) for e in f if e.endswith(ext)): | |
f.write( | |
'<a href="file:///{0}">{0} ► </a><a href="file:///{1}">{2}</a><br>'.format(ct, fp, fl).encode('utf-8')) | |
def txt(path, ext=vidia): | |
f='' | |
for i, a in enumerate(fetch(path, ext)): | |
# f+='[%s] %s\n' % (i,a) | |
f+='%s\n' % a | |
f+='Overall: %s files\n' % i | |
return (f.encode('utf-8'), 'txt') | |
def printall(path, ext=vidia): | |
for i, a in enumerate(fetch(path, ext)): | |
print('[%5d]' % i, a.encode('utf-8')) | |
# print('{0:<5}{1}'.format(i,a).encode('utf-8')) | |
def out(path, ext=vidia, *, func=None, outdir=''): | |
if not outdir: | |
outdir=path | |
if func is None: | |
printall(path, ext) | |
return | |
print(f'>> Scanning `{path}` with `{func.__name__}`\n>> for {ext}') | |
mode='wb' | |
content, ftype = func(path, ext) | |
name = os.path.join(outdir, f'{os.path.basename(path)}.{ftype}') | |
with open(name, mode) as outfile: | |
outfile.write(content) | |
print(f'>> Finished!\n>> File `{name}` was created.') | |
if __name__ == '__main__': | |
import sys, time | |
start = time.time() | |
where=os.path.realpath(sys.argv[1]) if len(sys.argv) == 2 else 'D:\\Downloads' | |
here='C:\\Users\\Neil\\Desktop\\fluff\\' | |
out(where, ext=vidia, func=vlc, outdir=here) | |
print(f'>> Done in {time.time() - start} sec.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment