diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1f3a444 --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +.DS_Store +.env +.flaskenv +*.pyc +*.pyo +env +env* +dist +build +*.egg +*.egg-info +_mailinglist +.tox +.cache/ +.idea/ +__pycache__/ +.ropeproject/ + +# Coverage reports +htmlcov +.coverage +.coverage.* +*,cover + +settings.cfg diff --git a/.virtualenv b/.virtualenv new file mode 100644 index 0000000..4386316 --- /dev/null +++ b/.virtualenv @@ -0,0 +1 @@ +meeseek diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9045973 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +flask +livereload +plexapi +ipython +ipdb diff --git a/server.py b/server.py new file mode 100644 index 0000000..daf65ee --- /dev/null +++ b/server.py @@ -0,0 +1,51 @@ +import os +from flask import Flask, request, Response +from livereload import Server + +from servers.plex import PlexHook + +app = Flask(__name__, instance_relative_config=False, + static_url_path='/', static_folder='views') +app.config.update( + DEBUG=True, + PLEX_HOST='', + PLEX_API_KEY='' +) +app.config.from_pyfile('settings.cfg') + + +@app.route('/') +def index(): + return app.send_static_file('index.html') + + +@app.route('/recommend_movie/', methods=['POST']) +def recommend_movie(): + try: + content = request.json + if 'movie_name' in content and content['movie_name'] != '': + plex = PlexHook(app.config['PLEX_HOST'], app.config['PLEX_API_KEY']) + results = plex.find_movie(content['movie_name']) + for r in results: + location = r.locations[0] + source_dir = os.path.dirname(location) + target_dir = \ + os.path.join(app.config['LINK_DIR'], r.title) + if not os.path.isfile(target_dir): + os.symlink(source_dir, target_dir) + + plex.update_library() + + return Response('Succesfully added item', 200) + return Response('movie-name key not present', 400) + except Exception as ex: + print(ex) + return Response(ex, 500) + + +if __name__ == '__main__': + print('Using {} and {}'.format(app.config['PLEX_HOST'], + app.config['PLEX_API_KEY'])) + server = Server(app.wsgi_app) + # server.watch + server.serve(host='0.0.0.0') diff --git a/servers/__init__.py b/servers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/servers/base.py b/servers/base.py new file mode 100644 index 0000000..b8146cb --- /dev/null +++ b/servers/base.py @@ -0,0 +1,3 @@ +class _ServerHook(object): + pass + diff --git a/servers/plex.py b/servers/plex.py new file mode 100644 index 0000000..b6b25f0 --- /dev/null +++ b/servers/plex.py @@ -0,0 +1,21 @@ +from plexapi.server import PlexServer + +from servers.base import _ServerHook + +# Todo: remove these to settings + + +class PlexHook(_ServerHook): + def __init__(self, plex_host, plex_token): + self._plex = PlexServer(plex_host, plex_token) + + def find_movie(self, criteria, mediatype='movie'): + ret = [] + for video in self._plex.search(criteria, mediatype=mediatype): + print(video) + ret.append(video) + return ret + + def update_library(self, name='Recommended'): + library = self._plex.library.section(name) + library.update() diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000..8af60e4 --- /dev/null +++ b/views/index.html @@ -0,0 +1,62 @@ + + + + + + + + + + + +
+

Hello, sailor!

+
+
+ + +
+ + +
+
+ + + + + + + + + + + \ No newline at end of file