here I have code in python and now I want to transfer it in Java.

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)
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):
results.append('http://farm'+str(item)+'.static.flickr.com/'+str(item)+'/'+str(item)+'_'+str(item)+'_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):
p = re.compile('<[/ ]?[a-zA-Z ]>')
title = p.sub('',item)
content = p.sub('',item)
returnStr = title + '\n' +\
item + '\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)
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)

Recommended Answers

All 2 Replies

Jython comes to mind. It would allow you to run Python code under Java.

Jython comes to mind. It would allow you to run Python code under Java.

I was about to say that.
Jython is Python for Java, so just download Netbeans and the latest version of Jython, and copy the file over and see if it compiles.
I've never worked with Jython, so I can't really help with the details.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.