dawoudt / JustWatchAPI

Python 3 JustWatch.com API - https://justwatch.com
MIT License
326 stars 45 forks source link

get_providers for only a specific show/movie #19

Closed RamyaChandra01 closed 6 years ago

RamyaChandra01 commented 6 years ago

Hey first off thanks for this!

I am trying to use this to find the availability of a film or show on various streaming sites for some economics research.

I want to be able to enter a tv/film and get a list of only the free streaming providers.

I just have this so far (which works): just_watch = JustWatch(country='US')

while True: x = input('Enter TV Series or Film Title: ') results = just_watch.search_for_item(query= x ) print(results) if x.strip() == 'quit()': break

Is there away to only return the streaming providers with a monetization_type of 'flatrate' instead of getting all the query results?

draogn commented 6 years ago

Sort of but it doesn't fully restrict as you might expect and you'll have to further process.

you can add monetization_types=["flatrate","rent","buy","free","ads"] (restrict to what you want) to the call

results = just_watch.search_for_item(query= x , monetization_types=["flatrate","free"])

You'll get back all the titles that match and are available via the monetization_types you've specifed. But the offers key contains a list of all the sources for that title including rent., buy, free etc..

What I've done myself is just looped through the results and initially restricted to any flatrate and free providers, and added to a list for further processing:

for this_item in results['items']:
    try:
        returned_name = this_item['title']
    except:
        returned_name = ''

    if clean_string(returned_name) == clean_string(line.rstrip()):
        try:
            for this_offer in results['items'][0]['offers']:
                if this_offer['monetization_type'] in ['flatrate','free']:
                    add_to_list(provider_id, str(this_offer['provider_id']))
        except:
            pass

I've set up a clean_string function to strip out punctuation from the comparison, as I've found a bit of variation in the returns - e.g. an & one day comes back as 'and' another. Looks like I also had an issue with duplicates coming back. Hope this helps.

RamyaChandra01 commented 6 years ago

Thanks so much! Here's what I modified it to:

just_watch = JustWatch(country='US')

def clean_string(returned_name): return returned_name.strip() def add_to_list(provider_id, this_offer): return provider_id.append(this_offer)

flag = True

while (flag): x = input('Enter TV Series or Film Title: ') provider_id = [] results = just_watch.search_for_item(query= x, monetization_types=["flatrate","free","ads"])

    for this_item in results['items']:
        try:
            returned_name = this_item['title']
        except:
            returned_name = ''

        if clean_string(returned_name) == clean_string(x.rstrip()):
            print(returned_name)    
            try:
                for this_offer in results['items'][0]['offers']:
                    if this_offer['monetization_type'] in ['flatrate','free']:
                        add_to_list(provider_id, str(this_offer['provider_id']))      
            except:
                pass
    print('[%s]' % ', '.join(map(str, provider_id)))
    if x.strip() == 'quit()':
            flag = False
draogn commented 6 years ago

Great. FWIW here's where I got to with my clean string function. Initially tried using string.punctuation but it didn't pick up all characters and after a couple of attempts used a regular expression instead. I could do with also adding a .strip() to this

def clean_string(input_str):

    """
    >>> clean_string("a.")
    'a'
    >>> clean_string(";'!'$£$%^*)a.")
    'a'
    >>> clean_string('robot & frank')
    'robot and frank'
    """

    #change ampersand to and
    new_string = input_str.replace("&", "and")

    #strip out all the punctuation
    #source https://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python
    #note using string.punctuation didn't trap £
    if False:
        table = str.maketrans(dict.fromkeys(string.punctuation))
        #string.punctuation doesn't return GBP pound symbol
        table['£'] = None
        new_string = new_string.translate(table)
    else:
        import re
        new_string = re.sub(r'[^\w\s]','',new_string)

    return new_string.lower()