Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
127 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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

715 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