# # Copyright (C) 2007 Mobio Networks, Inc. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # =begin provides advertisement fetch for Google's Mobile Adsense (alpha level keyword version and released "page scraping version" =end require 'digest/sha1' require 'collections/sequenced_hash' module Rmobio module Ads module AdSense @@adsense=$ADSENSE @@adServer="http://pagead2.googlesyndication.com/pagead/ads" @@adClient="ca-mb-pub-0061196910475770" @@mobioUseragent="Mobio" # we should probably come up with a better string # Determines whether this request should involve adSense. def doAdSense() # is adsense enabled for the project? if (@@adsense != 1) return false end # returns true of false depending on what client is making this request. if (@client == 'facebook') return false else return true end end def getAdParms getAd(params[:keywords]) end #$GLOBALS['google']['ad_type']='text'; #$GLOBALS['google']['channel']='8618723264'; #$GLOBALS['google']['client']='pub-0061196910475770'; #$GLOBALS['google']['format']='mobile_single'; #$GLOBALS['google']['https']=$_SERVER['HTTPS']; #$GLOBALS['google']['host']=$_SERVER['HTTP_HOST']; #$GLOBALS['google']['ip']=$_SERVER['REMOTE_ADDR']; #$GLOBALS['google']['markup']='xhtml'; #$GLOBALS['google']['oe']='utf8'; #$GLOBALS['google']['output']='xhtml'; #$GLOBALS['google']['ref']=$_SERVER['HTTP_REFERER']; #$GLOBALS['google']['url']=$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; #$GLOBALS['google']['useragent']=$_SERVER['HTTP_USER_AGENT']; def getAdByPage if (doAdSense()) @@userId=request.env['HTTP_USERID'] if (@@userId.nil?) @@userId="101" end adArgs= SequencedHash.new adArgs["ad_type"]="text" adArgs["channel"]="8618723264" adArgs["client"]="pub-0061196910475770" adArgs["format"]="mobile_single" adArgs["host"]=request.host adArgs["ip"]=request.remote_ip adArgs["markup"]="xhtml" adArgs["output"]="xhtml" adArgs["oe"]="utf8" adArgs["ref"]=request.referer adArgs["url"]="http://"+request.env["HTTP_HOST"]+request.env["REQUEST_URI"] #adArgs["useragent"]=request.user_agent adArgs["eip"]=Digest::SHA1.hexdigest(@@userId)[0..20] # oneway hashed userid # now built the URL to call out to based upon the base URL and ad hash adURL=@@adServer + "?" first=1 # dont put ampersand on first one adArgs.each_key do |x| if adArgs[x] (adURL=adURL+"&") unless first first=nil # start putting in &s adURL = adURL + x + "=" + adArgs[x] end end # ok, now call google's mobile adSense service and get back the full ad @pagead=open(adURL).read end end # this returns an adfrom Google's mobile AdSense, given a supplied set of keywords def getAd(keywords) puts "Adsense" doTinyUrl=true if (doAdSense()) @@userId=request.env['HTTP_USERID'] if (@@userId.nil?) @@userId="101" end # build up the various arguments in the adArgs hash adArgs= SequencedHash.new adArgs["ad_type"]="text" adArgs["client"]=@@adClient # get it from gateway instead! adArgs["format"]="mobile_single" adArgs["ip"]=request.remote_ip adArgs["markup"]="xhtml" adArgs["output"]="wml" adArgs["oe"]="utf-8" adArgs["url"]="http:%3A%2Fwww.getmobio.com" # mobio's home site adArgs["useragent"]=@@mobioUseragent # user agent for our browser. adArgs["eip"]=Digest::SHA1.hexdigest(@@userId)[0..20] # oneway hashed userid if (keywords) adArgs["kw"]=CGI::escape(keywords) adArgs["kw_type"]="broad" end # now built the URL to call out to based upon the base URL and ad hash adURL=@@adServer + "?" first=1 # dont put ampersand on first one adArgs.each_key do |x| if adArgs[x] (adURL=adURL+"&") unless first first=nil # start putting in &s adURL = adURL + x + "=" + adArgs[x] end end # ok, now call google's mobile adSense service and get back the full ad @ad=open(adURL).read if @ad logger.info "Returned ad" + @ad adDoc = REXML::Document.new @ad @text = adDoc.elements['//p'] if @text # do we have a

element # ok, now text has the full ad display content including links # grab @url, @urltext (link text for url), @phone, @phonetext (link text for call) if defined? @text[1].attributes['href'] and @text[1].attributes['href'] @adurl = doTinyUrl ? tinyUrl(@text[1].attributes['href']) : @text[1].attributes['href'] else @adurl = "nolink.rwap" end @urltext=@text[1].text @extratext=@text[2].to_s if @text[2] # this should have everything else that is not a child element if (@text.size>3 and @text[3]) # only process @text[3] for phone stuff if it exists @phone = (doTinyUrl ? tinyUrl(@text[3].attributes['href']) : @text[3].attributes['href']) if defined? @text[3].attributes['href'] and @text[3].attributes['href'] @phonetext=@text[3].text if defined? @text[3].text and @text[3].text end end end # if we get an ad back from adsense @ad # return the full ad text end end private def tinyUrl(url) shrinker="http://tinyurl.com/create.php?url="+url result=open(shrinker).read # ITS NOT WELLFORMED HTML SO WE CAN'T USE XML TO PARSE #result=result.gsub(/\<\/head\>/,"") #resultDoc= Document.new result #tinyText=resultDoc.elements['//blockquote'][1] #tinyLink=tinyText.elements['./b'].text # find the second

and extra out the contents of it pattern='
' firstblock=result.index(pattern) if (firstblock) secondblock=result[firstblock+1...result.size].index('
')+ firstblock+1 + pattern.size endsecondblock=result[secondblock...result.size].index('') + secondblock tinyLink=result[secondblock...endsecondblock] end end end end end