def watchlist_purge():
    """ Removes movies from watchlist if there is currently a rating """

    for movie in sess.query(Movie).filter(Movie.rating > 1).all():
        print movie.name
        movie.watchlist = '0'
    sess.commit()
def quick_edit_movie(movie_id):
    edited_movie = sess.query(Movie).filter_by(movie_id=movie_id).one()

    if 'email' in login_session:
        if login_session['email'] == '*****@*****.**':
            if request.method == 'POST':
                if request.form['rating']:
                    edited_movie.rating = request.form['rating']
                if request.form['publish']:
                    edited_movie.publish = request.form['publish']
                sess.add(edited_movie)
                try:
                    sess.commit()
                except exc.SQLAlchemyError:
                    flash("SQlalchemy error, session rollback initiated")
                    sess.rollback()

                return redirect(url_for('show_admin_movie_list'))
            else:
                return render_template('edit_rating.html', movie=edited_movie)
        else:
            flash("You are Not Authorized to View the Admin Page")
            return redirect('/movie/')

    else:
        flash("You are Not Authorized to View the Admin Page")
        return redirect('/movie/')
def update_movie():
    for movie in sess.query(Movie).filter(Movie.getStream != "x").all():

        movie.getStream = "x"
        print movie.getStream
        print movie.name + "updated"
    sess.commit()
def update_movie():
    for movie in sess.query(Movie).filter(Movie.rating > 0).all():
        omdbSearch = omdb.get(imdbid=movie.movie_id, fullplot=True)
        if omdbSearch.runtime != 'N/A':
            rtime = omdbSearch.runtime
            rtime = int(strip_numb(rtime))
            print rtime
        else:
            rtime = None
        movie.time = rtime
    sess.commit()
def get_guide_sources():
    """
    function reads in two columns of CSV seed data (guide id & imdb id) and commits to database
    """
    csv_file = os.path.join(APP_STATIC, 'CSV/sourcesMaster.csv')
    file_object = codecs.open(csv_file, encoding='utf-8')
    csv_ids = csv.reader(file_object)
    for row in csv_ids:
        guide_id = row[0]
        tt_id = row[1]
        if guide_id != "NA":
            for movie in sess.query(Movie).filter(Movie.movie_id == tt_id).all():
                movie.guideID = guide_id
                print guide_id
            sess.commit()
def edit_movie(movie_id):

    if 'email' in login_session:
        if login_session['email'] == '*****@*****.**':
            edited_movie = sess.query(Movie).filter_by(movie_id=movie_id).one()
            if request.method == 'POST':
                if request.form['name']:
                    edited_movie.name = request.form['name']
                if request.form['poster']:
                    edited_movie.poster = request.form['poster']
                if request.form['year']:
                    edited_movie.year = request.form['year']
                if request.form['rating']:
                    edited_movie.rating = request.form['rating']
                if request.form['actors']:
                    edited_movie.actors = request.form['actors']
                if request.form['director']:
                    edited_movie.director = request.form['director']
                if request.form['plot']:
                    edited_movie.plot = request.form['plot']
                if request.form['mtype']:
                    edited_movie.mtype = request.form['mtype']
                if request.form['publish']:
                    edited_movie.publish = request.form['publish']
                if request.form['stream']:
                    edited_movie.getStream = request.form['stream']
                sess.add(edited_movie)
                if request.form['guideid']:
                    edited_movie.guideID = request.form['guideid']
                if request.form['criterion']:
                    edited_movie.criterion = request.form['criterion']
                try:
                    sess.commit()
                except exc.SQLAlchemyError:
                    flash("SQlalchemy error, session rollback initiated")
                    sess.rollback()

                return redirect(url_for('show_admin_movies'))
            else:
                return render_template('edit_movie.html', movie=edited_movie)

        else:
            flash("You are Not Authorized to View the Admin Page")
            return redirect('https://www.google.com')

    else:
        flash("You are Not Authorized to View the Admin Page")
        return redirect('/movie/')
