Yahoo Pipes HTML Tables to JSON object array - html

I'm trying to use Yahoo Pipes to turn multiple tables inside a DIV into an array of JSON objects.
Below is what I currently have. The HTML is being parsed fine.
http://pipes.yahoo.com/pipes/pipe.edit?_id=cbd57c80e76b0a2973df0e8bbc357820
I want each of the table cells to be properties of of the JSON object. I believe this is possible with looping, however I can't figure out how to make this happen.

I figured this out using pure JS and the YQL API.
var yql_query = 'select * from html where url="http://www.scorespro.com/soccer/england/premier-league/2014-2015/standings/"';
yql_query += " and xpath='//*[#id=\"standings_1a\"]'";
yql_query = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(yql_query) + '&format=html&callback=?';
$.getJSON(yql_query, function (data) {
// do something crazy the power is now yours! $(data.results[0])
});

Related

Dynamically display a list of xml hotspots (for krpano) based on json list

A bit of a newbie question for xml/krpano,
I have a list of json items that I want to be dynamically loaded into XML <hotspots>. I can loop through each item in JavaScript but I have no clue how to do the same loop in XML!
Check out this picture:
Imagine that each rectangle with an image is one item in a JSON list. Each rectangle you see is a <hotspot>. Right now these three hotspots are hardcoded into the XML file, but I want to dynamically load hotspots based on how many JSON list items exist.
Here is one hotspot. If my json list has 16 items, I would expect 16 hotspots
to be loaded.
<!--* video image thumbnail *-->
<hotspot name="start" distorted="true"
url="/panorama/%$panoId%/thumb.png"
ath="0" atv="0"
ox="0" oy="36"
vr_timeout="2000"
zorder="99"
scale="0.8"
onclick="changepano( loadscene(video_scene, null, MERGE|KEEPVIEW|KEEPMOVING, BLEND(1)); );"
alpha="0.0"
onloaded="if(vr_start_done === true, removehotspot(start); start_vr(); , tween(alpha,1); );"
/>
Your question is about dynamically generating hotspots in KRPano from a JSON list.
It is not really clear to me the way you wrote your question if you want to read the JSON from KRPano XML file (let's say FROM KRPano) or if you are expecting to use Javascript to ask KRPano to produce the hotspots.
These are two completly distinct ways of doing it :)
Because I'm lazy and I suppose you want to deal with JSON in JS, I go for this solution...
Loading a JSON file from Javascript
Your KRPano project should look like a core HTML file presenting Javascript to embed the KRPano plugin.
There, you can declare a script content in your HTML in which you will parse your JSON content and you ask KRPano to generate a hotspot. This method should be called when you are sure KRPano is ready, or get it called from KRPano when it is ready, using "onready" attribute.
myHotspotList.json content:
var myHotspotList = [
{
name: "myFirstHotspot",
atv: 15.0,
ath: 56.5686,
url: "myHotspotImage.jpg"
}
];
tour.html content:
<html>
...
<script url="myHotspotList.json'></script>
<script>
function generateHotspots() {
// First, we get the KRPano plugin
var myKRPano = document.getElementById('krpanoSWFObject');
// Now we parse the JSON object
for(var idx in myHotspotList) {
// Get the current Hotspot data
var currHotspot = myHotspotList[idx];
// Ask KRPano to create a hotspot with our current name
myKRPano.call("addhotspot('"+ currHotspot.name +"');");
// Now set various attributes to this hotspot
myKRPano.call("set(hotpost['"+ currHotspot.name +"'].atv, "+currHotspot.atv+");");
myKRPano.call("set(hotpost['"+ currHotspot.name +"'].ath, "+currHotspot.ath+");");
myKRPano.call("set(hotpost['"+ currHotspot.name +"'].url, '"+currHotspot.url+"');");
}
}
</script>
...
// When you ask for pano creation, give your generation method as callback
embedpano({target:"krpanoDIV", onready:generateHotspots});
...
</html>
I hope this help and you got the trick with calling JSON object attributes and all.
Regards

