diff --git a/app/__init__.py b/app/__init__.py index afc0836..e8f3e30 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -96,6 +96,7 @@ from app.provider.view import ProviderView from app.stream.view import StreamView from app.service.view import ServiceView from app.subscriber.view import SubscriberView +from app.autofill.view import M3uParseView from app.epg.view import EpgView HomeView.register(app) @@ -103,4 +104,5 @@ ProviderView.register(app) StreamView.register(app) ServiceView.register(app) SubscriberView.register(app) +M3uParseView.register(app) EpgView.register(app) diff --git a/app/autofill/entry.py b/app/autofill/entry.py new file mode 100644 index 0000000..170c419 --- /dev/null +++ b/app/autofill/entry.py @@ -0,0 +1,15 @@ +from mongoengine import Document, StringField, ListField + +import pyfastocloud_models.constants as constants + + +class M3uParse(Document): + NAME_FIELD = 'name' + + meta = {'allow_inheritance': False, 'collection': 'm3uparse', 'auto_create_index': False} + name = StringField(unique=True, max_length=constants.MAX_STREAM_NAME_LENGTH, + min_length=constants.MIN_STREAM_NAME_LENGTH, + required=True) + tvg_id = ListField(StringField(unique=True), default=[]) + tvg_logo = ListField(StringField(unique=True), default=[]) + group = ListField(StringField(unique=True), default=[]) diff --git a/app/autofill/view.py b/app/autofill/view.py new file mode 100644 index 0000000..d3301a1 --- /dev/null +++ b/app/autofill/view.py @@ -0,0 +1,73 @@ +from flask_classy import FlaskView, route +from flask import request, jsonify, render_template, redirect, url_for +from flask_login import login_required + +from pyfastocloud_models.utils.m3u_parser import M3uParser +from app.common.service.forms import UploadM3uForm +import pyfastocloud_models.constants as constants +from app.autofill.entry import M3uParse +from pyfastocloud_models.utils.utils import is_valid_http_url + + +# routes +class M3uParseView(FlaskView): + route_base = '/m3uparse/' + + @login_required + def show(self): + m3u = M3uParse.objects() + return render_template('autofill/show.html', m3u=m3u) + + @route('/search/', methods=['GET']) + @login_required + def search(self, sid): + lines = M3uParse.objects(id=sid) + line = lines.first() + if line: + return jsonify(status='ok', line=line), 200 + + return jsonify(status='failed', error='Not found'), 404 + + @route('/upload_files', methods=['POST']) + @login_required + def upload_files(self): + form = UploadM3uForm() + if form.validate_on_submit(): + files = request.files.getlist("files") + for file in files: + m3u_parser = M3uParser() + data = file.read().decode('utf-8') + m3u_parser.load_content(data) + m3u_parser.parse() + + for file in m3u_parser.files: + title = file['title'] + if len(title) > constants.MAX_STREAM_NAME_LENGTH: + continue + + line = M3uParse.objects(name=title).first() + if not line: + line = M3uParse(name=title) + + tvg_id = file['tvg-id'] + if len(tvg_id) < constants.MAX_STREAM_TVG_ID_LENGTH: + line.tvg_id.append(tvg_id) + + tvg_group = file['tvg-group'] + if len(tvg_group) < constants.MAX_STREAM_GROUP_TITLE_LENGTH: + line.group.append(tvg_group) + + tvg_logo = file['tvg-logo'] + if len(tvg_logo) < constants.MAX_URL_LENGTH: + if is_valid_http_url(tvg_logo, timeout=0.1): + line.tvg_logo.append(tvg_logo) + + line.save() + + return redirect(url_for('M3uParseView:show')) + + @login_required + @route('/upload_m3u', methods=['POST', 'GET']) + def upload_m3u(self): + form = UploadM3uForm() + return render_template('autofill/upload_m3u.html', form=form) diff --git a/app/templates/autofill/show.html b/app/templates/autofill/show.html new file mode 100644 index 0000000..fe677d1 --- /dev/null +++ b/app/templates/autofill/show.html @@ -0,0 +1,100 @@ +{% extends 'layouts/layout_user.html' %} +{% from 'bootstrap/wtf.html' import form_field %} + +{% block title %} +M3U | {{ config['PUBLIC_CONFIG'].site.title }} +{% endblock %} +{% block styles %} + +{{super()}} +{% endblock %} +{% block content %} +
+
+

+ +
Version: {{ config['PUBLIC_CONFIG'].project.version }}
+

+
+
+
+
+
+

M3u

+
+ + +
+
+
+ + + + + + + + + + {% for line in m3u %} + + + + + + {% endfor %} + +
#NameActions
{{ loop.index }}{{ line.name }} + + Show + +
+
+
+ +
+
+
+ +{% endblock %} + +{% block scripts %} +{{ super() }} +{% endblock %} diff --git a/app/templates/autofill/upload_m3u.html b/app/templates/autofill/upload_m3u.html new file mode 100644 index 0000000..2e2e08b --- /dev/null +++ b/app/templates/autofill/upload_m3u.html @@ -0,0 +1,45 @@ +{% extends 'layouts/layout_home.html' %} +{% block title %} +Upload m3u | {{ config['PUBLIC_CONFIG'].site.title }} +{% endblock %} + + +{% block content %} +
+
+

+ +
Version: {{ config['PUBLIC_CONFIG'].project.version }}
+

+
+
+
+
+

Upload m3u files

+

Note: Please upload m3u files for service.

+ {{ util.flashed_messages(dismissible=True, container=False) }} +
+ {{ form.hidden_tag() }} +
+ {{ form.files }} +
+
+ Type: {{ form.type }} +
+
+ {{ form_field(form.upload, class="btn btn-success") }} +
+
+
+ +
+
+
+{%- endblock %} \ No newline at end of file diff --git a/app/templates/provider/dashboard.html b/app/templates/provider/dashboard.html index abe17af..b5e2b15 100644 --- a/app/templates/provider/dashboard.html +++ b/app/templates/provider/dashboard.html @@ -86,6 +86,12 @@ Dashboard | {{ config['PUBLIC_CONFIG'].site.title }} {% endif %} +
+ + Meta + +
Logout