Following from the concepts I wrote about yesterday, here’s two examples of API parsers using a WORK model.
RSS 2.0
Class definition – that’s the whole thing there!:
class RSS20(API):
_item_path = "channel.item"
_meta_path = "channel"
def __init__(self, uri):
API.__init__(self)
self._uri_base = uri
Using it:
api = RSS20(uri = 'http://feeds.feedburner.com/DavidJanesCode')
for item in api.IterItems():
print "-", item['title']
Results:
- WORK - Web Object Records - Syntax Error on Line 1 - Adding MapField to inputEx - Switching between mapping APIs and universal zoom levels - How to dynamically load map APIs - How to use the Google Maps API - How to use the Microsoft Virtual Earth API - Tip - how to get your browser’s User Agent - How to use the MapQuest API - How to use the Yahoo Maps Service AJAX API - How to detect internal link jumps - GenX - first public demonstration - Amazon’s OpenSearch: mostly useless - More style updates - How to do multi-column multilingual full text searching in Oracle - Tip - fixing broken menus over form on IE6 and IE7 - New style for this weblog - AUMFP - Demo - Tip - use mod_rewrite to redirect to subdirectory - AUMFP - The Almost Universal Microformats Parser
Amazon ECS
This will probably end up replacing PyECS!
Class definition:
class AmazonECS(API):
_base_query = {
"Sort" : "relevancerank",
"Operation" : "ItemSearch",
"Version" : "2008-08-19",
"ResponseGroup" : [ "Small", ],
}
_uri_base = "http://ecs.amazonaws.com/onca/xml"
_meta_path = "Items.Request"
_item_path = "Items.Item"
_page_max_path = 'Items.TotalPages'
_item_max_path = 'Items.TotalResults'
_page_max = -1
def __init__(self, **ad):
API.__init__(self, **ad)
def CustomizePageURI(self, page_index):
if page_index == 1:
return
return "%s=%s" % ( "ItemPage", page_index )
Using it:
api = AmazonECS(AWSAccessKeyId = os.environ["AWS_ECS_ACCESSKEYID"])
api.SearchOn(
Keywords = "Larry Niven",
SearchIndex = "Books",
Condition = "New",
)
for item in api.IterItems():
print "-", item['ItemAttributes.Title']
Results … note that this fetching many pages of results:
- Fleet of Worlds - Juggler of Worlds - Escape from Hell - Inferno - N-Space - The Ringworld Engineers (Ringworld) - The Draco Tavern - Legacy of Heorot: Legacy of Heorot - Footfall - A WORLD OUT OF TIME (ORBIT BOOKS) - The Burning City (Hardback) - Protector - Burning Tower - Three Books of Known Space - Ringworld Throne - Tales of Known Space: The Universe of Larry Niven - Scatterbrain - Ringworld - Lucifer's Hammer ... (continues) ...