-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
161 lines (117 loc) · 4.52 KB
/
app.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
153
154
155
156
157
158
159
160
161
from flask import Flask, jsonify, redirect, render_template, request, session, url_for
from tempfile import mkdtemp
from helpers import get_users, check_server, check_activity, get_movies, get_playlists, get_playlist_movies, login_required, get_sections
from playlist import add_playlist_to_plex
from plexapi.server import PlexServer
import json
import traceback
# init flask
app = Flask(__name__)
# configurations for session
app.config["SECRET_KEY"] = "07id2CFJSpRY5sQ3KEoZTjb6hoNfSFcI&&/a..#¤%//aaborpp"
app.config["SESSION_PERMANENT"] = False
@app.route("/connect", methods=["GET", "POST"])
def connect():
if request.method == "GET":
return render_template("connect.html.jinja")
else:
plex_url = request.form.get("plex-url")
plex_token = request.form.get("plex-token")
# check if plex server is available
if check_server(plex_url, plex_token):
session["plex_token"] = plex_token
session["plex_url"] = plex_url
return redirect("/")
else:
return redirect("/tryagain")
@app.route("/about")
def about():
return render_template("about.html.jinja")
@app.route('/')
@login_required
def index():
return render_template("index.html.jinja")
@app.route('/server')
@login_required
def server():
users = get_users(session["plex_url"], session["plex_token"])
return render_template("server.html.jinja", users=users)
@app.route('/playlists')
@login_required
def playlists():
plex_playlists = get_playlists(session["plex_url"], session["plex_token"])
playlists = []
for i, p_list in enumerate(plex_playlists):
playlists.append({"title": p_list.title,
"id": f'playlist-{i}',
})
return render_template("playlists.html.jinja", playlists=playlists)
@app.route('/addplaylist')
@login_required
def addplaylist():
sections = get_sections(session["plex_url"], session["plex_token"])
users = get_users(session["plex_url"], session["plex_token"])
data = {"sections": sections,
"users": users}
return render_template('addplaylist.html', data=data)
@app.route('/disconnect')
def disconnect():
session.clear()
return redirect("/")
@app.route('/tryagain')
def tryagain():
return render_template("tryagain.html.jinja")
@app.route('/update_activity', methods=["POST"])
def update_activity():
# get the username from the form
users = request.form.getlist("users[]")
user_dict = {}
# get all active users from plex
activity = check_activity(session["plex_url"], session["plex_token"])
for user in users:
# set standard to false
user_dict[user] = {"active": False}
for playing in activity:
if str(playing.usernames[0]) == user:
user_dict[user] = {"user": user,
"show": playing.grandparentTitle if playing.type == "episode" else playing.title,
"active": True}
return jsonify(user_dict)
@app.route('/search', methods=["GET"])
def search():
query = request.args.get("query")
movies = get_movies(session["plex_url"], session["plex_token"], query)
movies = [movie.title for movie in movies]
return jsonify(movies=movies)
@app.route('/playdata', methods=["GET"])
def playdata():
playlist = request.args.get("playlist")
movies = get_playlist_movies(
session["plex_url"], session["plex_token"], playlist)
return jsonify(movies)
@app.route('/addplaylisttoplex', methods=["POST"])
def addplaylisttoplex():
link = "https://www.imdb.com/list/"
imdb = request.form.get('imdb')
name = request.form.get('name')
section = request.form.get('section')
users = json.loads(request.form.get('users'))
data = {"success": False,
"error": "Something went wrong. Could not add the playlist to plex. Please try again.",
"failed": []}
try:
failed_movies = add_playlist_to_plex(
session["plex_url"], session["plex_token"], link + imdb, name, section, users)
data["success"] = True
data["error"] = None
data["failed"] = failed_movies
except IndexError:
# gets raised when the list cannot be scraped.
data["error"] = "Wrong list ID, could not retrieve data."
except NameError as e:
# gets raised when a user cannot access the library
data["error"] = str(e)
except Exception as e:
# print error message to trace error
print(traceback.print_exc())
return jsonify(data)