I find the code which is written in python..
but i don't know python language and want to understand the code...
so if it is possible to convert it into java...
i understand it properly..
please help me with that....
and convert it into similar java code...


Here is my code:

import logging
import urllib
import re

from waveapi import events
from waveapi import model
from waveapi import robot
from waveapi import document


from django.utils import simplejson
from google.appengine.api import urlfetch


def OnDocChanged(properties, context):
    #Get Contents of current blip
    blip = context.GetBlipById(properties['blipId'])
    contents = blip.GetDocument().GetText()
    #Clean Up Text Remove <br> <br />
    p = re.compile('(<( )*br( )*[/]>)')
    contents = p.sub('\n',contents)
    #Run Search
    sRes += "["
    sRes += processBlip(contents)

    if(sRes != contents):
        sRes += "]"
        root_wavelet = context.GetRootWavelet()
        root_wavelet.CreateBlip().GetDocument().SetText(sRes)

def processBlip(blip):
    text = blip.GetDocument().GetText()
    returnText = ""
    charsAdded = 0
    #Setup Regular Expression to find search strings
    p = re.compile("^(.)_ ([0-9a-zA-Z ]+)",re.MULTILINE)
    searchStrings = p.finditer(text)
    flickrImages = []
    #Iterate through search strings
    for c in searchStrings:
        s = c.groups()
        if(s[0] == 'G'):
            sRet = '\nGoogle Search for "' + s[1] + '"\n\n'
            sRet += searchGoogle(s[1])
            #returnText = returnText[0:c.end()+charsAdded] + sRet +
'\n'+ returnText[c.end()+charsAdded+1:len(returnText)]
            #charsAdded += len(sRet)
            stringToBlip(blip,sRet)
        elif(s[0] == 'F'):
            sRet = searchFlickr(s[1])
            for imageURL in sRet:
                cImg = document.Image(imageURL)
                flickrImages.append(cImg)
            title = '\nFlickr Search for "' + s[1] + '"\n\n'
            imageListToBlip(blip,flickrImages,title)

#blip.GetDocument().SetText(contents)
def stringToBlip(blip,contents):
    #newBlip = blip
    newBlip = blip.GetDocument().AppendInlineBlip()
    if(contents):
        newBlip.GetDocument().SetText(contents)

def imageListToBlip(blip,imgList,text):
    #newBlip = blip
    newBlip = blip.GetDocument().AppendInlineBlip()
    newBlip.GetDocument().SetText(text)
    for i in imgList:
        newBlip.GetDocument().AppendElement(i)

def searchFlickr(query):
    results = []
    url = 'http://api.flickr.com/services/rest/?
method=flickr.photos.search&api_key=13b98c74bedac9150a428e87bc782e81&format=json&per_page=5&text=
%s' % (urllib.quote(query))
    content = urlfetch.fetch(url=url).content
    content =content[:-1].replace("jsonFlickrApi(","")
    for item in simplejson.loads(content)['photos']['photo']:
        results.append('http://farm'+str(item['farm'])
+'.static.flickr.com/'+str(item['server'])+'/'+str(item['id'])
+'_'+str(item['secret'])+'_m.jpg')

    return results

def searchGoogle(query):
    results = ""
    #print "Searching Google with query \"" + query + "\""
    url = 'http://ajax.googleapis.com/ajax/services/search/web?
v=1.0&start=0&q=%s' % (urllib.quote(query))
    content = urlfetch.fetch(url=url).content
    for item in simplejson.loads(content)['responseData']['results']:
        p = re.compile('<[/ ]?[a-zA-Z ]>')
        title = p.sub('',item['title'])
        content = p.sub('',item['content'])
        returnStr = title + '\n' +\
                    item['url'] + '\n' +\
                    content + '\n\n'
        results += returnStr
    return results


def OnRobotAdded(properties, context):
    """Invoked when the robot has been added."""
    root_wavelet = context.GetRootWavelet()
    root_wavelet.CreateBlip().GetDocument().SetText("Welcome to Wave
Search!")

def OnBlipSubmit(properties, context):
    blip = context.GetBlipById(properties['blipId'])
    processBlip(blip)

if __name__ == '__main__':
    wavesearch = robot.Robot('WaveSearch',
                         image_url='http://wave-search.appspot.com/
public/logo.png',
                         version='1.02',
                         profile_url='http://wave-search.appspot.com')
    wavesearch.RegisterHandler(events.WAVELET_SELF_ADDED,
OnRobotAdded)
    wavesearch.RegisterHandler(events.BLIP_SUBMITTED, OnBlipSubmit)
    #wavesearch.RegisterHandler(events.DOCUMENT_CHANGED, OnDocChanged)
    wavesearch.Run(debug=True)

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to