Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 31-38 of 38 total

wsdl the google API (search google with ruby)

sign up to get a key first

   1  
   2  require 'soap/wsdlDriver'
   3  $KCODE = "UTF8"
   4  key = 'LVJnAm5QFHblahblahblah your key here'
   5  
   6  #create driver
   7  wsdl = "http://api.google.com/GoogleSearch.wsdl"
   8  driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
   9  query = "your query string here"
  10  start = 0
  11  max = 10
  12    
  13  # see http://dev.ctor.org/soap4r/browser/trunk/sample/wsdl/googleSearch/wsdlDriver.rb
  14  @results = driver.doGoogleSearch( key, query, start, max, true, "", true, 'lang_en', '','')
  15  snippets = @results.resultElements.collect { |r| r.snippet } # you can get all kinds'a' info here
  16  self.update_attribute(:html, snippets.join("\n")) # or whatever

Google map pin

   1  
   2  def pin(x,y,char=None):
   3      # top-left is x-5, y-17
   4      points = [ (0,0), (-2,-6), (-5,-10), (-5,-14), (-2,-17), \
   5                        (2,-17), (5,-14), (5,-10), (2,-6)]
   6      c.polygon([(x+dx,y+dy) for dx,dy in points], 0, (255,119,107), 1)
   7      if char:
   8          c.text((x-2, y-7), unicode(char))
   9      else:
  10          c.point((x,y-12), 0, width=5)

Instead of adding a pin image to the program, this function
will draw a google map pin at point (x,y) instead.
If you give it a char, it will put that char in the middle of
the pin as well
   1  
   2  pin(20,20,'A') # A in the pin
   3  pin(40,40)  # just bull-eye

Send message with Google Talk

Copied from Jkx's code here
http://www.larsen-b.com/Article/214.html
   1  
   2  import xmpp
   3  
   4  login = 'Your.Login' # @gmail.com 
   5  pwd   = 'YourPassword'
   6  
   7  cnx = xmpp.Client('gmail.com')
   8  cnx.connect( server=('talk.google.com',5223) )
   9  cnx.auth(login,pwd, 'botty')
  10  
  11  cnx.send( xmpp.Message( "YourFriend@gmail.com" ,"Hello World form Python" ) )

Using Google Maps EZ

Chris Houser writes an easy HTML API for Google Maps.
Here's the simplest example
   1  
   2  <html>
   3  <head>
   4    <script src="http://maps.google.com/maps?file=api&v=1&key=yourgmapskey"></script>
   5    <script src="http://bluweb.com/chouser/gmapez/gmapez.js"></script>
   6  </head>
   7  <body>
   8  
   9    <div class="GMapEZ" style="width: 300px; height: 300px;"></div>
  10  
  11  </body>
  12  </html>

See more examples here
http://bluweb.com/us/chouser/gmapez/docs.html

Converting between 2 google map tile types

Google maps is a marvelous app.
I try to program a prototype of it on pys60
http://bigbold.com/snippets/posts/show/458

There I only show the default map, not the satellite images.
Retrieving a different mode isn't that difficult.
I read the info from here
http://intepid.com/2005-07-17/21.50/
Then I begin comparing the 2 tile types of the same area
(around California)
http://mt.google.com/mt?v=w2.5&x=20&y=49&zoom=10 (map)
http://kh.google.com/kh?v=3&t=tqtsqrqt (satellite)

Here's the conversion routine between x,y,zoom and quadtree
   1  
   2  def quadtree(x,y, zoom):
   3  	out = []
   4  	m = {(0,0):'q', (0,1):'t', (1,0):'r', (1,1):'s'}
   5  	for i in range(17-zoom):
   6  		x, rx = divmod(x, 2)
   7  		y, ry = divmod(y, 2)
   8  		out.insert(0, m[(rx,ry)])
   9  	return 't' + ''.join(out)

Then to convert back
   1  
   2  def xyzoom(quad):
   3  	x, y, z = 0, 0, 17
   4  	m = {'q':(0,0), 't':(0,1), 'r':(1,0), 's':(1,1)}
   5   	for c in quad[1:]:
   6  		x = x*2 + m[c][0]
   7  		y = y*2 + m[c][1]
   8  		z -= 1
   9  	return x, y, z

