Jun 082011
 

Easy Content Units usually use JavaScript to write the contents of the unit on your webpage, the trouble with this method is that search engines have JavaScript switched off when they visit your website. This means they never see the contents on the easy content unit, if you have a page with not much content but a nice unit, search engines will not see a lot when they visit. Using the ASP.NET (VB) code below converts the unit and then writes it out to the page before the page is rendered. Because its using ASP.Net, no matter who visits the page, the ASP code will always run, so the easy content unit will be visible to all search engines.

These are the name-space libraries needed.

<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>

I have put this in the page load section, so it only runs on page load. The way code works basically is by calling the unit and then stripping out all the JavaScript that writes to the page using document.write. This then leaves just the bare HTML which can then be written to the page along with any other HTML on the page.

Sub Page_Load(sender As Object, e As EventArgs)

	'Variable declarations
    Dim aSplit, i
    Dim sOutput As String
    Dim objResponse As WebResponse

	'Read the ECU into a variable using a HTTP request
    Dim objRequest As WebRequest = HttpWebRequest.Create("http://www.easycontentunits.com/js_unit.php?ecu_mid=1199&ecu_uid=50564")

	'Get the response from the above request
    objResponse = objRequest.GetResponse()

	'Convert the response into a string using text streaming
    Using sr As New StreamReader(objResponse.GetResponseStream())
        strResult = sr.ReadToEnd()
        sr.Close()
    End Using

	'We now need to get rid of all the JS code

    'Get rid of document.write(unescape("
    strResult = replace(strResult,"document.write(unescape(""","")

    'Get rid of ")
    strResult = replace(strResult,""")","")

    'Get rid of );
    strResult = replace(strResult,");","")

    ' next convert %hexdigits to the character
    aSplit = Split(strResult, "%")
    If IsArray(aSplit) Then
        sOutput = aSplit(0)
        For i = 0 to UBound(aSplit) - 1
            sOutput = sOutput & Chr("&H" & Left(aSplit(i + 1), 2)) & Right(aSplit(i + 1), Len(aSplit(i + 1)) - 2)
        Next
    End If

    'Move finished hex conversion back to original var name
    strResult = sOutput

End Sub

You put the code below on the page where you want the unit to appear.

<%Response.Write(strResult)%>
Jun 052011
 

I written this bit of code for 2 reasons, first was to read whatever was posted onto a Tumblr blog and integrate it into a clients website. The other reason was if Tumblr was to go down, the blog page would still work due to the caching on the Tumblr data.

I always choose Tumblr when setting up a blog for a client where no one really looks direct at the blog, its more a easy way of letting the client create blog post and it magically appears on their website.

Anyway, enough rambling here is code. Please remember there is probably a million ways to do this and mine might not be the best but it works.

 

Header – This tries to load the XML from tumblr and store it in a file on the webserver. It then checks to see if a cached file/xml is present, if not it makes a cached file/xml out of the file stored on the webserver. The way the cache is created using a dependency, so if the file changes, the cache will change. It then loads the cached file/xml in a var called Posts.

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Linq" %>

Dim Posts As XElement
Sub Page_Load(sender As Object, e As EventArgs)

Try
'Read Tumblr Feed and save it locally
Dim xmlMasterUpdateDoc As New XmlDocument()
xmlMasterUpdateDoc.Load("http://*nameofyourtumblrblog*.tumblr.com/api/read")
xmlMasterUpdateDoc.Save("*somewhereonyourhostingspace*\blog.xml")

Catch xmlEx As XmlException
'Catch any XML error and display it.
Response.Write("XML Exception Thrown -> " + xmlEx.Message)

Catch eX As Exception
'Catch the error and display it.
Response.Write("Exception Thrown -> " + eX.Message)
Finally

End Try

If Cache("tumblrXE") is Nothing then

'There is no tumblrXE item in the Cache,  so load it from the local file
Dim tumblrResponse As XElement = XElement.Load("*somewhereonyourhostingspace*\blog.xml")

'Add it to the Cache and make it dependant on the local file (so it if changes the cache will)
Cache.Insert("tumblrXE", tumblrResponse, New CacheDependency("*somewhereonyourhostingspace*\blog.xml"))

End If

Posts = Cache("tumblrXE").Element("posts")

End Sub

This next bit of code, goes through all the posts and depending on the type displays them. I have only done regular, link and image post types.

<%
'if this value is 0, there are no posts.
If Posts.Attribute("total").Value <> "0" Then
'loop around all the posts
For Each itemElement As XElement In Posts.Elements("post")

If itemElement.Attribute("type") = "link" Then

Response.Write(itemElement.Attribute("date").Value)
Response.Write(itemElement.Elements("link-text").Value)
Response.Write(itemElement.Elements("link-url").Value)

End If

If itemElement.Attribute("type") = "photo" Then
          Response.Write(itemElement.Elements("photo-caption").Value)
          Response.Write(itemElement.Attribute("date").Value)
          Response.Write(itemElement.Elements("photo-link-url").Value)
          Response.Write(itemElement.Elements("photo-url").Value)
          Response.Write(itemElement.Elements("photo-caption").Value)
End If

If itemElement.Attribute("type") = "regular" Then
      Response.Write(itemElement.Elements("regular-title").Value)
      Response.Write(itemElement.Attribute("date").Value)
      Response.Write(itemElement.Elements("regular-body").Value)
End If

Next

End If

As I say, not sure if it’s the best way but it works for me so hopefully has given you a helping hand to implement your own solution.