Category : Tutorial

Simple python Google Plus API

September 6, 2011

This is simple api to get user infor from Goopge+ user profile by oid.

# -*- coding: utf-8 -*-
import urllib2
from scrapy.selector import HtmlXPathSelector
from scrapy.http import HtmlResponse
from scrapy.contrib.linkextractors.sgml import BaseSgmlLinkExtractor
import simplejson
import re
try:
   import cPickle as pickle
except:
   import pickle
import time
import datetime

home_dir='/opt/gplus/'

base_url = 'https://plus.google.com/'
oid = '111241698016692196136'

def getinfor(oid):
    gplus={'oid':oid,'name':'','friends':'','followers':'','image':'','location':'','url':'','gender':'Other','sites':'','introduction':'',
           'occupation':'','other_name':'','bragging_rights':'','slogan':''}
    print oid
    gplus['oid']=oid
    url = base_url + oid
    gplus['url']=url
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    data = response.read()
    response = HtmlResponse(url=url, body=data)

    friends = 0
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//h4[@class="a-c-ka-Sf d-s-r"]/text()')
        friends =  r.extract()[0]
        reobj = re.compile(r'(?<=\()[0-9]+(?=\))')
        friends = reobj.findall(friends)[0]
        friends = int(friends)
    except:
        friends = 0
    gplus['friends']=int(friends)
    #print friends


    followers=0
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//h4[@class="a-c-ka-Sf"]/text()')
        followers =  r.extract()[0]
        reobj = re.compile(r'(?<=\()[0-9]+(?=\))')
        followers = reobj.findall(followers)[0]
        followers = int(followers)
    except:
        followers = 0
    gplus['followers']=int(followers)
    #print followers

    name=''
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//span[@class="fn"]/text()')
        name =  r.extract()[0]
    except:
        name = 'NA'
    gplus['name']=name
    print name

    other_name=''
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//div[@class="a-b-c-B-sj a-c-B-F a-c-nc-M-i"]/div[@class="a-c-B-F-Oa d-s-r"]/text()')
        other_name =  r.extract()[0]
    except:
        other_name = ''
    gplus['other_name']=other_name
    #print other_name

    slogan=''
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//div[@class="a-b-c-dd a-c-dd a-c-nc-M-i"]/span[@class="a-c-dd-G"]/text()')
        slogan =  r.extract()[0]
    except:
        slogan = ''
    gplus['slogan']=slogan
    #print slogan

    image=''
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//div[@class="a-Ba-V-z-N"]/img/@src')
        image =  'http:' + r.extract()[0].split('?sz=')[0]
    except:
        image = 'https://lh5.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg'
    gplus['image']=image
    #print image

    location=''
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//ul[@class="a-c-B-fo-Yf d-s-r"]/li/text()')
        tmp =  r.extract()
        for i in  tmp:
            location += '%s,' % i
    except:
        location = ''
    gplus['location']=location.strip(',')
    #print location

    gender=''
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//div[@class="a-b-c-bo a-c-B-F a-c-nc-M-i"]/div[@class="a-c-B-F-Oa d-s-r"]/text()')
        gender =  r.extract()[0]
        if gender==u'N\u1eef':
            gender=u'Female'
        if gender==u'Nam':
            gender=u'Male'
    except:
        gender = 'Other'
    gplus['gender']=gender
    #print gender

    sites=''
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//ul[@class="a-c-B-F-Yf Qd"]/li/div[@class="a-c-B-h h"]/a/@href')
        tmp =  r.extract()
        for i in  tmp:
            sites += '%s,' % i
    except:
        sites = ''
    gplus['sites']=sites.strip(',')
    #print sites

    introduction=''
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//div[@class="a-b-c-B-cg a-c-B-F-Io a-c-B-F a-c-nc-M-i"]/div[@class="a-c-B-F-Oa d-s-r note"]')
        introduction =  r.extract()[0]
    except:
        introduction = ''
    gplus['introduction']=introduction
    #print introduction

    bragging_rights=''
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//div[@class="a-b-c-gj a-c-B-F a-c-nc-M-i"]/div[@class="a-c-B-F-Oa d-s-r note"]')
        bragging_rights =  r.extract()[0].replace('div[@class="a-c-B-F-Oa d-s-r note"]','')[:-6]
    except:
        bragging_rights = ''
    gplus['introduction']=bragging_rights
    #print bragging_rights

    occupation=''
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//div[@class="a-c-B-F-Oa d-s-r title"]/text()')
        tmp =  r.extract()
        for i in  tmp:
            occupation += '%s,' % i
    except:
        occupation = ''
    gplus['occupation']=occupation.strip(',')
    #print occupation

    employment=''
    try:
        x = HtmlXPathSelector(response)
        r = x.select('//div[@class="a-c-B-Tb-da org"]/text()')
        tmp =  r.extract()
        for i in  tmp:
            employment += '%s,' % i
    except:
        employment = ''
    gplus['employment']=employment.strip(',')
    #print employment
    try:
        data = simplejson.dumps(gplus)
        f= open(home_dir+oid,'w')
        f.write(data)
        f.close()
    except:
        pass
    return gplus

def getoids(oid):
    oids = []
    url1='https://plus.google.com/_/socialgraph/lookup/visible/?o=%5Bnull%2Cnull%2C%22'+oid+'%22%5D&rt=j'
    url2='https://plus.google.com/_/socialgraph/lookup/incoming/?o=%5Bnull%2Cnull%2C%22'+oid+'22%5D&n=1000&rt=j'
    req = urllib2.Request(url1)
    response = urllib2.urlopen(req)
    data = response.read()
    #print data
    reobj = re.compile(r'[0-9]{21}')
    oids = reobj.findall(data)
    oids = list(set(oids))
    return oids

if __name__ == "__main__":

    print getinfor('110036305659240738176')
    print getinfor('106691752460849015439')
    print getinfor('112601327826875202182')
    print getinfor('109851101592108475761')
    print getinfor('108677978490654211560')


Thanks to : http://gpluscounter.com

Tutorial Google Plus API, python Google Plus API,

"The Definitive Guide to HTML5 Video" Code examples online

April 17, 2011

Code examples for the book "The Definitive Guide to HTML5 Video" now online on our site:

"The Definitive Guide to HTML5 Video" Code examples online


You can buy the book on Amazon.

Tutorial html5 video Code examples, The Definitive Guide to HTML5 Video, Silvia Pfeiffer,

Canvas html5 tutorial pixel rotation

April 17, 2011

To detect mouseover events for individual pixels with HTML5 Canvas, we can create a custom event handler called handleMouseover() that checks if the mouse coordinates match the coordinates of a drawn pixel. HTML5 Canvas Pixel Mouseover Example


Open in new window


<!DOCTYPE HTML>
<html>
<head>
<script>

var canvas=null;
var context = null;
var pixelArray = new Array();

function isMouseoverPixel(e, pixel) {
var mouseX = e.clientX - canvas.offsetLeft;
var mouseY = e.clientY - canvas.offsetTop;

if(mouseX == pixel.x &&
mouseY == pixel.y) {
return true;
}
else return false;
}

function Pixel(x,y,color) {
this.x=x;
this.y=y;
this.color=color;
}

function drawPixel(x,y, fillColor) {
context.beginPath();
context.rect(x,y,1,1);
context.fillStyle=fillColor;
context.fill();

pixelArray.push(
new Pixel(x,y, fillColor));
}

window.onload = function() {
canvas=document.getElementById("myCanvas");
context=canvas.getContext("2d");

var colors = new Array();
colors.push("red");
colors.push("orange");
colors.push("yellow");
colors.push("green");
colors.push("blue");
colors.push("purple");

// draw 100px * 100px random pixel square
for (n=0; n<100; n++) {
for (i=0; i<100; i++) {
var colorIndex =
Math.floor(Math.random()*6)
drawPixel(230+n,60+i,colors[colorIndex]);
}
}

drawEventDisplay("Mouseover a pixel...");
}

function drawEventDisplay(eventDisplay) {
context.clearRect(0,0,400,30);
context.font="18pt Calibri";
context.fillStyle="black";
context.fillText(eventDisplay, 10, 25);
}

function handleMouseover(e) {
for (n=0; n<10000; n++) {
var thisPixel = pixelArray[n];
if (isMouseoverPixel(e,
thisPixel)){
var color = thisPixel.color;
drawEventDisplay(color + " pixel!");
}
}
}

</script>
</head>
<body>
<canvas id="myCanvas" width="578" height="200"
onmousemove="handleMouseover(event)"></canvas>
</body>
</html>

Tutorial canvas html5 tutorial pixel rotation,

WebGL Cheat Sheet

April 15, 2011