def get_movie():
    """
    parses out downloaded IMDB watchlist CSV file, creates list object
    to import data to postgres.
    """
    csv_file = os.path.join(APP_STATIC, 'CSV/watchlist.csv')
    file_object = codecs.open(csv_file, encoding='utf-8')
    csv_ratings = csv.reader(file_object)
    csv_ratings.next()

    for row in csv_ratings:
        tt_id = row[1]
        title = row[5]
        print title
        title_type = row[6]
        imdb_year = row[11]
        genre = row[12]
        director = row[7]
        imdb_rating = row[9]
        run_time = row[10]
        url = row[15]
        rating = 0
        watchlist = 'x'

        if imdb_rating == '':
            imdb_rating = 0

        if run_time == '':
            run_time = 0

        if not sess.query(Movie).filter(Movie.movie_id == tt_id).count():
            if title_type != "TV Episode":
                if title_type != "Documentary":
                    omdb_search = omdb.get(imdbid=tt_id, fullplot=True)
                    omdb_year = omdb_search.year
                    omdb_year = int(omdb_year)
                    print omdb_year
                    plot = omdb_search.plot
                    actors = omdb_search.actors
                    country = omdb_search.country

                else:
                    plot = 'NA'
                    actors = 'NA'
                    genre = 'NA'
                    country = 'NA'

                try:
                    tmdb_query = tmdb.Movies(tt_id).info()
                    time.sleep(0.01)
                    poster_path = tmdb_query['poster_path']
                    print type(poster_path)
                    if isinstance(poster_path, unicode):
                        poster = 'https://image.tmdb.org/t/p/w396' \
                                  + poster_path
                        print poster
                    else:
                        poster = STOCK_PHOTO
                        print poster

                except requests.exceptions.RequestException:
                    poster = STOCK_PHOTO

                mov = Movie(movie_id=tt_id, name=title, year=imdb_year,
                            genre=genre, IMDBrating=imdb_rating,
                            poster=poster, plot=plot, time=run_time,
                            actors=actors, director=director, country=country,
                            url=url, rating=rating, watchlist=watchlist)
                sess.add(mov)
            sess.commit()
        else:
            for movie in sess.query(Movie).filter(Movie.movie_id == tt_id).all():
                movie.watchlist = 'x'
                print movie.name + 'open'
            sess.commit()
def update_movie():
    for movie in sess.query(Movie).all():
        criterion = "x"
        print movie.name + "updated"
        movie.criterion = criterion
    sess.commit()
