[python] [Gracenote Web API] A little customization of pygn

I will post it to Qiita, thinking that there are people who handle such APIs. It's a technical blog, so if it's related, you can write anything. There is nothing else to mention here for today's dinner.

What is Gracenote in the first place?

ITunes is something you probably don't know about iPods and iPhones, but it's the Gracenote API that's used to insert CDs and tag songs. All kinds of songs from ancient times to east and west are registered, and especially in Japan, doujin music and drama CDs of eroge reservation benefits are recognized. Who is registered? Really ... (This country is already ...)

It's such an excellent API, but iTunes has a GNSDK that can do anything (C language). The Web API is a simplified version, but it returns quite detailed content. If you throw XML, it will return it in XML. (I personally prefer JSON, but) I have a wrapper, so it's pretty good.

Easy to use

I'm Pythonista so I only use pygn. (I like Python the most, but I'm also interested in C # and Node.js, so I may post it if I have time ...) Please register with Gracenote in advance to obtain the cliant ID and user ID.

I will write in a super sink. It's not the main subject of today.

test.py


import pygn, json

clientID = 'XXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
userID = 'XXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

def beautiful(gn_obj): 
   data = json.dumps(gn_obj, sort_keys=True, indent=4, ensure_ascii=False)
   return data

res = beautiful(pygn.search(clientID=clientID, userID=userID, album='Oh! Rival', artist='Porno graffiti'))
print(res)

Save it as ./test.py> res.json.

Main subject

I just made pygn correspond to \ <LANG > ja \ </ LANG >. This is a patch file.

pygn.patch


--- /home/hoge/Download/pygn-master/pygn.py	2015-05-15 17:06:08.436636117 +0900
+++ /usr/local/lib/python3.4/dist-packages/pygn.py	2015-05-15 04:04:38.551710764 +0900
@@ -7,6 +7,8 @@
 
 You will need a Gracenote Client ID to use this module. Please contact 
 [email protected] to get one.
+
+<LANG>Optional version
 """
 
 from __future__ import print_function
@@ -100,7 +102,8 @@
 # Added by Fabian in order to cover the Rhythm API
 # Returns a list of gnmetadata dictionaries 
 
-def createRadio(clientID='', userID='', artist='', track='', mood='', era='', genre='', popularity ='', similarity = '', count='10'):
+def createRadio(clientID='', userID='', lang='', artist='', track='', mood='', era='', genre='',
+                popularity ='', similarity = '', count='10'):
     """
     Queries a Radio playlist
     """
@@ -117,6 +120,7 @@
     
     # Build the user header 
     query.addAuth(clientID, userID)
+    query.addLang(lang)
     
     query.addQuery('RADIO_CREATE')
     
@@ -228,7 +232,7 @@
 
 #***********************************************************************************************************************
 
-def search(clientID='', userID='', artist='', album='', track='', toc=''):
+def search(clientID='', userID='',lang='', artist='', album='', track='', toc=''):
     """
     Queries the Gracenote service for a track, album, artist, or TOC
     
@@ -247,6 +251,7 @@
     query = _gnquery()
     
     query.addAuth(clientID, userID)
+    query.addLang(lang)
     
     if (toc != ''):
         query.addQuery('ALBUM_TOC')
@@ -432,7 +437,7 @@
 
 
 
-def get_discography(clientID='', userID='', artist='', rangeStart=1, rangeEnd=10):
+def get_discography(clientID='', userID='', lang='', artist='', rangeStart=1, rangeEnd=10):
     """
     Queries the Gracenote service for all albums containing an artist
     """
@@ -449,6 +454,7 @@
     query = _gnquery()
 
     query.addAuth(clientID, userID)
+    query.addLang(lang)
     query.addQuery('ALBUM_SEARCH')
     query.addQueryTextField('ARTIST', artist)
     query.addQueryOption('SELECT_EXTENDED', 'COVER,REVIEW,ARTIST_BIOGRAPHY,ARTIST_IMAGE,ARTIST_OET,MOOD,TEMPO')
