Link to home
Start Free TrialLog in
Avatar of alex_smith
alex_smithFlag for Australia

asked on

Using Python, I want to pass a URL some json and get back data (imitating Podbean's jQuery (AJAX?) function)

Podbean pages display the number of downloads each episode has had (e.g. http://cpx.podbean.com/e/life-faith-field-hospital/). This appears to be generated dynamically with the following:
jQuery(function($) {

    $.ajax({
        url: "http://www.podbean.com/api2/public/filesPlays",
        type:"GET",
        data:{"blogId":"63614","query":[{"file":"207_Field_Hospital.mp3","w":"ccb77541"}]}, 
        dataType:"json",      
        success: function(result) {
            if(result.code =="200")
            {
                $.each(result.data, function(id,count){
                    id = id.replace(/\./g,"_");
                    if(count >0)
                    {
                        $(".hits_"+id).text(count);	
                    }
                    else
                    {
                        $(".hits_"+id).parent(".hits").prev("span").remove();
                        $(".hits_"+id).parent(".hits").remove();	
                    }	
                });							
            }
        }
    });
});

Open in new window

I've been trying to use "requests" in Python (http://docs.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls) to get the number of "hits" but I don't know how to pass it json. My failed attempt was:
import requests

data = {"blogId":"63614","query":[{"file":"207_Field_Hospital.mp3","w":"ccb77541"}]}

r = requests.get("http://www.podbean.com/api2/public/filesPlays", json=data)
print(r.url)
print(r.status_code)
print(r.text)

Open in new window

If "requests" can't do it, I'm open to doing it another way with Python...
Avatar of Walter Ritzel
Walter Ritzel
Flag of Brazil image

Try this:
import requests
import json

data = {"blogId": "63614", "query": [{"file": "207_Field_Hospital.mp3", "w": "ccb77541"}]}

r = requests.get("http://www.podbean.com/api2/public/filesPlays", data=json.dumps(data))
print(r.url)
print(r.status_code)
print(r.text)

Open in new window

It is complaining of the blog id, but I think it is matter of correct structure of json.
I did not find documentation on this API to fix it for you.
ASKER CERTIFIED SOLUTION
Avatar of Walter Ritzel
Walter Ritzel
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alex_smith

ASKER

Thanks Walter!

Yes, the API doesn't seem to be documented :-/ I had tried your first suggestion and got the same error.

Thanks for the workaround. I agree there should be a better way but at least it works, which is much more than I had!
I'm relieved for your solution as it means I can continue writing my program but if you, or anyone else, figures out a better way, feel free to let me know :-)