Wikimedia function to get all my Templates

I need to get all the pages I have created like Templates in my wikimedia webpage. I have to do this with javascript.
Is this possible?
You can do this with a UserContribs API query, like this:
https://en.wikipedia.org/w/api.php?format=jsonfm&action=query&list=usercontribs&ucuser=Ilmari_Karonen&ucnamespace=10&ucshow=new&continue=
Basically, the parameters you need are:
format=json to get results in JSON format, which is probably what you want for JavaScript. (I've used jsonfm in the example link above to get pretty-printed human readable output.)
action=query to indicate that this is, indeed, a query rather than, say, an edit or a login attempt.
list=usercontribs to indicate that you want a list of a user's contributions (i.e. the stuff you see on the Special:Contributions page).
ucuser=your_username to select which user's contributions you want to see. (The example link above shows mine.)
ucnamespace=10 to select only contributions to templates. (10 is the namespace number for the built-in Template namespace).
ucshow=new to select only contributions that involve creating a new page. (Note that this also includes page moves; I don't see any simple way to filter those out.)
Of course, there are other parameters you may also want to include.
I've also included an empty continue= parameter to indicate that I want to use the new query continuation syntax, and to suppress the warning about it. Obviously, if you actually want to use query continuation, you'll need to implement the client-side part yourself (or use an MW API client that implements it for you). Here's one simplistic way to do that:
function getNewTemplatesForUser( username ) {
var queryURL = 'https://en.wikipedia.org/w/api.php?format=json&action=query&list=usercontribs&ucnamespace=10&ucshow=new';
queryURL += '&ucuser=' + encodeURIComponent( username );
var callback = function( json ) {
// TODO: actually process the results here
if ( json.continue ) {
var continueURL = queryURL;
for ( var attr in json.continue ) {
continueURL += '&' + attr + '=' + encodeURIComponent( json.continue[attr] );
}
doAjaxRequest( continueURL, callback );
}
};
doAjaxRequest( queryURL + '&continue=', callback );
}

Phonegap GetJson Strange Output - REST

I am having this situation: ( public api a test website of mine )
$.getJSON("http://ee-tutz.com/entry_api/rest/read_entry/json?auth[username]=test&auth[password]=guest&data[entry_id]=80",function(data){
alert(data); // here i get [obj Obj]
var tweetlistHTML = "";
for(var i = 0; i< data.results.length;i++){
tweetlistHTML +='<li>'+data.results[i].title+ '</li>'
}
var tweetList = $("#tweetlist");
tweetList.html(tweetlistHTML);
});
Is anything worng with the code ?
I want to output some data of out the resulting from the URL ( for example Title )
But it Doesen't Output the titles
Can you try to output the object details using JSON.stringify() method.
This will let you know the returned object properties and values.
e.g: alert the first object details.
alert(JSON.stringify(data.results[0]));
OR the entire objects in the array.
alert(JSON.stringify(data.results));
Also did you try to check the results form the desktop browser whether the URL is returning JSON response.
EDIT1:
I just checked the response of the URL
You need to change the data variable syntax.
data.data[i].title
instead of
data.results[i].title
EDIT2:
Oops. I forgot to change my alert code.
The alert should be.
alert(JSON.stringify(data.data[0]));
or you could alert the whole data.
alert(JSON.stringify(data));
EDIT3:
Your JSON data returned by the URL
http://ee-tutz.com/entry_api/rest/read_entry/json?auth[username]=test&auth[password]=guest&data[entry_id]=80
JSON data:
{"message":"Successfully readed","code":200,"code_http":200,"data":[{"entry_id":"80","site_id":"1","channel_id":"1","author_id":"1","forum_topic_id":null,"ip_address":"86.120.164.135","title":"POP ON OP: an Interactive Real-Time Animation Object-Book","url_title":"pop-on-op-an-interactive-real-time-animation-object-book","status":"open","versioning_enabled":"y","view_count_one":"0","view_count_two":"0","view_count_three":"0","view_count_four":"0","allow_comments":"y","sticky":"n","entry_date":"1405812194","year":"2014","month":"07","day":"20","expiration_date":"0","comment_expiration_date":"0","edit_date":"20140719233024","recent_comment_date":"0","comment_total":"0","main_content":"POP ON OP is an interactive real-time animation object-book in which you can experience movement directly on paper with a special film.","propietati":"74%$23,959 Funded ","testfisier":false,"poza":"<img alt=\"Photo little\" class=\"fit\" height=\"150\" src=\"https:\/\/s3.amazonaws.com\/ksr\/projects\/1055334\/photo-little.jpg?1404412397\" width=\"200\" \/>","descriere":" POP ON OP is an interactive real-time animation object-book in which you can experience movement directly on paper with a special film. ","autor":" by PARRATORO ","categories":[]}],"id":"80"}

GoogleAppsScript: How do I trim strings after parsing HTML?

What I'm trying to do is parse & extract the movies title, without all the HTML gunk, from the webpage which will eventually get saved into a spreadsheet. My code:
function myFunction() {
var url = UrlFetchApp.fetch("http://boxofficemojo.com/movies/?id=clashofthetitans2.htm")
var doc = url.getContentText()
var patt1 = doc.match(/<font face\=\"Verdana\"\ssize\=\"6\"><b>.*?<\/b>/i);
//var cleaned = patt1.replace(/^<font face\=\"Verdana\" size\=\"6\"><b>/,"");
//Logger.log(cleaned); Didn't work, get "cannot find function in object" error.
//so tried making a function below:
String.trim = function() {
return this.replace(/^\W<font face\=\"Verdana\"\ssize\=\"6\"><b>/,""); }
Logger.log(patt1.trim());
}
I'm very new to all of this (programming and GoogleScripting in general) I've been referencing w3school.com's JavaScript section but many things on there just don't work with Google Scripts. I'm just not sure what's missing here, is my RegEx wrong? Is there a better/faster way to extract this data instead of RegEx? Any help would be great, Thanks for reading!
While trying to parse information out of HTML that's not under your control is always a bit of a challenge, there is a way you could make this easier on yourself.
I noticed that the title element of each movie page also contains the movie title, like this:
<title>Wrath of the Titans (2012) - Box Office Mojo</title>
You might have more success parsing the title out of this, as it is probably more stable.
var url = UrlFetchApp.fetch("http://boxofficemojo.com/movies/?id=clashofthetitans2.htm");
var doc = url.getContentText();
var match = content.match(/<title>(.+) \([0-9]{4}\) -/);
Logger.log("Movie title is " + match[1]);

jQuery - Parse Json then Display Data

I'm having a problem parsing valid Json from a Twitter List then displaying the list on the page.
Here is my code;
var url = "http://api.twitter.com/1/aplusk/lists/5676047/statuses.json&callback=?";
$.getJSON(url, function(data) {
var results = '';
$(data.results).each(function() {
results += "<p class='tweet_result' id='tweet" + this.id_str + "'><a href='http://twitter.com/" + this.user.screen_name + "' title='' class='tweet_user'></p>";
});
$(results).prependTo("#twitter_results");
});
If you put the url in to www.jslint.com you can view the structure of the json
I'm new to json so I could be doing something stupid here.
Thanks in advance for your help and advice.
The URL has to be:
http://api.twitter.com/1/aplusk/lists/5676047/statuses.json?callback=?
(Note the question mark instead of the ampersand)
Also see the returned object, it does'nt have a member "results", it's a native javascript-array.
You'll have to iterate over data itself:
$(data).each(function(i,item)
where you can access the properties inside via
item.someProperty