Commit 30c0d21a authored by Hermann Krumrey's avatar Hermann Krumrey

Continue work on web app

parent 64635615
......@@ -39,11 +39,20 @@ class Directory:
Class that encapsulates all of toktokkie's functionality
"""
def __init__(self, path: str, generate_metadata: bool = False,
metadata_type: str = None):
def __init__(
self,
path: str,
generate_metadata: bool = False,
metadata_type: str = None,
metadata: Optional[Metadata] = None
):
"""
Initializes the metadata of the directory
:param path: The directory's path
:param generate_metadata: Generates metadata by prompting the user
:param metadata_type: The type of metadata
:param metadata: Specifies a metadata object, which will be used
instead of reading the metadata file
:except MissingMetadataException,
InvalidMetadataException,
MetadataMismatch
......@@ -63,7 +72,10 @@ class Directory:
if not os.path.isfile(self.metadata_file):
raise MissingMetadata(self.metadata_file + " missing")
self.metadata = get_metadata(self.path) # type: Metadata
if metadata is not None:
self.metadata = metadata
else:
self.metadata = get_metadata(self.path) # type: Metadata
if not os.path.isdir(self.metadata.icon_directory):
os.makedirs(self.metadata.icon_directory)
......
......@@ -25,7 +25,8 @@ from jsonschema import validate, ValidationError
from toktokkie.exceptions import InvalidMetadata
from toktokkie.metadata.schema.SchemaBuilder import SchemaBuilder
from toktokkie.metadata.ids.IdType import IdType
from toktokkie.metadata.ids.mappings import valid_id_types
from toktokkie.metadata.ids.mappings import valid_id_types, urlmap, \
literature_media_types
from toktokkie.metadata.ids.functions import objectify_ids, stringify_ids, \
fill_ids, minimize_ids
from toktokkie.metadata.MediaType import MediaType
......@@ -230,6 +231,27 @@ class Metadata:
"""
self.json["ids"] = stringify_ids(minimize_ids(ids))
@property
def urls(self) -> Dict[IdType, List[str]]:
"""
Generates URLs for the stored ID types of this metadata object
:return: The URLs mapped to their respective id types
"""
ids = self.ids
urls = {x: [] for x in ids.keys()}
anime_manga = \
"manga" if self.media_type() in literature_media_types else "anime"
for id_type, values in ids.items():
for value in values:
url = urlmap[id_type]\
.replace("@{ANIME_MANGA}", anime_manga)\
.format(value)
urls[id_type].append(url)
return urls
def set_ids(self, id_type: IdType, ids: List[str]):
"""
Sets IDs for one ID type to the metadata
......
......@@ -141,3 +141,21 @@ theme_song_ids = [
"""
ID types that can be associated with theme songs
"""
urlmap = {
IdType.ANILIST: "https://anilist.co/@{ANIME_MANGA}/{}",
IdType.MYANIMELIST: "https://myanimelist.net/@{ANIME_MANGA}/{}",
IdType.KITSU: "https://kitsu.io/@{ANIME_MANGA}/{}",
IdType.TVDB: "https://www.thetvdb.com/?id={}&tab=series",
IdType.VNDB: "https://vndb.org/{}",
IdType.IMDB: "https://www.imdb.com/title/{}",
IdType.ISBN: "https://isbnsearch.org/isbn/{}",
IdType.MANGADEX: "https://mangadex.org/title/{}",
IdType.MUSICBRAINZ_ARTIST: "https://musicbrainz.org/artist/{}",
IdType.MUSICBRAINZ_RECORDING: "https://musicbrainz.org/recording/{}",
IdType.MUSICBRAINZ_RELEASE: "https://musicbrainz.org/release/{}"
}
"""
Maps ID types to URL schemas
"""
......@@ -43,6 +43,7 @@ def init_db():
"""
with app.app_context():
from toktokkie.web.models.MediaLocation import MediaLocation
from toktokkie.web.models.CachedDirectory import CachedDirectory
db.create_all()
......
"""LICENSE
Copyright 2015 Hermann Krumrey <hermann@krumreyh.com>
This file is part of toktokkie.
toktokkie is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
toktokkie is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with toktokkie. If not, see <http://www.gnu.org/licenses/>.
LICENSE"""
import os
from toktokkie.web import db
class CachedDirectory(db.Model):
"""
Database table that stores a cached version of a media Directory
"""
__tablename__ = "cached_directories"
"""
The table name
"""
id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True,
nullable=False
)
"""
The ID of the cached directory
"""
path = db.Column(
db.String(255),
unique=True,
nullable=False
)
"""
The path to the cached directory
"""
metadata_json = db.Column(
db.String(),
nullable=False
)
@property
def name(self) -> str:
return os.path.basename(os.path.abspath(self.path))
......@@ -18,12 +18,15 @@ along with toktokkie. If not, see <http://www.gnu.org/licenses/>.
LICENSE"""
import os
import json
from flask import render_template, Response, request, redirect, url_for
from puffotter.os import get_ext
from toktokkie.web import app, db
from toktokkie.Directory import Directory
from toktokkie.web.models.MediaLocation import MediaLocation
from toktokkie.web.models.CachedDirectory import CachedDirectory
from toktokkie.metadata.MediaType import MediaType
from toktokkie.metadata.functions import get_metadata_class
@app.route("/")
......@@ -32,13 +35,19 @@ def index():
List all directories that were found in the stored content directories
:return: The response
"""
paths = [x.path for x in MediaLocation.query.all()]
mapped_dirs = {x: [] for x in MediaType}
for path in paths:
directories = Directory.load_child_directories(path)
for directory in directories:
mapped_dirs[directory.metadata.media_type()].append(directory)
mapped_dirs = {x: [] for x in MediaType}
cached_dirs = CachedDirectory.query.all()
for cached in cached_dirs:
cached_data = json.loads(cached.metadata_json)
metadata = get_metadata_class(cached_data["type"])(
directory_path=cached.path,
json_data=cached_data,
no_validation=True
)
_directory = Directory(cached.path, metadata=metadata)
mapped_dirs[_directory.metadata.media_type()].append(_directory)
media_names = sorted([x for x in MediaType], key=lambda x: x.value)
media_dirs = []
......@@ -89,3 +98,31 @@ def add_content_dir():
db.session.add(location)
db.session.commit()
return redirect(url_for("content_dirs"))
@app.route("/directory", methods=["GET"])
def directory():
path = request.args.get("path")
_directory = Directory(path)
return render_template("directory.html", directory=_directory)
@app.route("/update", methods=["GET"])
def update():
for x in CachedDirectory.query.all():
db.session.delete(x)
paths = [x.path for x in MediaLocation.query.all()]
for path in paths:
directories = Directory.load_child_directories(path)
for _directory in directories:
cached = CachedDirectory(
path=_directory.path,
metadata_json=json.dumps(_directory.metadata.json)
)
db.session.add(cached)
db.session.commit()
redirect(url_for("/"))
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid" width="172" height="172" viewBox="0 0 172 172">
<defs>
<style>
.cls-1 {
fill: #02a9ff;
}
.cls-1, .cls-2 {
fill-rule: evenodd;
}
.cls-2 {
fill: #fefefe;
}
</style>
</defs>
<g>
<path d="M111.322,111.157 L111.322,41.029 C111.322,37.010 109.105,34.792 105.086,34.792 L91.365,34.792 C87.346,34.792 85.128,37.010 85.128,41.029 C85.128,41.029 85.128,56.337 85.128,74.333 C85.128,75.271 94.165,79.626 94.401,80.547 C101.286,107.449 95.897,128.980 89.370,129.985 C100.042,130.513 101.216,135.644 93.267,132.138 C94.483,117.784 99.228,117.812 112.869,131.610 C112.986,131.729 115.666,137.351 115.833,137.351 C131.170,137.351 148.050,137.351 148.050,137.351 C152.069,137.351 154.286,135.134 154.286,131.115 L154.286,117.394 C154.286,113.375 152.069,111.157 148.050,111.157 L111.322,111.157 Z" class="cls-1"/>
<path d="M54.365,34.792 L18.331,137.351 L46.327,137.351 L52.425,119.611 L82.915,119.611 L88.875,137.351 L116.732,137.351 L80.836,34.792 L54.365,34.792 ZM58.800,96.882 L67.531,68.470 L77.094,96.882 L58.800,96.882 Z" class="cls-2"/>
</g>
</svg>
body{
background: #EEEEEE;
margin:40px auto;
max-width:650px;
line-height:1.6;
font-size:18px;
color:#444444;
padding:0 10px
}
h1,h2,h3{
line-height:1.2
}
<!DOCTYPE html>
<html lang="en">
{% include "header.html" %}
<body>
{% block body %}{% endblock %}
</body>
</html>
{% set cover = "{}".format(directory.metadata.get_icon_file("main")) %}
{% set cover_url = url_for('image', image_format='png') + '?path=' + cover %}
{% set title=directory.metadata.name %}
{% set icon = cover_url if cover != "None" else "static/logo.png" %}
{% extends "base_layout.html" %}
{% block body %}
<h1>{{ directory.metadata.name }}</h1>
{% if cover is not none %}
<img src="{{ icon }}">
{% endif %}
{% if directory.metadata.media_type().value == "tv" %}
<table>
{% for season in directory.metadata.seasons %}
{% set season_cover = "{}".format(directory.metadata.get_icon_file(season.name)) %}
{% set season_cover_url = url_for('image', image_format='png') + '?path=' + season_cover %}
<tr>
<td>
<img src="{{ season_cover_url }}" style="height: 50px;">
</td>
<td>{{ season.name }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
<table>
<tr>
{% for id_type, urls in directory.metadata.urls.items() %}
{% for url in urls %}
{% set image = "static/{}.png".format(id_type.value) %}
<td><a href="{{url}}">
<img src="{{image}}" style="height: 50px;">
</a></td>
{% endfor %}
{% endfor %}
</tr>
</table>
{% endblock %}
\ No newline at end of file
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<link rel="icon" href="{{ icon }}">
<link rel="stylesheet" type=text/css href="{{ url_for('static', filename='style.css') }}">
{% block head %}{% endblock %}
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<a href="/content_dirs">Configure</a>
{% set title = "toktokkie" %}
{% set icon = "static/logo.png" %}
{% for media_type, directories in media_dirs %}
<h1>{{ media_type.value }}</h1>
<ul>
{% for directory in directories %}
<li>{{ directory.metadata.name }}
{% set cover = directory.metadata.get_icon_file("main") %}
{% if cover is not none %}
<img style="max-height: 50px" width="50px"
src="{{ url_for('image', image_format='png') + '?path=' + cover }}">
{% endif %}
</li>
{% endfor %}
</ul>
{% endfor %}
{% extends "base_layout.html" %}
{% block body %}
<a href="/content_dirs">Configure</a>
{% for media_type, directories in media_dirs %}
<h1>{{ media_type.value }}</h1>
<table>
{% for directory in directories %}
{% set cover = directory.metadata.get_icon_file("main") %}
<tr>
<td>
{% if cover is not none %}
<img style="max-height: 50px" width="50px" src="{{ url_for('image', image_format='png') + '?path=' + cover }}">
{% endif %}
</td>
<td>
<a href="{{ url_for('directory') + '?path=' + directory.path }}">{{ directory.metadata.name }}</a>
</td>
</tr>
{% endfor %}
</table>
{% endfor %}
{% endblock %}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment