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...
AJAXPythonjQueryJSON

Avatar of undefined
Last Comment
alex_smith
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of alex_smith
alex_smith
Flag of Australia image

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!
Avatar of alex_smith
alex_smith
Flag of Australia image

ASKER

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 :-)
jQuery
jQuery

jQuery (Core) is a cross-browser JavaScript library that provides abstractions for common client-side tasks such as Document Object Model (DOM) traversal, DOM manipulation, event handling, animation and Ajax. jQuery also provides a platform to create plugins that extend jQuery's capabilities beyond those already provided by the library.

19K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo