Saturday, December 9, 2017

compare imdb top 250 list


from imdb import IMDb
import sys
import yaml

ia = IMDb()


def samplecode():

    # get a movie and print its director(s)
    the_matrix = ia.get_movie('0133093')
    print(the_matrix['director'])

    # show all the information sets avaiable for Movie objects
    print(ia.get_movie_infoset())

    # update a Movie object with more information
    ia.update(the_matrix, ['technical'])
    # show which keys were added by the information set
    print(the_matrix.infoset2keys['technical'])
    # print one of the new keys
    print(the_matrix.get('cinematographic process'))

    # search for a person
    for person in ia.search_person('Mel Gibson'):
        print(person.personID, person['name'])

    # get the first result of a company search,
    # update it to get the basic information
    ladd_company = ia.search_company('The Ladd Company')[0]
    ia.update(ladd_company)
    # show the available information and print some
    print(ladd_company.keys())
    print(ladd_company.get('production companies'))

    # get 5 movies tagged with a keyword
    dystopia = ia.get_keyword('dystopia', results=5)

    # get a Character object
    # deckard = ia.search_character('Rick Deckard')[0]
    # ia.update(deckard)
    # print(deckard['full-size headshot'])

    # get top250 and bottom100 movies
    top250 = ia.get_top250_movies()
    bottom100 = ia.get_bottom100_movies()


def gettoplist(listtype):
    if listtype == "movie":
        top250 = ia.get_top250_movies()
    elif listtype == "tv":
        top250 = ia.get_top250_movies()
    return top250


# top250 = gettoplist("movie")

# print(top250)
# print(dir(top250))

comm  = sys.argv[1]

def parselist(mlist):
    mdict = {}
    for i in mlist:
        mid = str(i.movieID)
        mtitle = i.data['title'] + "-" + str(i.data['year'])
        # print (mid,mtitle)
        mdict[mid] =mtitle
    return mdict


if comm == "getlist":
    top250 = gettoplist("movie")
    # with open("movielist", "w") as f:
    #     # f.write(str(top250))
    #     pass
    mdict = parselist(top250)

    with open('mlist.yml', 'w') as outfile:
        yaml.dump(mdict, outfile, default_flow_style=False)
elif comm == "updatelist":
    top250 = gettoplist("movie")
    # with open("movielist", "w") as f:
    #     # f.write(str(top250))
    #     pass
    mdict = parselist(top250)

    with open('mlist.yml', 'w') as outfile:
        yaml.dump(mdict, outfile, default_flow_style=False)

elif comm == "compare":
    mlist = "mlist.yml"
    with open(mlist, "r") as f:
        mlist = yaml.load(f)

    top250 = gettoplist("movie")
    newmdict = parselist(top250)
    newadded = []
    removed = []
    for i in mlist:
        if i not in newmdict.keys():
            removed.append(mlist[i])

    for j in newmdict:
        if j not in mlist.keys():
            newadded.append(newmdict[j])

    print("new added list")
    print("\n".join(newadded))
    print ("--" * 10)
    print("removed list")
    print("\n".join(removed))




1- usage

- to get the initial list
./imdb getlist


- to compare the list to the new version
./imdb compare

No comments:

Post a Comment