Skip to content

Instantly share code, notes, and snippets.

@jkoelker
Forked from kjmancuso/xbmc.py
Last active December 30, 2015 10:09
Show Gist options
  • Save jkoelker/7813749 to your computer and use it in GitHub Desktop.
Save jkoelker/7813749 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import MySQLdb as mdb
import MySQLdb.cursors
import datetime
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
import sqlalchemy as sa
from sqlalchemy.sql.expression import cast
## DB Conf
db_host = 'localhost'
db_user = 'root'
db_pass = None
db_schema = 'MyVideos75'
## Pathing config
thumbRoot = "http://192.168.1.20/xbmcthumbs"
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s/%s' % (db_user,
db_pass,
db_host,
db_schema)
db = SQLAlchemy(app)
class NumericString(sa.types.TypeDecorator):
impl = sa.types.Numeric
def column_expression(self, col):
return cast(col, sa.Integer)
class Tag(db.Model):
__tablename__ = 'tag'
class TagLink(db.Model):
__tablename__ = 'taglinks'
class Art(db.Model):
__tablename__ = 'art'
class Movie(db.Model):
__tablename__ = 'movie'
id = db.Column('idMovie', db.Integer, primary_key=True)
title = db.Column('c00', db.Text)
plot = db.Column('c01', db.Text)
plot_outline = db.Column('c02', db.Text)
tagline = db.Column('c03', db.Text)
votes = db.Column('c04', db.Text)
rating = db.Column('c05', db.Text)
writers = db.Column('c06', db.Text)
year = db.Column('c07', db.Text)
thumbnails = db.Column('c08', db.Text)
imdb_id = db.Column('c09', db.Text)
sort_title = db.Column('c10', db.Text)
runtime = db.Column('c11', NumericString)
mpaa_rating = db.Column('c12', db.Text)
imdb_ranking = db.Column('c13', db.Text)
genre = db.Column('c14', db.Text)
director = db.Column('c15', db.Text)
orig_title = db.Column('c16', db.Text)
thumb_spoof = db.Column('c17', db.Text)
studio = db.Column('c18', db.Text)
trailer_url = db.Column('c19', db.Text)
fanart_urls = db.Column('c20', db.Text)
country = db.Column('c21', db.Text)
id_path = db.Column('c23', db.Text)
file_id = db.Column('idFile', db.Integer)
@app.route('/')
def home():
navContext = {'main': 'Home', 'bread': [['/', 'Home']]}
mediaCount = {'tveps': 0, 'songs': 0}
res = db.session.query(sa.func.count(Movie.id), sa.func.sum(Movie.runtime))
mediaCount['movies'] = len(res[0])
mediaCount['moviesRunTime'] = datetime.timedelta(0, res[1])
return render_template('home.html', navContext=navContext,
mediaCount=mediaCount)
@app.route('/movies')
def movies():
navContext = {'main': 'Movies', 'bread': [['/', 'Home'],
['/movies', 'Movies']]}
info = {}
con = mdb.connect(host=db_host, user=db_user, db=db_schema,
cursorclass=MySQLdb.cursors.DictCursor, charset='utf8')
cur = con.cursor()
cur.execute('select idMedia from taglinks where idTag = 1 and '
'media_type = "movie"')
tags = cur.fetchall()
cur.execute('select idMovie, c07 AS "year", c09 as "imdb", '
'c11 AS "runtime", c16 AS "title", c22 AS "path" from movie')
movies = cur.fetchall()
movies = sorted(movies, key=lambda x: x['title'])
cur.execute('select url, media_id, art_id from art '
'where media_type = "movie" and type = "poster"')
con.close()
thumbs = cur.fetchall()
for item in movies:
thumb_tuple = [row for row in thumbs
if row['media_id'] == item['idMovie']]
item['runtime'] = "%02d:%02d" % divmod(int(item['runtime']), 60)
if len(thumb_tuple) > 0:
item['poster'] = getPoster(thumb_tuple[0]['url'])
if [row for row in tags if row['idMedia'] == item['idMovie']]:
item['tagged'] = True
info['count'] = len(movies)
return render_template('movies.html', navContext=navContext, movies=movies,
info=info)
@app.route('/movie/<moveIdx>')
def movieView(idx):
return 'null'
@app.route('/tvshows')
def tvshows():
navContext = {'main': 'TV Shows', 'bread': [['/', 'Home'],
['/tvshows', 'TV Shows']]}
return render_template('tvshows.html', navContext=navContext)
@app.route('/music')
def music():
navContext = {'main': 'Music', 'bread': [['/', 'Home'],
['/music', 'Music']]}
return render_template('music.html', navContext=navContext)
def getPoster(input_str):
# TODO: Run through PIL to make small res thumbs? -kevin
fileHash = get_crc32(input_str)
poster = '%s/%s/%s.jpg' % (thumbRoot, fileHash[0], fileHash)
return poster
def get_crc32(string):
string = string.lower()
bytes = bytearray(string.encode())
crc = 0xffffffff
for b in bytes:
crc = crc ^ (b << 24)
for i in range(8):
if (crc & 0x80000000):
crc = (crc << 1) ^ 0x04C11DB7
else:
crc = crc << 1
crc = crc & 0xFFFFFFFF
return '%08x' % crc
if __name__ == "__main__":
app.debug = True
app.run(host='0.0.0.0', port=9712)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment