add search for amazon
This commit is contained in:
parent
e41f1712e3
commit
5b0a1fc8ae
@ -1,7 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
usage: stream_check_instantwatcher.py [-h] [-l [...]] [-s ] [-t ]
|
usage: stream_check_instantwatcher.py [-h] [-l [...]] [-s ] [-t ]
|
||||||
|
|
||||||
Use instantwatcher.com to find if Plex items are on Netflix.
|
Use instantwatcher.com to find if Plex items are on Netflix, Amazon, or both.
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
@ -10,10 +10,18 @@ optional arguments:
|
|||||||
(choices: Your show or movie library names)
|
(choices: Your show or movie library names)
|
||||||
-s [], --search [] Search any name.
|
-s [], --search [] Search any name.
|
||||||
-t [], --type [] Refine search for name by using type.
|
-t [], --type [] Refine search for name by using type.
|
||||||
|
(choices: movie, show)
|
||||||
|
-e [], --episodes [] Refine search for individual episodes.
|
||||||
|
(choices: True, False)
|
||||||
|
(default: False)
|
||||||
|
-site [], --site [] Refine search for name by using type.
|
||||||
|
(choices: Netflix, Amazon, Both)
|
||||||
|
(default: Both)
|
||||||
|
-sl [], --search_limit []
|
||||||
|
Define number of search returns from page. Zero returns all.
|
||||||
|
(default: 5)
|
||||||
|
|
||||||
If site pulls more than 1 result, will check first 5 records.
|
If title is matched in both, Amazon is first then Netflix.
|
||||||
search_limit = 5
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@ -28,45 +36,51 @@ from plexapi.server import PlexServer
|
|||||||
|
|
||||||
## Edit ##
|
## Edit ##
|
||||||
PLEX_URL = 'http://localhost:32400'
|
PLEX_URL = 'http://localhost:32400'
|
||||||
PLEX_TOKEN = 'xxxxx'
|
PLEX_TOKEN = 'xxxx'
|
||||||
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
|
|
||||||
## /Edit ##
|
## /Edit ##
|
||||||
|
|
||||||
|
sess = requests.Session()
|
||||||
|
sess.verify = False
|
||||||
|
plex = PlexServer(PLEX_URL, PLEX_TOKEN, session=sess)
|
||||||
|
|
||||||
def instantwatch_search(name, type):
|
|
||||||
|
def instantwatch_search(name, media_type, site, search_limit):
|
||||||
|
|
||||||
NETFLIX_URL = 'http://www.netflix.com/title/'
|
NETFLIX_URL = 'http://www.netflix.com/title/'
|
||||||
search_limit = 5
|
limit = False
|
||||||
search = True
|
results_count = 0
|
||||||
|
|
||||||
if type == 'movie':
|
if media_type == 'movie':
|
||||||
content_type = '1'
|
content_type = '1'
|
||||||
elif type == 'show':
|
elif media_type == 'show':
|
||||||
content_type = '3'
|
content_type = '2'
|
||||||
|
elif media_type == 'episode':
|
||||||
|
content_type = '4'
|
||||||
else:
|
else:
|
||||||
content_type =''
|
content_type =''
|
||||||
|
|
||||||
payload = {'content_type': content_type,
|
payload = {'content_type': content_type,
|
||||||
'q': name.lower()}
|
'q': name.lower()}
|
||||||
|
|
||||||
r = requests.get('http://instantwatcher.com/search'.rstrip('/'), params=payload)
|
if site == 'Netflix':
|
||||||
|
r = requests.get('http://instantwatcher.com/search'.rstrip('/'), params=payload)
|
||||||
|
elif site == 'Amazon':
|
||||||
|
r = requests.get('http://instantwatcher.com/a/search'.rstrip('/'), params=payload)
|
||||||
|
else:
|
||||||
|
r = requests.get('http://instantwatcher.com/u/search'.rstrip('/'), params=payload)
|
||||||
|
|
||||||
results_lst = []
|
results_lst = []
|
||||||
|
|
||||||
res_data = bf.data(fromstring(r.content))
|
res_data = bf.data(fromstring(r.content))
|
||||||
|
|
||||||
# with open('data.json', 'w') as outfile:
|
|
||||||
# json.dump(res_data, outfile, indent=4, sort_keys=True)
|
|
||||||
|
|
||||||
res_data = res_data['html']['body']['div']['div'][1]
|
res_data = res_data['html']['body']['div']['div'][1]
|
||||||
|
|
||||||
# Any matches?
|
# Any matches?
|
||||||
results = res_data['div'][0]['div'][1]['div'][0]
|
res_results = res_data['div'][0]['div'][1]['div'][0]
|
||||||
title_check = res_data['div'][0]['div'][1]['div'][1]
|
title_check = res_data['div'][0]['div'][1]['div'][1]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if results['span']:
|
if res_results['span']:
|
||||||
total_results = results['span']
|
total_results = res_results['span']
|
||||||
for data in total_results:
|
for data in total_results:
|
||||||
results_lst += [data['$']]
|
results_lst += [data['$']]
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -75,6 +89,9 @@ def instantwatch_search(name, type):
|
|||||||
print('{} found {}.'.format(results_lst[0], results_lst[1]))
|
print('{} found {}.'.format(results_lst[0], results_lst[1]))
|
||||||
result_count = int(results_lst[1].split(' ')[0])
|
result_count = int(results_lst[1].split(' ')[0])
|
||||||
|
|
||||||
|
amazon_id = ''
|
||||||
|
amazon_url = ''
|
||||||
|
|
||||||
# Title match
|
# Title match
|
||||||
if result_count == 0:
|
if result_count == 0:
|
||||||
print('0 matches, moving on.')
|
print('0 matches, moving on.')
|
||||||
@ -83,30 +100,89 @@ def instantwatch_search(name, type):
|
|||||||
item_results_page = title_check['div']['div']
|
item_results_page = title_check['div']['div']
|
||||||
if result_count > 1:
|
if result_count > 1:
|
||||||
for results in item_results_page:
|
for results in item_results_page:
|
||||||
|
for data in results['a']:
|
||||||
|
try:
|
||||||
|
amazon_id = data['@data-amazon-title-id']
|
||||||
|
amazon_url = data['@data-amazon-uri']
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
for data in results['span']:
|
for data in results['span']:
|
||||||
if data['@class'] == 'title' and search is True and search_limit > 0:
|
if data['@class'] == 'title' and search_limit is not 0:
|
||||||
if data['a']['$'].lower() == name.lower():
|
if data['a']['$'].lower().startswith(name.lower()):
|
||||||
print('Match!')
|
if amazon_id:
|
||||||
print('Netflix Page: {}{}'.format(NETFLIX_URL, data['a']['@data-title-id']))
|
if data['a']['@data-title-id'] == amazon_id:
|
||||||
search = False
|
print('Match found on Amazon for {}'.format(data['a']['$']))
|
||||||
else:
|
print('Page: {}'.format(amazon_url))
|
||||||
print('Could not find exact name match.')
|
else:
|
||||||
|
print('Match found on Netflix for {}'.format(data['a']['$']))
|
||||||
|
print('Page: {}{}'.format(NETFLIX_URL, data['a']['@data-title-id']))
|
||||||
|
results_count += 1
|
||||||
search_limit -= 1
|
search_limit -= 1
|
||||||
|
if search_limit is 0:
|
||||||
|
limit = True
|
||||||
|
|
||||||
|
elif data['@class'] == 'title' and search_limit is 0 and limit is False:
|
||||||
|
if data['a']['$'].lower().startswith(name.lower()):
|
||||||
|
if amazon_id:
|
||||||
|
if data['a']['@data-title-id'] == amazon_id:
|
||||||
|
print('Match found on Amazon for {}'.format(data['a']['$']))
|
||||||
|
print('Page: {}'.format(amazon_url))
|
||||||
|
else:
|
||||||
|
print('Match found on Netflix for {}'.format(data['a']['$']))
|
||||||
|
print('Page: {}{}'.format(NETFLIX_URL, data['a']['@data-title-id']))
|
||||||
|
results_count += 1
|
||||||
|
|
||||||
elif result_count == 1:
|
elif result_count == 1:
|
||||||
|
for data in item_results_page['a']:
|
||||||
|
try:
|
||||||
|
amazon_id = data['@data-amazon-title-id']
|
||||||
|
amazon_url = data['@data-amazon-uri']
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
for data in item_results_page['span']:
|
for data in item_results_page['span']:
|
||||||
if data['@class'] == 'title':
|
if data['@class'] == 'title':
|
||||||
if data['a']['$'].lower() == name.lower():
|
if data['a']['$'].lower().startswith(name.lower()):
|
||||||
print('Match!')
|
print('Match! For {}'.format(data['a']['$']))
|
||||||
print('Netflix Page: {}{}'.format(NETFLIX_URL, data['a']['@data-title-id']))
|
if amazon_id:
|
||||||
|
if data['a']['@data-title-id'] == amazon_id:
|
||||||
|
print('Page: {}'.format(amazon_url))
|
||||||
|
else:
|
||||||
|
print('Page: {}{}'.format(NETFLIX_URL, data['a']['@data-title-id']))
|
||||||
|
results_count += 1
|
||||||
else:
|
else:
|
||||||
print('Could not find exact name match.')
|
print('Could not find exact name match.')
|
||||||
|
return results_count
|
||||||
|
|
||||||
|
|
||||||
def plex_library_search(lib_name):
|
def plex_library_search(lib_name, site, epi_search, search_limit):
|
||||||
for t in plex.library.section(lib_name).all():
|
for title in plex.library.section(lib_name).all():
|
||||||
print('Running check on {}'.format(t.title))
|
print('Running check on {}'.format(title.title))
|
||||||
instantwatch_search(t.title, t.type)
|
file_path = []
|
||||||
|
if title.type == 'show' and epi_search is True:
|
||||||
|
if instantwatch_search(title.title, title.type, site, search_limit) > 0:
|
||||||
|
print('Show was found. Searching for episode paths.')
|
||||||
|
for episode in title.episodes():
|
||||||
|
# Need to check episodes against sites to truly find episode matches.
|
||||||
|
# For now just return paths for episodes if Show name matches.
|
||||||
|
# print('Running check on {} - {}'.format(title.title, episode.title))
|
||||||
|
# show_ep = '{} - {}'.format(title.title, episode.title)
|
||||||
|
# if instantwatch_search(show_ep, 'episode', site) > 0:
|
||||||
|
file_path += [episode.media[0].parts[0].file]
|
||||||
|
|
||||||
|
elif title.type == 'movie':
|
||||||
|
if instantwatch_search(title.title, title.type, site, search_limit) > 0:
|
||||||
|
file_path = title.media[0].parts[0].file
|
||||||
|
else:
|
||||||
|
if instantwatch_search(title.title, title.type, site, search_limit) > 0:
|
||||||
|
print('Show was found but path is not defined.')
|
||||||
|
|
||||||
|
if file_path:
|
||||||
|
if type(file_path) is str:
|
||||||
|
print('File: {}'.format(file_path))
|
||||||
|
elif type(file_path) is list:
|
||||||
|
print('Files: \n{}'.format(' \n'.join(file_path)))
|
||||||
|
|
||||||
print('Waiting 5 seconds before next search.')
|
print('Waiting 5 seconds before next search.')
|
||||||
sleep(5)
|
sleep(5)
|
||||||
|
|
||||||
@ -122,20 +198,30 @@ def main():
|
|||||||
'(choices: %(choices)s)')
|
'(choices: %(choices)s)')
|
||||||
parser.add_argument('-s', '--search', metavar='', nargs='?',
|
parser.add_argument('-s', '--search', metavar='', nargs='?',
|
||||||
help='Search any name.')
|
help='Search any name.')
|
||||||
parser.add_argument('-t', '--type', metavar='', choices=['movie', 'show'], nargs='?',
|
parser.add_argument('-m', '--media_type', metavar='', choices=['movie', 'show'], nargs='?',
|
||||||
help='Refine search for name by using type.')
|
help='Refine search for name by using media type.\n'
|
||||||
|
'(choices: %(choices)s)')
|
||||||
|
parser.add_argument('-e', '--episodes', metavar='', nargs='?', type=bool, default=False, choices=[True, False],
|
||||||
|
help='Refine search for individual episodes.\n'
|
||||||
|
'(choices: %(choices)s)\n(default: %(default)s)')
|
||||||
|
parser.add_argument('-site', '--site', metavar='', choices=['Netflix', 'Amazon', 'Both'], nargs='?',
|
||||||
|
default='Both', help='Refine search for name by using type.\n'
|
||||||
|
'(choices: %(choices)s)\n(default: %(default)s)')
|
||||||
|
parser.add_argument('-sl', '--search_limit', metavar='', nargs='?', type=int, default=5,
|
||||||
|
help='Define number of search returns from page. Zero returns all.'
|
||||||
|
'\n(default: %(default)s)')
|
||||||
|
|
||||||
opts = parser.parse_args()
|
opts = parser.parse_args()
|
||||||
# print(opts)
|
# print(opts)
|
||||||
|
|
||||||
if opts.search:
|
if opts.search:
|
||||||
instantwatch_search(opts.search, opts.type)
|
instantwatch_search(opts.search, opts.media_type, opts.site, opts.search_limit)
|
||||||
else:
|
else:
|
||||||
if len(opts.library) > 1:
|
if len(opts.library) > 1:
|
||||||
for section in opts.library:
|
for section in opts.library:
|
||||||
plex_library_search(section)
|
plex_library_search(section, opts.site, opts.episodes, opts.search_limit)
|
||||||
else:
|
else:
|
||||||
plex_library_search(opts.library[0])
|
plex_library_search(opts.library[0], opts.site, opts.episodes, opts.search_limit)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user