def get_new_guide_sources():
    if sess.query(Movie).filter(Movie.getStream == 'x').count():

        for movie in sess.query(Movie).filter(Movie.getStream == 'x').all():
            guide_id = movie.guideID
            title = movie.name
            tt_id = movie.movie_id

            if guide_id != "NA":
                if guide_id != "":
                    if guide_id != None:
                        movie.getStream = '0'
                        sess.commit()
                        guide_search_url = guideSearch + str(guide_id)
                        g_request = requests.get(guide_search_url)
                        guide_search_json = g_request.json()
        
                        for a_movie in guide_search_json['subscription_web_sources']:
    
                            if a_movie['display_name'] == "Hulu":
                                hulu_link = a_movie['link']
                                for c_movie in sess.query(Movie).filter\
                                    (Movie.movie_id == tt_id).all():
                                    c_movie.hulu = hulu_link
                                    print "Found Hulu"                                
                                    print hulu_link
                                sess.commit()
    
                            if a_movie['display_name'] == "Amazon Prime":
                                amazon_prime_link = a_movie['link']
                                for c_movie in sess.query(Movie).filter\
                                    (Movie.movie_id == tt_id).all():
                                    c_movie.amazon_prime = amazon_prime_link
                                    print 'Found Amazon'
                                    print amazon_prime_link
                                sess.commit()
    
                            if a_movie['display_name'] == "Fandor":
                                fandor_link = a_movie['link']
                                for c_movie in sess.query(Movie).filter\
                                    (Movie.movie_id == tt_id).all():
                                    c_movie.fandor = fandor_link
                                    print 'Found Fandor'
                                    print fandor_link
                                sess.commit()
    
                        for a_movie in guide_search_json['free_web_sources']:
                            if a_movie['display_name'] == "Crackle":
                                crackle_link = a_movie['link']
                                for c_movie in sess.query(Movie).filter\
                                    (Movie.movie_id == tt_id).all():
                                    c_movie.crackle = crackle_link
                                    print 'Found Crackle'
                                    print crackle_link
                                sess.commit()
                            if a_movie['display_name'] == "Vimeo":
                                vimeo_link = a_movie['link']
                                for c_movie in sess.query(Movie).filter\
                                    (Movie.movie_id == tt_id).all():
                                    c_movie.vimeo = vimeo_link
                                    print 'Found Vimeo'
                                    print vimeo_link
                                sess.commit()
    
                            if a_movie['display_name'] == "YouTube":
                                youtube_link = a_movie['link']
                                for c_movie in sess.query(Movie).filter\
                                    (Movie.movie_id == tt_id).all():
                                    c_movie.youtube = youtube_link
                                    print 'Found YouTube'
                                    print youtube_link
                                sess.commit()
    
                        for a_movie in guide_search_json['purchase_web_sources']:
                            if a_movie['display_name'] == "Amazon":
                                amazon_link = a_movie['link']
                                for c_movie in sess.query(Movie).filter\
                                    (Movie.movie_id == tt_id).all():
                                    c_movie.amazon = amazon_link
                                    print 'Found Amazon'
                                    print amazon_link
                                sess.commit()
                            if a_movie['display_name'] == "iTunes":
                                itunes_link = a_movie['link']
                                for c_movie in sess.query(Movie).filter\
                                    (Movie.movie_id == tt_id).all():
                                    c_movie.itunes = itunes_link
                                    print 'Found ITunes'
                                    print itunes_link
                                sess.commit()
    
                            if a_movie['display_name'] == "YouTube":
                                youtube_prem_link = a_movie['link']
                                for c_movie in sess.query(Movie).filter\
                                    (Movie.movie_id == tt_id).all():
                                    c_movie.youtube_premium = youtube_prem_link
                                    print 'Found YouTube Premium'
                                    print youtube_prem_link
                                sess.commit()
                            if a_movie['display_name'] == "Google":
                                googleplay_link = a_movie['link']
                                for c_movie in sess.query(Movie).filter\
                                    (Movie.movie_id == tt_id).all():
                                    c_movie.googleplay = googleplay_link
                                    print 'Found GooglePlay'
                                    print googleplay_link
                                sess.commit()
def update_movie():
    for movie in sess.query(Movie).filter(Movie.year < 1930).all():
        movie.silent = "x"
        print movie.name + "updated"
        print movie.silent
    sess.commit()