Using them is simple
   1  
   2  >>> quadtree(20,49,10)
   3  'tqtsqrqt'
   4  >>> xyzoom('tqtsqrqt')
   5  (20, 49, 10)
   6  >>> sat_url = 'http://kh.google.com/kh?v=3&t=' + quadtree(20,49,10)

Using google map on mobile phone

Download the tile images for testing
   1  
   2  from urllib import urlretrieve
   3  turl =  'http://mt.google.com/mt?v=w2.4&x=%s&y=%s&zoom=%s'
   4  tfile = '%s-%s-%s.gif'
   5  z = 12
   6  for x in range(5,10):
   7    for y in range(10,14):
   8      urlretrieve(turl % (x,y,z), tfile % (z,y,x))

Then put the images in the phone. Then use the following code
to browse around the images (and zoom in/out)
   1  
   2  from appuifw import *
   3  from key_codes import *
   4  from graphics import Image
   5  import e32
   6  
   7  app.screen = 'full'
   8  app.body = c= Canvas()
   9  
  10  x, y, z = 10*256, 24*256, 11
  11  dirname = u'C:\\system\\data\\gmap\\'  # where I save the tile images
  12  
  13  def draw():
  14      gx, ox = divmod(x, 256)
  15      gy, oy = divmod(y, 256)
  16      f = dirname + '%s-%s-%s.gif' % (z,gy,gx)
  17      c.blit(Image.open(f), target=(-ox,-oy))
  18  
  19      if ox > 80:
  20          f = dirname + '%s-%s-%s.gif' % (z,gy,gx+1)
  21          c.blit(Image.open(f), target=(256-ox,-oy))
  22      if oy > 48:
  23          f = dirname + '%s-%s-%s.gif' % (z,gy+1,gx)
  24          c.blit(Image.open(f), target=(-ox,256-oy))
  25      if ox > 80 and oy > 48:
  26          f = dirname + '%s-%s-%s.gif' % (z,gy+1,gx+1)
  27          c.blit(Image.open(f), target=(256-ox,256-oy))
  28  
  29  def move(dx,dy):
  30      global x, y
  31      x += dx * 50
  32      y += dy * 50
  33      draw()
  34  
  35  def zoomin():
  36      global x,y,z
  37      x = x*2 + 88
  38      y = y*2 + 104
  39      z = z-1
  40      draw()
  41  
  42  def zoomout():
  43      global x,y,z
  44      x = x/2 - 44
  45      y = y/2 - 52
  46      z = z+1
  47      draw()
  48  
  49  c.bind(EKeyRightArrow,lambda:move(1, 0))
  50  c.bind(EKeyLeftArrow,lambda:move(-1, 0))
  51  c.bind(EKeyUpArrow,lambda:move(0, -1))
  52  c.bind(EKeyDownArrow,lambda:move(0, 1))
  53  c.bind(EKeySelect, zoomin)
  54  c.bind(EKeyStar, zoomout)
  55  
  56  running = 1
  57  def quit():
  58      global running
  59      running = 0
  60  app.exit_key_handler= quit
  61  
  62  draw()
  63  while running:
  64      e32.ao_sleep(0.1)

I put a bit more explanation in Nokia's forum here. (Newer version is available)
http://discussion.forum.nokia.com/forum/showthread.php?s=&postid=153609
Also see the screenshot here
http://flickr.com/photos/korakot/30189624/

Atom / RSS feed of your GMail account

   1  https://USERNAME:PASSWORD@gmail.google.com/gmail/feed/atom

Stop Google Web Accelerator in your Rails apps

   1  class ApplicationController < ActionController::Base
   2    before_filter :disable_link_prefetching
   3  
   4    private
   5      def disable_link_prefetching
   6        if request.env["HTTP_X_MOZ"] == "prefetch" 
   7          logger.debug "prefetch detected: sending 403 Forbidden" 
   8          render_nothing "403 Forbidden" 
   9          return false
  10        end
  11      end
  12  end


Code by David Heinemeier Hansson.
« Newer Snippets
Older Snippets »
Showing 31-38 of 38 total