Initial commit

This commit is contained in:
Fergal Moran
2017-09-19 12:15:38 +01:00
parent 5335c41f93
commit 7df79436e1
8 changed files with 168 additions and 0 deletions

25
.gitignore vendored Normal file
View File

@@ -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

1
.virtualenv Normal file
View File

@@ -0,0 +1 @@
meeseek

5
requirements.txt Normal file
View File

@@ -0,0 +1,5 @@
flask
livereload
plexapi
ipython
ipdb

51
server.py Normal file
View File

@@ -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')

0
servers/__init__.py Normal file
View File

3
servers/base.py Normal file
View File

@@ -0,0 +1,3 @@
class _ServerHook(object):
pass

21
servers/plex.py Normal file
View File

@@ -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()

62
views/index.html Normal file
View File

@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css"
integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Hello, sailor!</h1>
<form id="recommend-movie-form">
<div class="form-group">
<label for="movie_name">Movie Name: </label>
<input type="text" name="movie_name">
</div>
<button class="btn" type="submit">Send</button>
</form>
</div>
<!-- jQuery first, then Tether, then Bootstrap JS. -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"
integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"
integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn"
crossorigin="anonymous"></script>
<script>
function convertForm(formArray) {
var returnArray = {};
for (var i = 0; i < formArray.length; i++) {
returnArray[formArray[i]['name']] = formArray[i]['value'];
}
return returnArray;
}
var form = $("#recommend-movie-form");
form.submit(function (e) {
e.preventDefault();
var data = JSON.stringify(convertForm(form.serializeArray()));
console.log(data);
$.ajax({
type: "POST",
url: "/recommend_movie/",
dataType: 'json',
data: data,
contentType: "application/json"
});
});
</script>
</body>
</html>