def get_movie():

    reload(sys) # just to be sure
    sys.setdefaultencoding('utf-8')

    """
    Strips out important sections of the IMDB rss feed and uses that data
    to pull more data from omdb and tmdb APIs
    """
    csv_file = os.path.join(APP_STATIC, 'CSV/criterion.csv')
    file_object = codecs.open(csv_file, encoding='utf-8')
    csv_ratings = csv.reader(file_object)
    csv_ratings.next()

    for row in csv_ratings:
        tt_id = row[1]
        title = row[5]
        title = title.encode('utf-8')
        print title
        title_type = row[6]
        imdb_year = row[11]
        print imdb_year
        genre = row[12]
        director = row[7]
        imdb_rating = row[9]
        run_time = row[10]
        url = row[15]
        rating = 0

        if imdb_rating == '':
            imdb_rating = 0

        if run_time == '':
            run_time = 0

        guide_url = guideIMDB + tt_id
        guide_request = requests.get(guide_url)
        guide_json = guide_request.json()
        if guide_json != {}:
            guide_id = guide_json.get('id')
            get_stream = 'x'
            print get_stream
            print guide_id
        else:
            guide_id = ''
            get_stream = '0'

        if not sess.query(Movie).filter(Movie.movie_id == tt_id).count():
            if title_type != "TV Episode":
                if title_type != "":
                    print 'hi'
                    criterion = 0
                    omdb_search = omdb.get(imdbid=tt_id, fullplot=True)
                    omdb_year = omdb_search.year
                    omdb_year = int(omdb_year)
                    plot = omdb_search.plot
                    actors = omdb_search.actors
                    country = omdb_search.country
                else:
                    plot = 'NA'
                    actors = 'NA'
                    genre = 'NA'
                    country = 'NA'

                try:
                    a_query = tmdb.Movies(tt_id).info()
                    time.sleep(0.01)
                    poster_path = a_query['poster_path']
                    print type(poster_path)
                    if isinstance(poster_path, unicode):
                        poster = 'https://image.tmdb.org/t/p/w396' + poster_path
                        print poster
                    else:
                        poster = STOCK_PHOTO
                        print poster

                except requests.exceptions.RequestException:
                    poster = STOCK_PHOTO

                mov = Movie(movie_id=tt_id, name=title, year=imdb_year, genre=genre,
                            IMDBrating=imdb_rating, poster=poster, plot=plot,
                            time=run_time, actors=actors, director=director,
                            country=country, url=url, guideID=guide_id, getStream=get_stream,
                            rating=rating, criterion=criterion)
                sess.add(mov)
            sess.commit()
        else:
            for movie in sess.query(Movie).filter(Movie.movie_id == tt_id).all():
                movie.criterion = '0'
                movie.getStream = 'x'
                print movie.name + 'open'
            sess.commit()
def get_movie_watchlist():
    """
    Strips out important sections of the IMDB rss feed and uses that data
    to pull more data from omdb and tmdb APIs
    """
    reload(sys)
    sys.setdefaultencoding('utf-8')

    xml_file = os.path.join(APP_STATIC, 'CSV/w_rss')
    # defines the rss feed untangle will parse
    rss_object = untangle.parse(xml_file)

    # creates python object from XML unicode_string
    # set variable to strip out IMDB username from <description>

    for item in rss_object.rss.channel.item:
        # looping over the python object of rss data
        title = (item.title.cdata)
        # create object of <title> children
        link = (item.link.cdata)
        # create object of <link> children to use for unique id's
        if not re.search('TV Episode', title):
            if not re.search('Documentary', title):
            # use regex to remove tv show and documentary ratings
                clean_title = stripped(title)
                # removes the date and parens from the <title> children
                tt_id = strip_id(link)
                # returns only the unique 'tt' imdb id
                clean_year = strip_year(title)

                if not sess.query(Movie).filter(Movie.movie_id == tt_id).count():
                    # removes imdb username from the <description> children
                    print clean_title
                    # returns only the rating from the already stripped <description> children
                    watchlist = 'x'
                    rating = 0
                    omdb_search = omdb.get(imdbid=tt_id, fullplot=True)
                    omdb_year = omdb_search.year
                    omdb_year = int(omdb_year)

                    if omdb_year == clean_year:
                        plot = omdb_search.plot
                        actors = omdb_search.actors
                        director = omdb_search.director
                        year = int(omdb_year)
                        genre = omdb_search.genre
                        country = omdb_search.country
                        if omdb_search.imdb_rating != 'N/A':
                            imdb_rating = omdb_search.imdb_rating
                        else:
                            imdb_rating = 0.0
                        if omdb_search.runtime != 'N/A':
                            rtime = omdb_search.runtime
                            rtime = int(strip_numb(rtime))
                        else:
                            rtime = None

                        guide_url = guideIMDB + tt_id
                        guide_request = requests.get(guide_url)
                        guide_json = guide_request.json()
                        if guide_json != {}:
                            guide_id = guide_json.get('id')
                            get_stream = 'x'
                            print guide_id
                        else:
                            guide_id = ''
                            get_stream = '0'
                    else:
                        plot = 'NA'
                        actors = 'NA'
                        director = 'NA'

                    try:
                        tmdb_query = tmdb.Movies(tt_id).info()
                        time.sleep(0.01)
                        poster_path = tmdb_query['poster_path']
                        print type(poster_path)
                        if isinstance(poster_path, unicode):
                            poster = 'https://image.tmdb.org/t/p/w396' + poster_path
                            print poster
                        else:
                            poster = STOCK_PHOTO
                            print poster

                    except requests.exceptions.RequestException:
                        poster = STOCK_PHOTO

                    mov = Movie(movie_id=tt_id, name=clean_title, year=year,
                                genre=genre, IMDBrating=imdb_rating, poster=poster,
                                plot=plot, time=rtime, actors=actors, director=director,
                                country=country, url=link, watchlist=watchlist,
                                rating=rating, guideID=guide_id, getStream=get_stream)
                    sess.add(mov)
                    sess.commit()
