Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
139 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

916 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