Python Top Artists From Last.fm CSV

This python example script will get the top 1,000 artists from last.fm and save it as a CSV file. Just add your API key and desired file path.

########################################
import os, sys, re, datetime, traceback, urllib2, urllib
import xml.etree.ElementTree as etree

API_KEY = ""
OUTPUT_FILEPATH = ".csv"

last_fm_top_artists_url = "http://ws.audioscrobbler.com/2.0/?method=chart.gettopartists&api_key=%s&limit=1000" \
% API_KEY
last_fm_top_artists_feed_request = urllib2.Request(last_fm_top_artists_url)
last_fm_top_artists_opener = urllib2.build_opener()
last_fm_top_artists_xml_file = last_fm_top_artists_opener.open(last_fm_top_artists_feed_request)
last_fm_top_artists_tree = etree.parse(last_fm_top_artists_xml_file)
last_fm_top_artists_root = last_fm_top_artists_tree.getroot()

artists_node = last_fm_top_artists_root.find("artists")
artists = artists_node.getiterator("artist")
f = open(OUTPUT_FILEPATH, "w")
for artist in artists:
    f.write(artist.find('name').text.encode('utf-8'))
    f.write('\n')
f.close()
print "COMPLETED"

Python Akamai Content Control Utility Example

This python example script will invalidate all URLs for the given CP Code via the Akamai Content Control Utility API.

########################################
import httplib

d = dict(
USERNAME = USERNAME,
PASSWORD = PASSWORD,
EMAIL = EMAIL,
CPCODE = CPCODE
)

soap_message = """<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<purgeRequest xmlns="http://ccuapi.akamai.com/purge">
<name xsi:type="xsd:string">%(USERNAME)s</name>
<pwd xsi:type="xsd:string">%(PASSWORD)s</pwd>
<network xsi:type="xsd:string">ff</network>
<opt soapenc:arrayType="xsd:string[3]" xsi:type="soapenc:Array">
<item xsi:type="xsd:string">email-notification=%(EMAIL)s</item>
<item xsi:type="xsd:string">type=cpcode</item>
<item xsi:type="xsd:string" />
</opt>
<uri soapenc:arrayType="xsd:string[1]" xsi:type="soapenc:Array">
<item xsi:type="xsd:string">%(CPCODE)s</item>
</uri>
</purgeRequest>
</soap:Body>
</soap:Envelope>
""" % d

print soap_message

# construct and send the header
webservice = httplib.HTTP("ccuapi.akamai.com")
webservice.putrequest("POST", "/soap/servlet/soap/purge")
webservice.putheader("Host", "ccuapi.akamai.com")
webservice.putheader("User-Agent", "Python post")
webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(soap_message))
webservice.putheader("SOAPAction", "http://ccuapi.akamai.com/purge#purgeRequest")
webservice.endheaders()
webservice.send(soap_message)

# get the response
statuscode, statusmessage, headers = webservice.getreply()
print "Response: ", statuscode, statusmessage
print "Headers: \n", headers
result = webservice.getfile().read()
print result

Baskets