rpiwebapp-public/scripts/func.py

68 lines
1.9 KiB
Python
Raw Normal View History

2019-07-06 23:00:00 -07:00
from comicapi import comicarchive
from blinker import Namespace
import os, sys
from scripts import database
rpi_signals = Namespace()
comic_loaded = rpi_signals.signal("comic-loaded")
# Directories
COMICS_DIRECTORY = "/usb/storage/media/Comics/"
MOVIES_DIRECTORY = "/usb/storage/media/Videos/Movies/"
TV_SHOWS_DIRECTORY = "/usb/storage/media/Videos/TV/"
VIDEOS_DIRECTORY = "/usb/storage/media/Videos/Videos/"
GAMES_DIRECTORY = "/usb/storage/media/games/"
MUSIC_DIRECTORY = "/usb/storage/media/Music/"
#############
def get_comics():
meta = []
i = 0
for root, dirs, files in os.walk(COMICS_DIRECTORY):
for f in files:
if f.endswith(".cbr"):
path = os.path.join(root, f)
if not database.comic_path_in_db(path):
archive = comicarchive.ComicArchive(path, default_image_path="/usb/www/matthew/rpiWebApp/static/icon.png")
meta.append((path, archive.readCIX()))
i += 1
if i > 20:
comic_loaded.send("anonymous", meta=meta.copy())
meta.clear()
i = 0
comic_loaded.send("anonymous", meta=meta)
def bytestring_path(path):
"""Given a path, which is either a bytes or a unicode, returns a str
path (ensuring that we never deal with Unicode pathnames).
"""
# Pass through bytestrings.
if isinstance(path, bytes):
return path
# Try to encode with default encodings, but fall back to UTF8.
try:
return path.encode(_fsencoding())
except (UnicodeError, LookupError):
return path.encode('utf8')
def _fsencoding():
"""Get the system's filesystem encoding. On Windows, this is always
UTF-8 (not MBCS).
"""
encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
if encoding == 'mbcs':
# On Windows, a broken encoding known to Python as "MBCS" is
# used for the filesystem. However, we only use the Unicode API
# for Windows paths, so the encoding is actually immaterial so
# we can avoid dealing with this nastiness. We arbitrarily
# choose UTF-8.
encoding = 'utf8'
return encoding