@@ -534,7 +540,7 @@
 
     return discography
     
-def fetch(clientID='', userID='', GNID=''):
+def fetch(clientID='', userID='', GNID='', lang=''):
     """
     Fetches a track or album by GN ID
     """
@@ -551,6 +557,7 @@
     query = _gnquery()
 
     query.addAuth(clientID, userID)
+    query.addLang(lang)
     query.addQuery('ALBUM_FETCH')
     query.addQueryGNID(GNID)
     query.addQueryOption('SELECT_EXTENDED', 'COVER,REVIEW,ARTIST_BIOGRAPHY,ARTIST_IMAGE,ARTIST_OET,MOOD,TEMPO')
@@ -721,6 +728,10 @@
         client.text = clientID
         user.text = userID
     
+    def addLang(self, language):
+        lang = xml.etree.ElementTree.SubElement(self.root, 'LANG')
+        lang.text = language
+        
     def addQuery(self, cmd):
         query = xml.etree.ElementTree.SubElement(self.root, 'QUERY')
         query.attrib['CMD'] = cmd

This will add a lang option to each parameter.

In the previous example ...

res = beautiful(pygn.search(clientID=clientID, userID=userID, lang='ja' album='Oh! Rival', artist='Porno graffiti'))

It's like that. Even if it comes back in Japanese, the mood and so on will only be in Japanese ... Tab insertion doesn't work, so I think it probably won't work. In that case, please add by hand.

Recommended Posts

[python] [Gracenote Web API] A little customization of pygn
Hit a method of a class instance with the Python Bottle Web API
A little bit from Python using the Jenkins API
EXE Web API by Python
Web API with Python + Falcon
Comparison of 4 Python web frameworks
I just changed the sample source of Python a little.
A record of patching a python package
A good description of Python decorators
Automation of a research on geographical information such as store network using Python and Web API
[python, ruby] fetch the contents of a web page with selenium-webdriver
[Python] A memorandum of beautiful soup4
[Python / Django] Create a web API that responds in JSON format
A brief summary of Python collections
A collection of one-liner web servers
Hit the web API in Python
Get a capture of the entire web page in Selenium Python VBA
Development and deployment of REST API in Python using Falcon Web Framework
[Python] Web application from 0! Hands-on (3) -API implementation-
Python: Reading JSON data from web API
Make a relation diagram of Python module
A little scrutiny of pandas 1.0 and dask
Daemonize a Python web app with Supervisor
[Python] A quick web application with Bottle!
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
A little more detail on python comprehensions
I tried to make a Web API
Python exception handling a little more convenient
Run a Python web application with Docker
Let's make a web framework with Python! (1)
The story of blackjack A processing (python)
[Python] Get a list of folders only
Let's make a web framework with Python! (2)
A memorandum of python string deletion process
Let Python measure the average score of a page using the PageSpeed Insights API
[Python] 3 types of libraries that improve log output a little [logzero, loguru, pyrogrus]
Get a list of articles posted by users with Python 3 Qiita API v2
A memo of a tutorial on running python on heroku
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Draw a graph of a quadratic function in Python
Created a Python wrapper for the Qiita API
How to open a web browser from python
Procedure to use TeamGant's WEB API (using python)
[python] Create a list of various character types
A memorandum of calling Python from Common Lisp
I thought a little about TensorFlow's growing API
Start a simple Python web server with Docker
Make a copy of the list in Python
Rewriting elements in a loop of lists (Python)
A note about the python version of python virtualenv
Create a web map using Python and GDAL
[Python] Web development preparation (building a virtual environment)
Launch a web server with Python and Flask
Explosive speed with Python (Bottle)! Web API development
Make a joyplot-like plot of R in python
[Python] A rough understanding of the logging module
Anonymous upload of images using Imgur API (using Python)
Get a glimpse of machine learning in Python
[Python] A rough understanding of iterators, iterators, and generators
A well-prepared record of data analysis in Python