forked from soju6jan/lib_metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_nfo.py
executable file
·154 lines (127 loc) · 5.96 KB
/
util_nfo.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# -*- coding: utf-8 -*-
# python
import os, traceback, time, json
# third-party
import requests
from flask import Blueprint, request, send_file, redirect, Response
from lxml import etree as ET
import lxml.builder as builder
from lxml.builder import E
from lxml import html
# sjva 공용
from framework import app, path_data, check_api, py_urllib, SystemModelSetting
from framework.logger import get_logger
from framework.util import Util
from .plugin import P
logger = P.logger
class UtilNfo(object):
@classmethod
def change_html(cls, text):
if text is not None:
return text.replace(' ', ' ').replace('<', '<').replace('>', '>').replace('&', '&').replace('"', '"').replace('#', '#').replace(''', "‘")
@classmethod
def append_tag(cls, parent, dictionary, key, **kwargs):
#logger.debug('key:%s, value:%s', key, dictionary[key])
try:
if key in dictionary and dictionary[key] is not None and dictionary[key] != '':
#parent.append(E(key, cls.change_html(str(dictionary[key])), kwargs))
value = dictionary[key]
if type(value) == int or type(value) == float:
value = str(value)
parent.append(E(key, cls.change_html(value), kwargs))
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
@classmethod
def append_tag_list(cls, parent, dictionary, key):
#logger.debug('key:%s, value:%s', key, dictionary[key])
try:
if key in dictionary and dictionary[key] is not None and len(dictionary[key]) > 1:
for value in dictionary[key]:
parent.append(E(key, cls.change_html(value)))
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
@classmethod
def _make_nfo_movie(cls, info):
try:
#logger.debug('make nfo movie')
#logger.debug(json.dumps(info, indent=4))
movie = builder.ElementMaker().movie()
#EE = builder.ElementMaker(namespace="http://www.itunes.com/dtds/podcast-1.0.dtd", nsmap={'itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd'})
#logger.debug('TITLE : %s', info['title'])
movie = E.movie (
E.title(cls.change_html(info['title'])),
E.originaltitle(info['originaltitle']),
E.sorttitle(info['sorttitle']),
E.id(info['originaltitle']),
E.uniqueid(info['code'], type=info['site'], default='true'),
)
cls.append_tag(movie, info, 'credits')
cls.append_tag(movie, info, 'mpaa')
cls.append_tag(movie, info, 'studio')
cls.append_tag(movie, info, 'plot')
cls.append_tag(movie, info, 'runtime')
cls.append_tag(movie, info, 'tagline')
cls.append_tag(movie, info, 'premiered')
cls.append_tag(movie, info, 'year')
cls.append_tag_list(movie, info, 'genre')
cls.append_tag_list(movie, info, 'country')
cls.append_tag_list(movie, info, 'tag')
if info['thumb'] is not None and len(info['thumb']) > 0:
for item in info['thumb']:
tag = E.thumb(item['value'], aspect=item['aspect'])
movie.append(tag)
if info['fanart'] is not None and len(info['fanart']) > 0:
for item in info['fanart']:
tag = E.fanart(E.thumb(item))
movie.append(tag)
if info['ratings'] is not None and len(info['ratings']) > 0:
for item in info['ratings']:
tag = E.ratings(name=item['name'], max=str(item['max']))
cls.append_tag(tag, item, 'value')
cls.append_tag(tag, item, 'votes')
movie.append(tag)
if info['extras'] is not None and len(info['extras']) > 0:
for item in info['extras']:
if item['content_type'] == 'trailer':
tag = E.trailer(item['content_url'])
movie.append(tag)
if info['actor'] is not None and len(info['actor']) > 0:
for item in info['actor']:
tag = E.actor()
cls.append_tag(tag, item, 'name')
cls.append_tag(tag, item, 'role')
cls.append_tag(tag, item, 'order')
cls.append_tag(tag, item, 'thumb')
movie.append(tag)
root = movie
tmp = ET.tostring(root, pretty_print=True, xml_declaration=True, encoding="utf-8")
if isinstance(tmp, bytes):
tmp = tmp.decode('utf-8')
return tmp
#return app.response_class(tmp, mimetype='application/xml')
except Exception as e:
logger.error('Exception:%s', e)
logger.error(traceback.format_exc())
@classmethod
def make_nfo_movie(cls, info, output='text', filename='movie.nfo', savepath=None):
text = cls._make_nfo_movie(info)
if output == 'text':
return text
elif output == 'xml':
return app.response_class(text, mimetype='application/xml')
elif output == 'file':
from io import StringIO
output_stream = StringIO(u'%s' % text)
response = Response(
output_stream.getvalue().encode('utf-8'),
mimetype='application/xml',
content_type='application/octet-stream',
)
response.headers["Content-Disposition"] = "attachment; filename=%s" % filename
return response
elif output == 'save':
if savepath is not None:
from tool_base import ToolBaseFile
return ToolBaseFile.write(text, savepath)