def get_movie_ratings():
    """
    Strips out important sections of the IMDB rss feed and uses that data
    to pull more data from omdb and tmdb APIs
    """

    reload(sys)
    sys.setdefaultencoding('utf-8')
    # required to decode unicode strings from imdb rss and tmdb API

    xml_file = os.path.join(APP_STATIC, 'CSV/rss')

    # defines the rss feed untangle will parse
    rss_object = untangle.parse(xml_file)
    # creates python object from xml_file unicode_string
    # set variable to strip out IMDB username from <description>

    for item in rss_object.rss.channel.item:
        # looping over the python object of rss data
        title = (item.title.cdata)
        # create object of <title> children
        desc = (item.description.cdata)
        # create object of <description> children (ratings info)
        link = (item.link.cdata)
        # create object of <link> children to use for unique id's
        if not re.search('TV Episode', title):
            if not re.search('Documentary', title):
                # use regex to remove tv show and documentary ratings
                clean_title = stripped(title)
                # removes the date and parens from the <title> children
                tt_id = strip_id(link)
                # returns only the unique 'tt' imdb id
                clean_year = strip_year(title)
                url = IMDB + tt_id

                if not sess.query(Movie).filter(Movie.movie_id == tt_id).count():
                    d_desc = strip_name(desc)
                    # removes imdb username from the <description> children
                    print title
                    rating = strip_numb(d_desc)
                    rating = int(rating)
                    # returns only the rating from the already stripped <description> children
                    omdb_search = omdb.get(imdbid=tt_id, fullplot=True)
                    omdb_year = omdb_search.year
                    omdb_year = int(omdb_year)
                    get_stream = 'x'


                    if omdb_year == clean_year:
                        plot = omdb_search.plot
                        actors = omdb_search.actors
                        director = omdb_search.director
                        year = int(clean_year)
                        publish = 'unpublished'
                        watchlist = '0'
                        if omdb_search.imdb_rating != 'N/A':
                            imdb_rating = omdb_search.imdb_rating
                        else:
                            imdb_rating = 0.0

                        if omdb_search.runtime != 'N/A':
                            rtime = omdb_search.runtime
                            rtime = int(strip_numb(rtime))
                        else:
                            rtime = None

                        guide_url = guideIMDB + tt_id
                        guide_request = requests.get(guide_url)
                        guide_json = guide_request.json()
                        if guide_json != {}:
                            guide_id = guide_json.get('id')
                            print guide_id
                        else:
                            guide_id = ''
                            get_stream = '0'

                    else:
                        plot = 'NA'
                        actors = 'NA'
                        director = 'NA'
                        publish = 'unpublished'

                    try:
                        tmdb_query = tmdb.Movies(tt_id).info()
                        time.sleep(0.01)
                        poster_path = tmdb_query['poster_path']
                        print type(poster_path)
                        if isinstance(poster_path, unicode):
                            poster = 'https://image.tmdb.org/t/p/w396' + poster_path
                            print poster
                        else:
                            poster = STOCK_PHOTO
                            print poster

                    except requests.exceptions.RequestException:
                        poster = STOCK_PHOTO

                    mov = Movie(movie_id=tt_id, name=clean_title, year=year,\
                                rating=rating, poster=poster, plot=plot,\
                                actors=actors, director=director, publish=publish,\
                                time=rtime, IMDBrating=imdb_rating, watchlist=watchlist,\
                                url=url, getStream=get_stream)
                    sess.add(mov)

                    try:
                        sess.commit()
                        print "Successfully Updated Ratings"

                    except exc.SQLAlchemyError:
                        sess.rollback()

                if sess.query(Movie).filter(Movie.movie_id == tt_id)\
                                    .filter(Movie.watchlist == 'x').count():
                    for movie in sess.query(Movie).filter(Movie.movie_id == tt_id).all():
                        d_desc = strip_name(desc)
                        rating = strip_numb(d_desc)
                        rating = int(rating)
                        movie.rating = rating
                        movie.publish = 'unpublished'

                    sess.commit()
    for watchlist_movie in sess.query(Movie).filter(Movie.rating > 1)\
                           .filter(Movie.watchlist == 'x').all():
        print watchlist_movie.name
        watchlist_movie.watchlist = '0'


    sess.commit()
