Skip to content

Instantly share code, notes, and snippets.

@cgrice
Created October 5, 2009 13:19
Show Gist options
  • Save cgrice/202109 to your computer and use it in GitHub Desktop.
Save cgrice/202109 to your computer and use it in GitHub Desktop.
"""Scan a barcode from a CD/Album, and determine if it was released by a member of the RIAA"""
__author__ = 'Chris Grice <[email protected]>'
__license__ = 'MIT Licence'
import android
import urllib
import urllib2
import base64
import hmac
import hashlib
import time
from xml.dom import minidom
from BeautifulSoup import BeautifulSoup
AWS_ACCESS_KEY_ID = 'KEY'
AWS_SECRET_ACCESS_KEY = 'SECRET_KEY'
def ksort(d, func = None):
keys = d.keys()
keys.sort(func)
return keys
class RIAAScanner:
def __init__(self):
self.artist = ''
self.title = ''
def get_ASIN(self, ean_13):
base_url = "http://ecs.amazonaws.com/onca/xml"
url_params = dict(
Service='AWSECommerceService',
Operation='ItemLookup',
IdType='EAN',
ItemId=ean_13,
SearchIndex='Music',
AWSAccessKeyId=AWS_ACCESS_KEY_ID,
ResponseGroup='Images,ItemAttributes,EditorialReview,SalesRank')
url_params['Timestamp'] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
keys = url_params.keys()
keys.sort()
values = map(url_params.get, keys)
url_string = urllib.urlencode(zip(keys,values))
string_to_sign = "GET\necs.amazonaws.com\n/onca/xml\n%s" % url_string
# Sign the request
signature = hmac.new(
key=AWS_SECRET_ACCESS_KEY,
msg=string_to_sign,
digestmod=hashlib.sha256).digest()
# Base64 encode the signature
signature = base64.encodestring(signature).strip()
# Make the signature URL safe
urlencoded_signature = urllib.quote_plus(signature)
url_string += "&Signature=%s" % urlencoded_signature
url = "%s?%s" % (base_url, url_string)
print url
req = urllib2.Request(url)
response = urllib2.urlopen(req)
the_page = response.read()
dom = minidom.parseString(the_page)
the_asin = dom.getElementsByTagName('ASIN')[0].childNodes[0].data
self.artist = dom.getElementsByTagName('Artist')[0].childNodes[0].data
self.title = dom.getElementsByTagName('Title')[0].childNodes[0].data
return the_asin
def is_riaa_safe(self, asin):
url = 'http://www.riaaradar.com/search.asp'
values = {'keyword' : asin,
'sortorder' : 'salesrank',
'searchtype' : 'ASIN' }
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
soup = BeautifulSoup(the_page)
info = soup.find('img', alt="Warning!")
if info is not None:
return "unsafe"
else:
info = soup.find('img', alt="Safe")
if info is not None:
return "safe"
else:
return "unknown"
if __name__ == '__main__':
droid = android.Android()
barcode = droid.scanBarcode()
droid.makeToast('Fetching product info')
EAN = barcode['result']['SCAN_RESULT']
Scanner = RIAAScanner()
ASIN = Scanner.get_ASIN(EAN)
droid.makeToast('Scanned: '+Scanner.title+' - '+Scanner.artist)
safe = Scanner.is_riaa_safe(ASIN)
if safe == 'safe':
notify = '%s - %s is not associated with RIAA' % (Scanner.artist, Scanner.title)
droid.notify(notify, 'Safe Album', notify)
droid.makeToast(notify)
elif safe == 'unsafe':
notify = '%s - %s is associatd with RIAA' % (Scanner.artist, Scanner.title)
droid.notify(notify, 'Unsafe Album', notify)
droid.makeToast(notify)
else:
droid.makeToast('RIAA association unknown')
droid.exitWithResultOk()
@Natanael90
Copy link

Love it! I'm going to use it!
I can't wait to make my own scripts that others will want to use!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment