Solved

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

Posted on 2016-08-06
4
88 Views
Last Modified: 2016-08-06
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...
0
Comment
Question by:alex_smith
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 41745472
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.
0
 
LVL 16

Accepted Solution

by:
Walter Ritzel earned 500 total points
ID: 41745637
Here is a version fully working. I believe there should be a better way to deal with these query parameters that are arrays or something, but I did not find anything.

import requests

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

r = requests.get('http://www.podbean.com/api2/public/filesPlays', data)

print(r.url)
print(r.status_code)
print(r.text)

Open in new window

0
 

Author Comment

by:alex_smith
ID: 41745788
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!
0
 

Author Closing Comment

by:alex_smith
ID: 41745790
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 :-)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript_Set_dropdown_value 7 40
JSON  parse help 7 40
jquery tab header text 1 23
Reactjs with .NET 3 37
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question