Single Image from Flikr using API in AJAX
Here is a code example of how to get one single image out of Flikr, and place it into a html page using AJAX.
Just call getImageFromFlikr with the search text, and an output element. It works asynchronously, so it will return as soon as Flikr is contacted.
function getImageFromFlikr(searchText, output)
{
try
{
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState!=4) return;
if (xmlhttp.status != 200)
{
output.innerHTML = “Error”;
}
if (xmlhttp.status == 200)
{
output.innerHTML = eval(xmlhttp.responseText); //retrieve result as an JavaScript object
}
}
var strUrl = “http://api.flickr.com/services/rest/?”;
strUrl += “method=flickr.photos.search”;
strUrl += “&api_key=cabbc1b68c18461ca0627991833d8f97”;
strUrl += “&text=” + encodeURIComponent(searchText);
strUrl += “&per_page=1”;
strUrl += “&format=json”;
strUrl += “&sort=relevance”;
xmlhttp.open(“GET”,strUrl,true);
xmlhttp.open(“GET”,strUrl,true);
xmlhttp.send();
}
catch (err)
{
output.innerHTML = err.message;
}
}function jsonFlickrApi(flikrObj)
{
var fmt=”http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}_m.jpg”;
fmt = fmt.replace(“{farm-id}”,flikrObj.photos.photo[0].farm);
fmt = fmt.replace(“{server-id}”,flikrObj.photos.photo[0].server);
fmt = fmt.replace(“{id}”,flikrObj.photos.photo[0].id);
fmt = fmt.replace(“{secret}”,flikrObj.photos.photo[0].secret);
var html = “<img src='” + fmt + “‘>”;
return html;
}
Find Backlinks to a website using AJAX
One of the most basic SEO tools is a backlink checker, and if you need to fine tune this, then automating the check will save you lots of time, so this is my experience with the SEOMOZ links API using AJAX.
The basis of the API, is a call to the URL: http://lsapi.seomoz.com/linkscape/links/<URL>?Scope=page_to_page&Sort=page_authority where <URL> is the site in question. This throws up the basic HTTP authentication challenge, so I’ve included the username and password:
Your Access ID: member-2dd03f462e
Your Secret Key: 018a47ae0940f6342577b24dfce80a5f
This code requires the Base64.js code library from www.webtoolkit.info, to base64 encode the HTTP authentication. Note that this code will only work when run locally from Internet Explorer, it won’t work hosted online due to cross-domain restrictions.
<input type="text" name="input" id="input" value="www.xyz.com">
<input type="button" onclick="BacklinkCheck()" value="Check">
<br>
<div id="output"></div>
<script language="javascript">
function BacklinkCheck()
{
var auth = authenticate('member-2dd03f462e','018a47ae0940f6342577b24dfce80a5f');
var input = document.getElementById("input");
var output = document.getElementById("output");
output.innerHTML = "working...";
var http=new XMLHttpRequest();
var url = "http://lsapi.seomoz.com/linkscape/links/"
url += input.value + "?Scope=page_to_page&Sort=page_authority";
http.open("GET", url, true);
http.setRequestHeader('Authorization', auth);
http.setRequestHeader('Content-type', 'text/html; charset=unicode');
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
var html = "<table>"
var jsondata=eval("("+http.responseText+")");
jsondata = jsondata.sort(
function(a, b){
if (a.upl < b.upl) //sort string ascending
return -1
if (a.upl > b.upl)
return 1
return 0 //default return value (no sorting)
});
for (i in jsondata )
{
var backlink = jsondata[i];
if (i % 2 == 0)
{
html += "<tr>"
}
else
{
html += "<tr>"
}
html += "<td width=50%><a onclick=\"this.innerHTML='" + backlink.uu.replace('\'','') + "'\">"+ backlink.upl + "</a></td>";
html += "<td width=50%>" + backlink.ut + "</td>";
html += "</tr>";
}
html += "</table>";
output.innerHTML = html;
}
if(http.readyState == 4 && http.status != 200)
{
alert("Something went wrong");
}
}
http.send(null);
}
function authenticate(user, password) {
var tok = user + ':' + password;
var hash = Base64.encode(tok);
return "Basic " + hash;
}
</script>
Convert text to a HTML table
Here is a handy litte script that converts tabulated text into a HTML table. Apply a CSS stylesheet to it, and it’ll look nice too …
<textarea id="input" cols=100 rows=10>
</textarea><br>
<input type="button" id="Tabelize" onclick="tabilize()" value="tabilize">
<div id="output"></div>
<script language="javascript">
function tabilize()
{
var input = document.getElementById("input");
var output = document.getElementById("output");
var splitInput = input.value.split("\n");
var html = "<table>";
for(i in splitInput)
{
if (i % 2 == 0)
{
html += "<tr>";
}
else
{
html += "<tr>";
}
var splitRow = splitInput[i].split("\t");
for(j in splitRow)
{
html += "<td>" + splitRow[j] + "</td>";
}
html += "</tr>";
}
html += "</table>";
html = html.replace(/</g,"<");
html = html.replace(/>/g,">");
output.innerHTML = html;
}
</script>
Open Source Pastebin in .NET
A pastebin is designed to be an easy way to quickly post some text online, so that others can see it. It also has applications in cross-firewall data traversal, where one peer can upload data to a pastebin, and the other peer can poll the pastebin for the message to arrive. This was the kind of pastebin I was after.
I looked at pastebin.com and paste.kde.org, to see if they were suitable. Paste.kde.org seemed promising, where you could paste easily, using a URL like this:
Which would return an ID, which you could retrieve using http://paste.kde.org/api/xml/117613, for example. The problem came, when I wanted to list all pastes by a particular user, or project, and I spotted that it wasn’t working as expected, like http://paste.kde.org/~fiachspastebin/api/xml/all/ seemed to return numerous pastes by other users.
So, I decided to write my own pastebin, which would store the messages in-memory. This meant that they would be deleted as the IIS worker process recycled, but, I didn’t need longevity.
Using two simple actions “action=update” which either created or updated a paste, and assigned it to a “user“, containing the text contained in the “text” querystring parameter. Then when “action=get” then the text associated with that “user” would be returned.
So, to store text with a user, you simply call:
http://url/pastebin.aspx?action=update&user=dananos&text=Hello+World
Then to retrieve that text you call
http://url/pastebin.aspx?action=get&user=dananos
And that’s it. The code I used is below:
public static Hashtable data = new Hashtable();
protected void Page_Load(object sender, EventArgs e)
{
string action = Request.QueryString["Action"];
string user = Request.QueryString["User"];
string text = Request.QueryString["Text"];
if (string.IsNullOrEmpty(action) || string.IsNullOrEmpty(user))
{
Response.Write("Action and User is required");
return;
}
if (action == "update")
{
PasteBin.data[user] = text;
Response.Write("OK");
}
if (action == "get")
{
if (PasteBin.data.ContainsKey(user))
{
Response.Write(PasteBin.data[user]);
}
else
{
Response.Write("NO DATA");
}
}
}
I’ve ported the same code to PHP, for Linux users. This code requires the $_APP library, which is posted at http://www.leosingleton.com/projects/code/phpapp/
This is hosted at http://pastebin.omadataobjects.com/bin.php
<?php
include("app.php");
application_start();
$action = $_REQUEST["action"];
$user = $_REQUEST["user"];
$text = $_REQUEST["text"];
if ($action == "" || $user == "")
{
echo("action and user is required");
}
else
{
if ($action=="update")
{
$_APP[$user] = $text;
echo "OK";
}
if ($action=="get")
{
echo stripslashes($_APP[$user]);
}
}
application_end();
?>
Bluetooth on Android
I came accross the APK file for BlueTooth for android, Bluetooth_File_Transfer_4.20.apk, which if you rename to a ZIP you can decompress. You then can convert the resulting DEX file into a JAR using DEX2JAR. Then you can convert the JAR into Java classes using JD-GUI.
The resulting source code can be seen here: http://zip.webtropy.com/default.aspx/qeOS3a
Worth a look.
403.14 mvc
If you get the error 403.14 when opening a MVC based web application, then here are a few checks:
- Ensure that you have a web.config in both the root and Views folder
- Ensure that MVC 2 is installed on your webserver
- Run C:\Windows\Microsoft.NET\Framework64\v4.0.30128\aspnet_regiis -i
Free App Hub account, without Dreamspark
In order to get a Microsoft App Hub account, you can either pay 100 USD or, if you are a student, use a Dreamspark account to get in for free. If you neither want to pay the 100 dollars, and are no longer a student, then we can offer you a free account.
If you would like to publish an app on the Windows Phone 7 App hub without an account, then simply send us the XAP file, along with the required graphics and a description, and we will publish it on your behalf. Email us at support@openmerchantaccount.com with your app.
Graphics required are:
Application icon included in package (62×62 PNG)
Application tile image included in package (173×173 PNG)
Small tile (99×99 PNG)
Medium tile (173×173 PNG – normally this would be the application tile image)
Large tile (200×200 PNG)
Background artwork (1000×800 PNG – this is optional)
Description Text
Keywords Text
One or more application screenshots (480×800 PNG)
We do not offer outpayments, but your app will be published as a paid app. If you want your app to be published as a free app, then contact us for a price. Updates to apps will be charged at a reasonable rate.
During the approval process, if your app fails, then you will be given one attempt to fix it. If you fail a second time, then a small charge will be requested before we submit subsequent times. A PDF will be sent to you describing the fault.
GIF Proxy – Convert GIF to JPG (for Silverlight)
One of the most irritating features of Silverlight, is that it can’t handle GIF images. There is the Imagetools library from CodePlex, but it’s complex to implement.
Here’s a simple way of doing it, via a GIF proxy. This is a simple web page that takes a url of a GIF image in the querystring and outputs a JPEG:
Example: Here’s a GIF: http://www-wales.ch.cam.ac.uk/~wales/CCD/Thomson2/gif/560.gif
And Here’s it as a Jpeg: http://{removed}/GifProxy.aspx?url=http://www-wales.ch.cam.ac.uk/~wales/CCD/Thomson2/gif/560.gif
Great for Silverlight, WP7 (Windows Phone 7) etc.
CrossDomain.xml proxy
Ever tried to request a url in Silverlight, or WP7 to be stopped because you need to have a crossdomain.xml file installed in the root of the server.
Here’s a solution, a crossdomain.xml proxy.
Call http://<url>/CrossDomainProxy.aspx?url=http://YOURDOMAIN/YOURSCRIPT
and http://<url>/CrossDomain.xml is set to accept all hosts and requests.
Therefore even if there is not a CrossDomain.xml policy file on your server, you can use this proxy to bypass that.
Please, if you use this, give this blog a link back!, and a thank you would be nice!
Find All Websites on a Webserver in C#
This is a handy script that can tell you how many sites are hosted on an IP address. It uses the Bing API, so it basically asks bing if it knows of sites on that IP address. This means that they have to be listed in bing, i.e. with public access.
public static int SitesOnIP(string ip)
{
string url = "http://api.search.live.net/json.aspx?";
url += "Appid=92B665B5421E197DC762503859279DFEBBE0B998";
url += "&query=IP:" + ip;
url += "&sources=web&web.count=50";
// "Web":{"Total":31900
string strRegex = @"Web....Total..(?<Count>\d+)";
WebClient web = new WebClient();
string strJson = web.DownloadString(url);
string strCount = Regex.Match(strJson, strRegex).Groups["Count"].Value;
return Convert.ToInt32(strCount);
}