from __future__ import print_function
from plexapi.server import PlexServer
from plexapi.myplex import MyPlexUser
from plexapi.exceptions import NotFound
import json
import sys
import MySQLdb as mdb

db_conn = None
json_config = None
with open(sys.argv[1]) as data_file:
    json_config = json.load(data_file)

def return_movie_db():
    movie_data = []

    user = MyPlexUser.signin(
            json_config['plex']['user'],
            json_config['plex']['password']
            )

    servers = user.resources()

    db_conn = None
    for server in servers:
        print("Connecting to: " + server.name)
        try:
            instance = server.connect()

            for section in instance.library.sections():
                for movie in section.all():
                    movie_data.append({
                        "title": unicode(movie.title).encode('utf-8'),
                        "summary": unicode(movie.summary).encode('utf-8'),
                        "art": unicode(movie.art).encode('utf-8'),
                        "server": unicode(instance.friendlyName).encode('utf-8')
                        })
        except Exception:
            print("Couldn't connect to: " + server.name)

    return movie_data

def connect_db():
    return mdb.connect(**json_config['mysql'])

def query_2(cursor, sql):
    global db_conn
    db_conn.ping(True)
    try:
        cursor.execute(sql)
    except Exception:
        db_conn = connect_db()
        cursor.execute(sql)

def query_3(cursor, sql, params):
    global db_conn
    db_conn.ping(True)
    try:
        cursor.execute(sql, params)
    except Exception:
        db_conn = connect_db()
        cursor.execute(sql, params)

def create_database():
    global db_conn
    db_conn = connect_db()
    c = db_conn.cursor()

    query_2(c, '''create table movieList
(rowid INT NOT NULL AUTO_INCREMENT PRIMARY KEY, title text, summary text, art
text, server text)''')

    db_conn.commit()

    c.close()

def clear_database():
    global db_conn
    db_conn = connect_db()
    c = db_conn.cursor()

    query_2(c, '''drop table movieList''')

    db_conn.commit()

    c.close()

def populate_database():
    global db_conn
    db_conn = connect_db()
    c = db_conn.cursor()

    movie_data = return_movie_db()

    for movie in movie_data:
        query_3(c, '''insert into movieList (title, summary, art, server)
values (%s, %s, %s, %s)''', (movie['title'], movie['summary'], movie['art'],
            movie['server']))

    db_conn.commit()
    c.close()

def main():
    print(json.dumps(return_movie_db(), indent=4), file=sys.stderr)

if __name__ == '__main__':
    main()

@@ -147,16 +147,37 @@
 otFound%0A
+import plexapi.video%0A
 import j
@@ -603,32 +603,40 @@
 necting to: %22 + 
+unicode(
 server.name)%0A   
@@ -631,16 +631,33 @@
 er.name)
+.encode('utf-8'))
 %0A       
@@ -799,24 +799,253 @@
 tion.all():%0A
+                    link = None%0A                    if type(movie) is plexapi.video.Movie:%0A                        link = unicode(movie.getStreamUrl()).encode('utf-8')%0A                    else:%0A                        link = %22%22%0A%0A
             
@@ -1249,16 +1249,29 @@
  %22art%22: 
+instance.url(
 unicode(
@@ -1288,32 +1288,33 @@
 .encode('utf-8')
+)
 ,%0A              
@@ -1332,17 +1332,16 @@
 server%22:
- 
 unicode(
@@ -1370,32 +1370,140 @@
 .encode('utf-8')
+,%0A                        %22type%22: unicode(type(movie)).encode('utf-8'),%0A                        %22link%22: link
 %0A               
@@ -1584,16 +1584,24 @@
 to: %22 + 
+unicode(
 server.n
@@ -1604,16 +1604,33 @@
 er.name)
+.encode('utf-8'))
 %0A%0A    re
@@ -2341,16 +2341,164 @@
 ver text
+, type text, link text)''')%0A%0A    query_2(c, '''ALTER TABLE movieList ENGINE = MYISAM''')%0A%0A    query_2(c, '''ALTER TABLE movieList ADD FULLTEXT(title
 )''')%0A%0A 
@@ -2942,16 +2942,28 @@
 , server
+, type,%0Alink
 )%0Avalues
@@ -2975,16 +2975,24 @@
  %25s, %25s,
+ %25s, %25s,
  %25s)''',
@@ -3026,16 +3026,28 @@
 mmary'%5D,
+%0A           
  movie%5B'
@@ -3052,28 +3052,16 @@
 %5B'art'%5D,
-%0A           
  movie%5B'
@@ -3068,16 +3068,46 @@
 server'%5D
+, movie%5B'type'%5D, movie%5B'link'%5D
 ))%0A%0A    
@@ -3134,24 +3134,68 @@
  c.close()%0A%0A
+    print(%22Completed Upload Successfully%22)%0A%0A
 def main():%0A

fix fulltext search