Created
December 15, 2016 00:33
-
-
Save alanbernstein/aa67f1a488461d3a1d7b107d49f7122e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import os | |
import requests | |
from time import sleep | |
from amazon.api import AmazonAPI | |
from canistreamit import search, streaming | |
# from bs4 import BeautifulSoup | |
# python-amazon-simple-product-api==2.1.0 | |
from panda.debug import debug, pp | |
# clue to amazon docs http://docs.aws.amazon.com/AWSECommerceService/latest/DG/EX_SearchingbyKeyword.html | |
# this doesnt work | |
def cisi_example(): | |
search_list = search('jurassic park') | |
movie = search_list[0] | |
stream = streaming(movie['_id']) | |
print(movie['title']) | |
print(stream) | |
if len(stream) > 0: | |
print(stream.keys()) | |
def amazon_api_example_search(): | |
key = os.getenv('AMAZON_KEY', None) | |
secret = os.getenv('AMAZON_SECRET', None) | |
tag = os.getenv('AMAZON_ASSOCIATE_TAG', None) | |
amazon = AmazonAPI(key, secret, tag) | |
products = amazon.search_n(1, Keywords='gremlins', SearchIndex='All') | |
# 400 error, until i added admin access to my AWS user. now works | |
print('example search') | |
print(products[0].title) | |
print(products[0].asin) | |
print(products[0].binding) | |
print(products[0].creators) | |
print(products[0].large_image_url) | |
print(products[0].offer_url) | |
print(products[0].release_date) | |
print(products[0].price_and_currency) | |
# no easy way to get prime availability from the API - | |
# just get the full html from the offer_url, and | |
# search for "watch now" | |
# problem - have to be logged in... | |
def amazon_api_example_lookup(): | |
# https://pypi.python.org/pypi/python-amazon-simple-product-api | |
key = os.getenv('AMAZON_KEY', None) | |
secret = os.getenv('AMAZON_SECRET', None) | |
tag = os.getenv('AMAZON_TAG', None) | |
amazon = AmazonAPI(key, secret, tag) | |
product = amazon.lookup(ItemId='B0051QVF7A') | |
product.title | |
# 'Kindle, Wi-Fi, 6" E Ink Display - for international shipment' | |
product.price_and_currency | |
# (89.0, 'USD') | |
product.ean | |
# '0814916014354' | |
product.large_image_url | |
# 'http://ecx.images-amazon.com/images/I/411H%2B731ZzL.jpg' | |
product.get_attribute('Publisher') | |
# 'Amazon Digital Services, Inc' | |
product.get_attributes(['ItemDimensions.Width', 'ItemDimensions.Height']) | |
# {'ItemDimensions.Width': '450', 'ItemDimensions.Height': '34'} | |
class AmazonClient(object): | |
base_url = 'http://www.amazon.com/dp/' | |
prime_pattern = 'Available on Prime' | |
def __init__(self): | |
self.key = os.getenv('AMAZON_KEY', None) | |
self.secret = os.getenv('AMAZON_SECRET', None) | |
self.tag = os.getenv('AMAZON_ASSOCIATE_TAG', None) | |
self.client = AmazonAPI(self.key, self.secret, self.tag) | |
def search(self, keywords): | |
products = self.client.search_n(1, Keywords=keywords, SearchIndex='All') | |
if products: | |
return products[0] | |
def search_video(self, keywords): | |
products = self.client.search_n(1, Keywords=keywords, SearchIndex='Video') | |
if products: | |
return products[0] | |
def search_prime_video(self, keywords): | |
products = self.client.search_n(1, Keywords=keywords, SearchIndex='Video') | |
if not products: | |
return {} | |
url = self.base_url + products[0].asin | |
resp = requests.get(url) | |
result = { | |
'products': products, | |
'url': url, | |
'prime': bool(re.search(self.prime_pattern, resp.content)), | |
} | |
return result | |
def get_tv_show_names(): | |
fname = 'tv_show_names_201606.txt' | |
with open(fname, 'r') as f: | |
lines = f.read().splitlines() | |
return lines | |
def main(): | |
# cisi_example() | |
# amazon_api_example_search() | |
ac = AmazonClient() | |
names = get_tv_show_names() | |
for name in names[0:5]: | |
res = ac.search_prime_video(name) | |
print(name) | |
print(res) | |
sleep(1) | |
debug() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment