Posts for Tag: code snipplets

Full list of Coca Cola brands - had no idea there were so many

As a part of my Ruby learning process, i decided to write something to retrieve the full list of brands that Coca Cola currently owns from their website. The list is split across multiple pages, but with some Ruby magic and a nice library called Hpricot to traverse the HTML it was 5 minutes of work :)

Here's the script: require 'open-uri' require 'rubygems' require 'hpricot' require 'pp'

my_file = File.new("cola.txt", 'w')

('a'..'z').each do |letter| open("http://www.thecoca-colacompany.com/brands/product_list_#{letter}.html") do |f|

# meta info #pp f.meta

#pp 'Content-Type: ' + f.content_type #pp 'Last modified: ' + f.last_modified.to_s

doc = Hpricot.XML(f) div = doc/"#products_link_list" (div/:ul).each do |ul| (ul/:li).each do |li| if (li/:a).first my_file.puts((li/:a).first.inner_html) else my_file.puts li.inner_html.to_s end end end

end end And here's the full list of brands that belong to Coca Cola:

A&W Acueducto Ades Agua Manantial Ali Alive Alpina Ambasa Ameyal Amorino Andina/Andina light Andina Fortified Andina Fresh Andina Frut Andina Nectar Apollinaris Aqua+ Aquabona Aquactivede by Aquarius Aquana Aquarius Aquarius Active Diet BACARDI Mixers* BACARDI Premium Mixers* Bajoru Gira Bankia Barq's Barrelitos BEAT Beautia Bebere Benedictino Beverly Bevi Bibo Big Crush Big Tai Bimbo Bimbo Break Biotime Bird's Nest* Bistra Bistrone Bjare Blackfire Boco Bogadera Bom Bit Maesil BonAqua/BonAqa Borneo Botaniq 1QQ Botany BPM Brazzi Bright & Early Brisa Agua Pura Brisa by Bonaqua Brisa con un Toque de Gas Brisa Spa Bu Bubbly burn buzz CAFÉ ZU caffeine-free Barq's caffeine-free Coca-Cola caffeine-free Diet Coke/caffeine-free Coca-Cola light Calypso Canada Dry Canning's Cappy Caprice Caribou Coffee* Carioca Cascal Carver's Cepita Chaqwa Charrua Chaudfontaine Cheers cherry Coke cherry Coke zero Chinotto Chinotto light Ciel CITRA Club Coca-Cola Coca-Cola Black Cherry Vanilla Coca-Cola Blāk Coca-Cola C2 Coca-Cola Citra Coca-Cola light/Diet Coke Coca-Cola with Lemon Coca-Cola with Lime Coca-Cola with Raspberry Coca-Cola Zero Cocoteen Cola Guaraná Jesus Country Club Cresta Cristal Crush Crusta Crystal Damla Minera DANNON* DASANI DASANI Active DASANI Balance DASANI Flavors DASANI Nutriwater DASANI Plus Del Valle Del Valle Frut Del Valle Frutsi Del Valle Frutsi Desarrollo Del Valle Kapo Del Valle Mais light Del Valle light Delaware Punch diet A&W diet Andina Frut/Andina Frut light diet Andina Nectar/Andina Nectar light diet Barq's diet Canada Dry Diet cherry Coke Diet Coke/Coca-Cola light Diet Coke Black Cherry Vanilla Diet Coke Citra/Coca-Cola light Citra Diet Coke Plus Diet Coke Sweetened with Splenda® Diet Coke with Lemon/Coca-Cola light with Lemon Diet Coke with Lime/Coca-Cola light with Lime Diet Coke with Raspberry Diet Crush diet Dr Pepper/Dr Pepper Zero diet Fanta/Fanta light/Fanta Zero/Fanta Free diet Freskyta diet INCA KOLA diet Kia Ora diet Krest diet Lift/Lift light diet Lilt/Lilt Zero diet Master Pour diet Mello Yello diet Mezzo Mix diet Minute Maid Soft Drink diet Nestea/Nestea light* diet Northern Neck diet Oasis diet Quatro/Quatro light diet Schweppes diet Seagram's* diet Sprite/Sprite light/diet Sprite Zero/Sprite Zero diet Squirt diet Tai/Tai light diet Vanilla Coke Disney Xtreme Cooler* Diva Dobriy Dogadan Dorna Dr Pepper E2 Earth and Sky Eight O'Clock El Rayek Enviga* Epika Escuis Eva Water Fanta Fanta Verdia Far Coast Felicia Finley Fioravanti Fire Five Alive Flavor Rage Fontana Fresca Fresca 1 Frescolita Freskyta Fresquinha Fress Frestea* Friolin Frisco Frugos Frugos Fresh Fruit Solutions Fruitia Fruitier Fruitopia Fruktime Frutina Frutonic Full Throttle Full Throttle Blue Demon Full Throttle Sugar Free Funchum Orange Drink Furusato Dayori FUZE Healthy Infusions GEORGIA GEORGIA Club GEORGIA Gold GEORGIA Grande Gini Girios Gira glacéau smartwater glacéau vitaminwater glacéau vitaminwater zero Gladiator Godiva* Belgian Blends Gold Peak Gold Spot Golden Crush Goombay Punch Goulburn Valley Grapette Groovy Guaraná Kuat light Guaraná Kuat Zero Hajime Haru No Mint Shukan Hawai Healthworks Heppinger Hero Hi Spot Hi-C Hires Hit Horizon Hot Point Huang Ice Dew Ice Dew 3+ Ice Tea Leao illy issimo* INCA KOLA Ipsei Izvorul Alb Jaz Cola Jericho Jet Tonic Jolly Juice Joy Joya Juan Valdez Juices To Go Just Juice Kani Kapo Kapo Zero Karada Meguri-Cha Kashaya Tea Keloco Keri Kia Ora Kildevaeld Kilimanjaro Kin Kinley Kist Kiwi Blue Kochakaden Kola Inglesa Koumisoukai Krest Kristal Kropla Beskidu Krushka & Bochka Kuat Kuat Guaraná Kuat light Kuli Kyun La Jolla La Vuelta Lanitis Lanitis Extra Lanitis Filaraki Leao Green Tea Leao Guaraná Power Leao Iced Tea Less Is More, Kinley Lift Lift Plus Lift Plus light Lilia Lilt Limca Limelite Limonade Linnuse Lion Love Body Maaza Mad River Malvern Manantial Mare Rosso Marocha Master Chill Master Pour Matte Leao Matusov Pramen Mazoe Mello Mello Yello Mer Mezzo Mezzo Mix Miami Mickey* Migoro-Nomigoro Minaqua Mineragua Minute Maid Minute Maid Active Minute Maid Antiox Minute Maid Deli Minute Maid Duofrutas Minute Maid Fruit Plus Minute Maid Heart Wise Minute Maid Just 10 Minute Maid Light Minute Maid Mais Minute Maid Nutri+ Minute Maid Premium Minute Maid Soft Drink Minute Maid Splash Mireille Mission Mone Montefiore Mori No Mizudayori Morning Deli Mother Mr. Pibb multiVita Nada Nagomi Nalu Namthip Nanairo Acha Naturaqua Natures Own* Nectarin Nestea* Nestea Choglit* Nestea COOL* Nestea Viteao* Nevada Neverfail Next Nico Nordic Mist Northern Neck NOS Nos Entendemos Oasis Odwalla Olimpija Orchy Paani Treated Water Pacific Orchard Pampa Paradise Sun Parle Pasazade Pearona Peats Ridge Springs Pibb Xtra Pibb Zero Piko Pilskania Planet Java* Play Energy Drink Pocket Dr Poiana Negri Polar Brew Poms Ponkana Pop Portello POWERADE POWERADE Advance POWERADE alive POWERADE aqua+ POWERADE Balance POWERADE light POWERADE OPTION POWERADE Zero Powerplay Premio Presta Presta light Pulp Orange Pulpy Pump Pumped Enhanced Hydration Pura Sol Qoo Quatro Quwat Jabal R'fresh Ramlosa Real Real Leaf Real Gold Rehab Relentless Rich Richy Riwa Roemerquelle Rosa Rosalta Roses Royal Tru Royal Tru light Safety First Safia Sahtain Samantha Samurai San Luis Sarsi Saryusaisai Schuss Schweppes Schweppes Abbey Well Seagram's* Seltz Sensation Sensun Gazoz SENZAO Shandy Shangri-La ShiZen* Shock Signature Sim Simba Simply Apple Simply Grapefruit Simply Lemonade Simply Limeade Simply Orange Smart Sobo Sokenbicha Solo Sonfil Soonsoo 100 Southern Sun Sparkle Sparletta Sparletta Iron Brew Splash Splice Sport Sprite Sprite 3G Sprite Duo Sprite Flavors Sprite Ice Sprite Remix Sprite Zero/diet Sprite/Sprite light /diet Sprite Zero Spur Squirt Stoney Gold Ginger Beer Sucos Mais Sunfill SURGE Svali Sveva Sweecha TaB TaB energy TaB X-Tra TADAS Tai The Spirit of Georgia The Wellness From Coca-Cola Thextons Thums Up Tian Yu Di/Heaven and Earth Tiky Toppur Top's Tropi Tropical Turkuaz Ultra Urge Urun V Florida 7 Valle Frut Valpre Valser Valser Viva Vanilla Coke Vanilla Coke Zero VAULT VAULT Zero Vegitabeta VICA Vio Vita Vital Wilkins Wink Winnie the Pooh* Yangguang Yangguang Juicy T Yo Conozco a Hugo Yoli Youki Yuan Ye

Irony of searches and one iPad visit to my blog

Just a couple of pictures this time.

The first one is about something i noticed i was doing, while searching for a code fragment. One could call it ironic, trying to find "find" :)

The other is proof of my amazement by the fact that there was one person who visited my blog using an iPad :D

UPDATE: Turns out i was looking at a 2 day period only. This month there have been actually 4 iPad visits, hehe :)

Websockets tutorial/example with pywebsocket

As everyone already knows, Google Chrome now supports websockets. In essence, this allows you to keep a connection open with a webserver indefinitely (analogous to typical sockets) and send data bi-directionally. Unfortunately Chrome is the only browser currently supporting this, but I'm pretty sure this will change.

So I decided to give this a try and experiment a bit with it. This is my step by step process on getting a web page opening a websocket to a server and receiving the server's date and time every second. It is based on an article by Joe Armstrong, though he uses Erlang for the server, while I decided to follow the easy road and use Google's pywebsocket - an apache module (uses mod_python) that allows you to create handlers for websocket connections in a easy-to-use fashion. The code also contains a way to start a standalone server (i.e. not requiring apache) for testing purposes.

So here are the steps I took to get this working: 1. Create a web page This is the code I borrowed from Joe, though slightly modified to fit my purposes (it requires jquery, by the way): <html>

<head>

<script src="jquery-1.3.2.min.js"></script> <script>

$(document).ready(function(){

var ws;

if ("WebSocket" in window) { debug("Horray you have web sockets. Trying to connect..."); ws = new WebSocket("ws://localhost:9998/echo");

ws.onopen = function() { // Web Socket is connected. You can send data by send() method. debug("connected..."); ws.send("hello from the browser"); ws.send("more from browser"); };

run = function() { var val=$("#i1").val(); // read the entry $("#i1").val("");       // and clear it ws.send(val);           // tell erlang return true;            // must do this };

ws.onmessage = function (evt) { //alert(evt.data); var data = evt.data; var i = data.indexOf("!"); var tag = data.slice(0,i); var val = data.slice(i+1); $("#" + tag).html(val); };

ws.onclose = function() { debug(" socket closed"); }; } else { alert("You have no web sockets"); };

function debug(str){ $("#debug").append("<p>" +  str); };

}); </script>

</head>

<body>

<h1>Interaction experiment</h1>

<h2>Debug</h2> <div id="debug"></div>

<fieldset> <legend>Clock</legend> <div id="clock">I am a clock</div> </fieldset>

</body>

</html> 2. Download and install pywebsocket Checkout the code with svn checkout http://pywebsocket.googlecode.com/svn/trunk/ pywebsocket-read-only

Then do python setup.py build and sudo python setup.py install inside the src folder. This will install it into your python environment. 3. Being lazy, means we will change an example handler The way pywebsocket works is delegating the connections to something they call handlers. In the pywebsocket-read-only/src/example folder you will find a file named echo_wsh.py. They have this convention where handlers are named <entry_point>_wsh.py. This means that when you later call (from your web page) the url http://localhost:9998/echo the server will delegate the processing of that connection to that file.

I modified that file to something like this: # Copyright 2009, Google Inc. # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # #     * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. #     * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following disclaimer # in the documentation and/or other materials provided with the # distribution. #     * Neither the name of Google Inc. nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from mod_pywebsocket import msgutil from datetime import datetime import time

_GOODBYE_MESSAGE = 'Goodbye'

def web_socket_do_extra_handshake(request): print 'Connected.' pass  # Always accept.

def web_socket_transfer_data(request): while True: time.sleep(1) date = datetime.now() #try: #    line = msgutil.receive_message(request) #except Exception, e: #    print 'Foi com os porcos' #    raise e #print 'Got something: %s' % line #msgutil.send_message(request, line) msgutil.send_message(request, 'clock!%s' % date) #if line == _GOODBYE_MESSAGE: #    return So basically whenever a new connection is made to this entry point, a call to web_socket_do_extra_handshake is made. After that, web_socket_transfer_data is called and it's your responsibility to create the loop that receives messages and handles the flow (as you can see in the commented lines). I don't care about that right now, since I only want to push the date and time to the client every second. 3. Start the standalone server Go to the pywebsocket-read-only/src/mod_pywebsocket folder and run the following command: sudo python standalone.py -p 9998 -w ../example/ This will start the server in port 9998 and use the handlers directory specified by the -w option. That is where our echo_wsh.py lives. 4. Test it :D So now open your browser (remember, only chrome supports websockets currently) and open the html file your created in the beginning. Voilá, server's date and time every second in the clock div.

Android location provider mock

UPDATE (2013/05/28): Since then, i've moved from Android to iOS development. The reason was simple: i got an iPhone from my company. As such, and unfortunately, i haven't dedicated myself to Android as much, sorry for that. In any case, let me take the chance to shamelessly advertise my latest iOS app, Toilet Papery, a simple yet powerful groceries app ;)

So, yeah, I resumed playing around with android, this time version 2.0.

I'm really tempted to buy the new Motorola Milestone that should come out in Europe sometime between... now... and early next year, so I wanna be ready to create all the crazy stuff I have in mind for it :)

One of the things I noticed was that it's not that straightforward to provide sample GPS data. Somehow the emulator's GPX and KMZ functionality is not working fine for me and, even if it was, I don't want to load the file everytime (i.e. I'm lazy).

So, after some googling around, here's my solution to read points from a file and feed them 1 per second to the location manager, so I can finally work with them in my app:

My main activity implements LocationListener, so it can be passed to the LocationManager to receive GPS events. Here's what I do when creating my activity: public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.main);

((TextView) this.findViewById(R.id.textView)).setText("Something else");

// LocationManager locationManager = (LocationManager) // getSystemService(Context.LOCATION_SERVICE); // locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, // 0, 0, this);

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

String mocLocationProvider = LocationManager.GPS_PROVIDER; locationManager.addTestProvider(mocLocationProvider, false, false, false, false, true, true, true, 0, 5); locationManager.setTestProviderEnabled(mocLocationProvider, true); locationManager.requestLocationUpdates(mocLocationProvider, 0, 0, this);

try {

List data = new ArrayList(); InputStream is = getAssets().open("data.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = reader.readLine()) != null) {

data.add(line); } Log.e(LOG_TAG, data.size() + " lines");

new MockLocationProvider(locationManager, mocLocationProvider, data).start();

} catch (IOException e) {

e.printStackTrace(); } } This will basically setup the test location provider and read the points into a list. Then I feed that stuff to my mock location provider (just a normal thread) that will read them 1 per second and trigger the new location back to this activity. Here's the code for MockLocationProvider: public class MockLocationProvider extends Thread {

private List data;

private LocationManager locationManager;

private String mocLocationProvider;

private String LOG_TAG = "faren";

public MockLocationProvider(LocationManager locationManager, String mocLocationProvider, List data) throws IOException {

this.locationManager = locationManager; this.mocLocationProvider = mocLocationProvider; this.data = data; }

@Override public void run() {

for (String str : data) {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace(); }

// Set one position String[] parts = str.split(","); Double latitude = Double.valueOf(parts[0]); Double longitude = Double.valueOf(parts[1]); Double altitude = Double.valueOf(parts[2]); Location location = new Location(mocLocationProvider); location.setLatitude(latitude); location.setLongitude(longitude); location.setAltitude(altitude);

Log.e(LOG_TAG, location.toString());

// set the time in the location. If the time on this location // matches the time on the one in the previous set call, it will be // ignored location.setTime(System.currentTimeMillis());

locationManager.setTestProviderLocation(mocLocationProvider, location); } } } Notice the location.setTime() call. Read the comment why it is necessary. Took me forever to find this one in google :D

Peace and great Androiding ;)