def get_movie():
    csv_file = os.path.join(APP_STATIC, 'CSV/academy_award_prod.csv')
    file_object = codecs.open(csv_file, encoding='utf-8')
    csv_ratings = csv.reader(file_object)
    csv_ratings.next()

    for row in csv_ratings:
        tt_id = row[1]
        title = row[5]
        print title
        title_type = row[6]
        imdb_year = row[11]
        print imdb_year
        genre = row[12]
        director = row[7]
        imdb_rating = row[9]
        run_time = row[10]
        url = row[15]
        rating = 0
        if imdb_rating == '':
            imdb_rating = 0

        if run_time == '':
            run_time = 0
        if not sess.query(Movie).filter(Movie.movie_id == tt_id).count():
            if title_type != "TV Episode":
                if title_type != "":
                    print 'hi'
                    academy_award_prod = 'x'
                    omdb_search = omdb.get(imdbid=tt_id, fullplot=True)
                    omdb_year = omdb_search.year
                    omdb_year = int(omdb_year)
                    plot = omdb_search.plot
                    actors = omdb_search.actors
                    country = omdb_search.country
                else:
                    plot = 'NA'
                    actors = 'NA'
                    genre = 'NA'
                    country = 'NA'

                try:
                    a_query = tmdb.Movies(tt_id).info()
                    time.sleep(0.01)
                    poster_path = a_query['poster_path']
                    print type(poster_path)
                    if isinstance(poster_path, unicode):
                        poster = 'https://image.tmdb.org/t/p/w396' + poster_path
                        print poster
                    else:
                        poster = STOCK_PHOTO
                        print poster

                except requests.exceptions.RequestException:
                    poster = STOCK_PHOTO

                mov = Movie(movie_id=tt_id, name=title, year=imdb_year, genre=genre,
                            IMDBrating=imdb_rating, poster=poster, plot=plot,
                            time=run_time, actors=actors, director=director,
                            country=country, url=url, rating=rating, academy_award_prod=academy_award_prod)
                sess.add(mov)
            sess.commit()
        else:
            for movie in sess.query(Movie).filter(Movie.movie_id == tt_id).all():
                movie.academy_award_prod = 'x'
                print movie.name + 'open'
            sess.commit()