WebGL has been getting a fair amount of buzz lately - and rightfully so, because it is cool. For those that don't know, WebGL is the 3D extension of the Canvas element, based on OpenGL ES 2.0. Having a standardized low-level graphics API like that available in the browser is pretty exciting stuff if you ask me. 3D canvas graphics has been long underway and not until just recently did it get to an interesting state when the first signs of WebGL showed up in both the (Mac) WebKit and Firefox nightly builds.


Note: This cheat sheet has become a bit out of date as the specs have been released and changes have been made that are not reflected in the cheat sheet. I will update it as soon as possible. Cheers.

It's still very much a work in progress and the official specs haven't been made public yet, I'm not even sure how far they (Khronos, the working group responsible) are in settling on the specs. So that means there aren't much in terms of references out there for people like me who are anxious to play around with this new toy. There are a few example demos from both the WebKit and the Mozilla camps and cool sites like Learning WebGL are starting to pop up. There's of course the OES 2.0 reference and the source code for both WebKit and Mozilla implementations is also readily available. So, I decided to just make my own reference sheet by combining those sources and the result is a condensed WebGL cheat sheet which fits on 4 pages - or 2 if you have good eyes and print two on each page.


As an added bonus, this exercise forced me to dig through the entire OES2.0 spec, which was great since I'm an OpenGL newbie and learning stuff is cool!


Of course, given the current state of WebGL, any of the information in this document is subject to change from day to day. I might most certainly have missed a bunch of things as well due to lack of insight and good references. In addition to that there also seem to be a few differences in the two implementations, so those will be corrected when I know what actually needs correcting.


I also don't have access to a Mac and since the WebKit implementation is Mac-only for now, I haven't actually seen it action. There might be more differences between the two than I've found just by glancing over the source.


Anyway, here it is in both PDF and HTML:


WebGL Cheat Sheet PDF


WebGL Cheat Sheet HTML


The HTML version has the extra bonus feature of tooltip information when you hover the mouse over (most of the) function parameters and enum values.


And corrections and suggestions are of course most welcome!


Source: http://blog.nihilogic.dk

Tutorial Cheatsheets, html5 example code, html5 cheat sheet,

How to add Mp3,Mp4,Ogg,Wav files to blogger using HTML5

April 13, 2011

How many of us know that the new blogger templates are HTML5 templates,In order to find whether yourtemplate is html5 look for the doctype, if the doctype is then your blogger template is HTML5 else your doctype should look like this .So we have to take advantage of using the features of HTML5 new tag elements.Most of the blogger/blogspot users find difficult to add Mp3,Mp4,Ogg,Wav file format to their post.Their are lot of other ways to add the above mentioned formats using gadgets from google or using some other web apps.Why not we use the HTML5 audio and video tag elements to our post to entertain our visitors easily using these tag elements. HTML5 have more features but in this post i am going look onto the audio and video tag elements.

How to add Mp3,Mp4,Ogg,Wav  files to blogger using HTML5

Support for browsers
Audio
As of Firefox 3.5, Chrome 3, Opera 10.5, and Safari 4, we can take advantage of many of the new HTML 5 features, including native audio support without the need for Flash.

Currently, there are 3 supported formats for the audio element(source):

FormatIE 8Firefox 3.5Opera 10.5Chrome 3.0Safari 3.0
Ogg VorbisNoYesYesYesNo
MP3NoNoNoYesYes
WavNoYesYesNoYes

Video

Currently, there are 2 supported video formats for the video element:

FormatIE 8Firefox 3.5Opera 10.5Chrome 3.0Safari 3.0
OggNoYesYesYesNo
MPEG 4NoNoNoYesYes

How to add Mp3,Mp4,Ogg,Wav  files to blogger using HTML5
How to add Audio and video tag elements to blogger
Audio
In the Edit html mode while you are posting add the below code and the replace src url with above mentioned formats to your src url
<audio src="song.ogg" controls="controls">

Your browser does not support the audio element. </audio>




Video
For more details on controls Look here
<video src="movie.ogg" width="320" height="240" controls="controls">

Your browser does not support the video tag. </video>


Source: http://www.fuzionpro.info


More :
How to add [CODE] block to a Blogger post?
In HTML, for quotes use <blockquote>put quote here</blockquote>.
For code use <code>put code here</code>.
To style them use blockquote { put styling code here } and code { put styling code here } in CSS section.
For code there is also another option:
http://bloggersentral.blogspot.com/2009/04/how-to-show-code-in-blog-post.html

Tutorial add code to blogger html5, add html5 mp3 to blog,