<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Informático en apuros</title>
	<atom:link href="http://diegosuarezgarcia.net/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://diegosuarezgarcia.net/blog</link>
	<description>Aventuras y desventuras de un ingeniero técnico. Linux, Ruby, informática general y otras hierbas</description>
	<lastBuildDate>Fri, 09 Dec 2011 09:19:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Rootear nexus S Ubuntu 11.10</title>
		<link>http://diegosuarezgarcia.net/blog/?p=202</link>
		<comments>http://diegosuarezgarcia.net/blog/?p=202#comments</comments>
		<pubDate>Fri, 09 Dec 2011 09:19:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sin categoría]]></category>

		<guid isPermaLink="false">http://diegosuarezgarcia.net/blog/?p=202</guid>
		<description><![CDATA[Para todos aquellos que, como yo, os pica la camiseta y no sois capaces a esperar a que vuestro Nexus S reciba la OTA de Ice Cream Sandwich, y que no tenéis ganas de bucear en internet cómo demonios haceros con el root de vuestro chisme, dejo aquí una guía for dummies. como siempre, disclaimer: [...]]]></description>
			<content:encoded><![CDATA[<p>Para todos aquellos que, como yo, os pica la camiseta y no sois capaces a esperar a que vuestro Nexus S reciba la OTA de Ice Cream Sandwich, y que no tenéis ganas de bucear en internet cómo demonios haceros con el root de vuestro chisme, dejo aquí una guía for dummies. como siempre, disclaimer: <strong>ESTAS COSAS SON PELIGROSAS PARA EL DISPOSITIVO</strong>. NO TIENE POR QUÉ PASAR NADA, PERO <strong>PUEDE</strong> PASAR. NO ME RESPONSABILIZO DE LO QUE HAGAIS CON VUESTROS MOVILES O DE SI PASAN A SER UN PISAPAPELES MUY CARO.<br />
Por cierto, huelga decir que hay que hacer copia de seguridad. El proceso hace un reset de fábrica al teléfono, así que perderemos todas las aplicaciones, datos, fotos, videos, etc. del teléfono.<br />
Ingredientes:<br />
* android sdk<br />
* fastboot para linux<br />
* recovery.img<br />
* su-2.3.6.1-ef-signed.zip<br />
<a href='http://diegosuarezgarcia.net/blog/wp-content/uploads/2011/12/nexus-root.zip'>Menos el android SDK, el resto lo tenéis en este zip</a></p>
<p>Lo primero es desbloquear el fastboot. Para ello, tenemos que instalarnos el SDK de Android, <a href="http://developer.android.com/sdk/index.html">descargable desde su pagina web</a>. Viene sin una de las herramientas que necesitamos, fastboot, así que os la dejo en el pack de la descarga.<br />
Tenemos que iniciar, antes de nada, el movil en modo fastboot, que significa hacerlo apretando el boton de subir volumen y el de encendido a la vez hasta que se encienda. Veréis que la pantalla es distinta a la habitual.<br />
En esta pantalla se puede ver que nuestro dispositivo está bloqueado de fábrica, como se ve en la imagen (lock state: locked) <img alt="" src="http://www.mestreandroid.com.br/wp-content/2011/11/bootloader-nexus-s.jpg" class="alignright" width="540" height="405" /><br />
Desbloqueando el movil:<br />
* Enchufar por cable USB al PC.<br />
* Verificar, ejecutando &#8220;fastboot devices&#8221;, que reconoce el dispositivo. Si no sale nada, no lo ha reconocido, y puede que tengais que editar el archivo /etc/udev/rules.d/51-android.rules, añadiendole la siguiente linea:<br />
SUBSYSTEM==&#8221;usb&#8221;, SYSFS{idVendor}==&#8221;18d1&#8243;, MODE=&#8221;0666&#8243;<br />
En cualquier caso, después de este paso el fastboot devices debería mostrarnos una linea con un numero de serie. Eso significa que ya ha reconocido nuestro trasto, así que seguimos.<br />
* Hacemos el unlock en si, con la siguiente orden (en el directorio donde tengamos fastboot, se entiende)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>fastboot oem unlock</pre></div></div>

<p>Una vez desbloqueado, veremos que al reiniciar, debajo del logo de google, aparece un candado abierto. Buena señal <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Ahora tenemos que copiar el paquete de &#8220;superuser&#8221;. Podemos hacerlo bien por USB como lo haríamos con cualquier archivo, o bien con la utilidad adb push /ruta/de/origen /sdcard/destino<br />
En este caso, mis ficheros estaban en /tmp, así que:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>adb push <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>su-2.3.6.1-ef-signed.zip <span style="color: #000000; font-weight: bold;">/</span>sdcard<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>Flasheamos el recovery:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>fastboot flash recovery <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>recovery-clockwork-5.0.2.0-crespo.img</pre></div></div>

<p>Iniciamos el recovery en el menu de fastboot del telefono (tercera entrada, debajo de reboot), y una vez ahí, navegamos por los menús para instalar el su-2.3.6 que acabamos de dejar en el telefono.</p>
<p>Con esto, ya deberíamos poder arrancar el teléfono normalmente, y tendremos un hermoso nexus s rooteado, dispuesto para instalar el ROM Manager y empezar a instalar cosicas <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://diegosuarezgarcia.net/blog/?feed=rss2&#038;p=202</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>#RedResiste</title>
		<link>http://diegosuarezgarcia.net/blog/?p=198</link>
		<comments>http://diegosuarezgarcia.net/blog/?p=198#comments</comments>
		<pubDate>Fri, 02 Dec 2011 11:51:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sin categoría]]></category>

		<guid isPermaLink="false">http://diegosuarezgarcia.net/blog/?p=198</guid>
		<description><![CDATA[No puedo hacer otra cosa que sumarme. Asco de políticos, asco de sociedad, asco de &#8216;industria cultural&#8217;&#8230; Hace exactamente dos años tuvimos conocimiento del anteproyecto de Ley Sinde. Gran parte de la ciudadanía señaló de inmediato su rechazo en las redes a través del #manifiesto por los derechos fundamentales en Internet. En estos 24 meses [...]]]></description>
			<content:encoded><![CDATA[<p>No puedo hacer otra cosa que sumarme. Asco de políticos, asco de sociedad, asco de &#8216;industria cultural&#8217;&#8230;</p>
<hr/>
<div>
<p><span id="internal-source-marker_0.4764828330327333" style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;">Hace  exactamente dos años tuvimos conocimiento del anteproyecto de Ley  Sinde. Gran parte de la ciudadanía señaló de inmediato su rechazo en las  redes a través del #</span><a href="http://es.wikipedia.org/wiki/Manifiesto_%C2%ABEn_defensa_de_los_derechos_fundamentales_en_internet%C2%BB"><span style="background-color: transparent; color: #000099; font-family: Arial; font-size: 15px; vertical-align: baseline;">manifiesto por los derechos fundamentales en Internet</span></a><span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;">.  En estos 24 meses el debate social sobre esta iniciativa ha sido  intenso y ha aglutinado a ciudadanos y organizaciones preocupados por la  merma de derechos y libertades.  Ahora, pocos días después de haber  sido deslegitimado por las urnas, un gobierno moribundo pretende aprobar  el reglamento que desarrolla esta ley en abierta connivencia con el  gobierno entrante.</span><br />
<span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;"></span><br />
<span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;">La  Ley Sinde tendrá numerosos efectos indeseados: al introducir una fuerte  inseguridad jurídica en la regulación de Internet, se dificulta  gravemente la actividad de los emprendedores tecnológicos que el Partido  Popular pretende que contribuyan a reactivar la economía. La redacción  de la Ley Sinde señala claramente que se aplica a todos los servicios de  la sociedad de la información; no deben confundirnos los mensajes que  afirman que su única razón es la de cerrar webs de descargas. Nada es  peor para el crecimiento de un mercado que la inseguridad de no saber si  al día siguiente un negocio puede ser cerrado por la aplicación  arbitraria de una norma en manos del gobierno de turno.</span><br />
<span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;"></span><br />
<span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;">El  panorama de la propiedad intelectual en nuestro país es atroz: la  Embajada de los Estados Unidos ha impuesto la aprobación de la Ley  Sinde, el canon digital a empresas y administraciones fue declarado  ilegal por el Tribunal de Justicia de la Unión Europea pero tras año y  medio se sigue pagando, los antiguos dirigentes de la SGAE -siempre  defendida por el Ministerio de Cultura- se hallan imputados en la  Audiencia Nacional por el saqueo generalizado del dinero de los autores.  Este panorama cuadra con el general: una corrupción política extendida y  no censurada en las urnas, unida a la subordinación de la democracia  a  los intereses de unos pocos con nombres y apellidos a los que sin  embargo se les llama “mercados”.</span><br />
<span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;"></span><br />
<span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;">Sólo  con inteligencia, diálogo y trabajo se pueden resolver los actuales  retos de la propiedad intelectual y comenzar a construir una salida a la  preocupante situación económica actual. </span><a href="http://eur-lex.europa.eu/LexUriServ/LexUriServ.do?uri=CELEX:62010CJ0070:EN:HTML"><span style="background-color: transparent; color: #000099; font-family: Arial; font-size: 15px; vertical-align: baseline;">Como el Tribunal Europeo de Justicia</span></a><span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;">,  entendemos que la tensión entre la propiedad intelectual y  la libertad  de empresa, el derecho a la privacidad y el derecho a recibir o emitir  información ha de resolverse en favor de estos tres últimos derechos.  Será la única manera de crear riqueza y de mantener las libertades que  tanto ha costado conseguir.</span><br />
<span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;"></span><br />
<span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;">Frente  a la arbitrariedad, la defensa histórica de la ciudadanía ha consistido  en asegurar la garantía de los derechos fundamentales sustrayéndolos de  la política, esto es, de los poderes de la mayoría y del mercado: se  trata de derechos inviolables, indisponibles e inalienables.</span><br />
<span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;"></span><br />
<span style="background-color: transparent; font-family: Arial; font-size: 15px; text-decoration: none; vertical-align: baseline;">Los gobiernos van y vienen. La red resiste.</span></p>
<div style="clear: both;"></div>
</div>
<p><a href="http://redresiste.net/">http://redresiste.net/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://diegosuarezgarcia.net/blog/?feed=rss2&#038;p=198</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autenticación de ejabberd contra Redmine</title>
		<link>http://diegosuarezgarcia.net/blog/?p=195</link>
		<comments>http://diegosuarezgarcia.net/blog/?p=195#comments</comments>
		<pubDate>Sat, 05 Nov 2011 19:16:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sin categoría]]></category>

		<guid isPermaLink="false">http://diegosuarezgarcia.net/blog/?p=195</guid>
		<description><![CDATA[Una entradilla para quitarle algo de polvo al blog, que lo tengo abandonadísimo&#8230; En el curro hemos montado un pequeño servidor de mensajería instantánea con ejabberd. En cualquier sistema debian-based es trivial montar uno, puesto que viene por paquete y basta hacer un apt-get install. Lo que resulta más difícil, o al menos a mí [...]]]></description>
			<content:encoded><![CDATA[<p>Una entradilla para quitarle algo de polvo al blog, que lo tengo abandonadísimo&#8230;</p>
<p>En el curro hemos montado un pequeño servidor de mensajería instantánea con ejabberd. En cualquier sistema debian-based es trivial montar uno, puesto que viene por paquete y basta hacer un apt-get install. Lo que resulta más difícil, o al menos a mí me lo resultó, es que autentique a los usuarios con un método que no sea el propio (ni contra un LDAP, para eso hay un plugin maravilloso).</p>
<p>En nuestro caso, queríamos aprovechar los usuarios de nuestro tracker, Redmine, para que solo tuvieran acceso aquellos usuarios registrados en nuestra herramienta de incidencias. Esto forma parte de un plan más ambicioso, donde los shared rosters, de los que hablaré otro día, estuvieran configurados por diferentes proyectos y más cosas, pero en principio conseguir que el login se haga contra la base de datos de Redmine a mí me vale.</p>
<p>Para eso medio copié-medio adapté un script que me encontré en los foros de ejabberd, para hacer autenticación en python.<br />
Aquí os dejo el resultado, por si a alguien le sirve de algo (prometo contestar dudas en caso de que alguien las tenga, pero anuncio que mis conocimientos de erlang son nulos <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> )</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'logger'</span>
ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;RAILS_ENV&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>= <span style="color:#996600;">&quot;production&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'/opt/redmine/config/environment'</span>
&nbsp;
path = <span style="color:#996600;">&quot;/tmp/ejabberd-rubyauth.log&quot;</span>
file = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>path, <span style="color:#CC00FF; font-weight:bold;">File</span>::WRONLY <span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC00FF; font-weight:bold;">File</span>::APPEND <span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC00FF; font-weight:bold;">File</span>::CREAT<span style="color:#006600; font-weight:bold;">&#41;</span>
file.<span style="color:#9900CC;">sync</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
logger = <span style="color:#CC00FF; font-weight:bold;">Logger</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>file<span style="color:#006600; font-weight:bold;">&#41;</span>
logger.<span style="color:#9900CC;">level</span> = <span style="color:#CC00FF; font-weight:bold;">Logger</span>::DEBUG
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> auth<span style="color:#006600; font-weight:bold;">&#40;</span>username, password<span style="color:#006600; font-weight:bold;">&#41;</span>
    ret = User.<span style="color:#9900CC;">try_to_login</span><span style="color:#006600; font-weight:bold;">&#40;</span>username, password<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">if</span> ret
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> checkuser<span style="color:#006600; font-weight:bold;">&#40;</span>username<span style="color:#006600; font-weight:bold;">&#41;</span>
    ret = User.<span style="color:#9900CC;">find_by_login</span><span style="color:#006600; font-weight:bold;">&#40;</span>username<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">if</span> ret
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
<span style="color:#9966CC; font-weight:bold;">end</span>    
&nbsp;
logger.<span style="color:#9900CC;">info</span> <span style="color:#996600;">&quot;Starting ejabberd authentication service&quot;</span>
&nbsp;
<span style="color:#ff6633; font-weight:bold;">$stdout</span>.<span style="color:#9900CC;">sync</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#ff6633; font-weight:bold;">$stdin</span>.<span style="color:#9900CC;">sync</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#CC0066; font-weight:bold;">loop</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    <span style="color:#ff6633; font-weight:bold;">$stdin</span>.<span style="color:#9900CC;">eof</span>? <span style="color:#008000; font-style:italic;"># wait for input</span>
    msg = <span style="color:#ff6633; font-weight:bold;">$stdin</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    logger.<span style="color:#9900CC;">info</span> <span style="color:#996600;">&quot;2 bytes read.&quot;</span>
    length = msg.<span style="color:#9900CC;">unpack</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'n'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">first</span>
&nbsp;
    msg = <span style="color:#ff6633; font-weight:bold;">$stdin</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>length<span style="color:#006600; font-weight:bold;">&#41;</span>
    cmd, <span style="color:#006600; font-weight:bold;">*</span>data = msg.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    logger.<span style="color:#9900CC;">info</span> <span style="color:#996600;">&quot;Received message: &quot;</span><span style="color:#006600; font-weight:bold;">+</span>msg.<span style="color:#9900CC;">to_s</span>
    success = <span style="color:#9966CC; font-weight:bold;">case</span> cmd
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;auth&quot;</span>
      auth<span style="color:#006600; font-weight:bold;">&#40;</span>data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;isuser&quot;</span>
      checkuser<span style="color:#006600; font-weight:bold;">&#40;</span>data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>  
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#0000FF; font-weight:bold;">false</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    bool = success ? <span style="color:#006666;">1</span> : <span style="color:#006666;">0</span>
    <span style="color:#ff6633; font-weight:bold;">$stdout</span>.<span style="color:#9900CC;">write</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span>, bool<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">pack</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;nn&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
        logger.<span style="color:#9900CC;">info</span> <span style="color:#996600;">&quot;Error authenticating &quot;</span><span style="color:#006600; font-weight:bold;">+</span>e.<span style="color:#9900CC;">to_s</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Una vez lo tenemos, cambiamos en el archivo de config de ejabberd la línea que gestiona la autenticación:</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #109ab8;">&#123;</span>auth_method<span style="color: #6bb810;">,</span> external<span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">.</span>
  <span style="color: #109ab8;">&#123;</span>extauth_program<span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;/ruta/absoluta/al/script.rb&quot;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">.</span></pre></div></div>

<p>Reinicio del demonio del ejabberd, y a rodar <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://diegosuarezgarcia.net/blog/?feed=rss2&#038;p=195</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP en directorios de usuario con Apache y mod_userdir</title>
		<link>http://diegosuarezgarcia.net/blog/?p=191</link>
		<comments>http://diegosuarezgarcia.net/blog/?p=191#comments</comments>
		<pubDate>Thu, 04 Aug 2011 09:07:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sin categoría]]></category>

		<guid isPermaLink="false">http://diegosuarezgarcia.net/blog/?p=191</guid>
		<description><![CDATA[Siguiendo mi vieja regla de &#8220;si falla una vez, arréglalo, si falla dos y a la segunda no te acuerdas de la solución, ponlo en el blog&#8221;, entrada cortita de cómo solucionar que en los directorios de usuario (http://server/~usuario) no ejecute PHP por defecto. En la configuración de apache de mod_php, por alguna razón (de [...]]]></description>
			<content:encoded><![CDATA[<p>Siguiendo mi vieja regla de &#8220;si falla una vez, arréglalo, si falla dos y a la segunda no te acuerdas de la solución, ponlo en el blog&#8221;, entrada cortita de cómo solucionar que en los directorios de usuario (http://server/~usuario) no ejecute PHP por defecto.</p>
<p>En la configuración de apache de mod_php, por alguna razón (de seguridad, me supongo) hay una serie de líneas que se dedican a capar el uso de PHP en los directorios de usuario. Para poder usar PHP tenemos que comentarlas. </p>
<p>En una distribución basada en debian, el archivo es:</p>
<pre>/etc/apache2/mods-enabled/php5.conf</pre>
<p>Y las líneas a comentar, como bien detalla el archivo, son las siguientes:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#    &lt;IfModule mod_userdir.c&gt;</span>
<span style="color: #666666; font-style: italic;">#        &lt;Directory /home/*/public_html&gt;</span>
<span style="color: #666666; font-style: italic;">#            php_admin_value engine Off</span>
<span style="color: #666666; font-style: italic;">#        &lt;/Directory&gt;</span>
<span style="color: #666666; font-style: italic;">#    &lt;/IfModule&gt;</span></pre></div></div>

<p>Una vez comentadas, reiniciamos el servidor y listo <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://diegosuarezgarcia.net/blog/?feed=rss2&#038;p=191</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Redis, leaderboards, wordpress y otras chicas del montón</title>
		<link>http://diegosuarezgarcia.net/blog/?p=187</link>
		<comments>http://diegosuarezgarcia.net/blog/?p=187#comments</comments>
		<pubDate>Mon, 01 Aug 2011 21:22:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sin categoría]]></category>

		<guid isPermaLink="false">http://diegosuarezgarcia.net/blog/?p=187</guid>
		<description><![CDATA[Tras una temporadita de sequía bloguera correspondiente al verano y al angustiante calor de las tierras madrileñas, me he forzado a mí mismo a actualizar con el último experimento del curro. Para ponernos en antecedentes, imaginemos el -caso real- escenario en el que tenemos un blog de wordpress relativamente popular (150k usuarios únicos al día, [...]]]></description>
			<content:encoded><![CDATA[<p>Tras una temporadita de sequía bloguera correspondiente al verano y al angustiante calor de las tierras madrileñas, me he forzado a mí mismo a actualizar con el último experimento del curro.<br />
Para ponernos en antecedentes, imaginemos el -caso real- escenario en el que tenemos un blog de wordpress relativamente popular (150k usuarios únicos al día, un millón de hits diarios), y queremos tener estadísticas de los posts más visitados. Lo que todos haríamos, en principio, sería irnos a los plugins mejor puntuados de wordpress, y escoger el fantástico <a href="http://wordpress.org/extend/plugins/wordpress-popular-posts/">wordpress-popular-posts</a> de <a href="http://profiles.wordpress.org/users/Ikki24/">Héctor Cabrera</a>. Y nos quedaríamos tan anchos si no fuera por el pequeño problema de que como he dicho, es un blog popular, y cada visita significa un update en la tabla de la base de datos, lo que conduce a un cuello de botella inaceptable en nuestro caso. ¿Qué hacemos entonces?</p>
<h2>Enter Redis!!</h2>
<p>Para el que no lo sepa, <a href="http://redis.io/">Redis </a> es simplemente un almacén clave valor en memoria. Una especie de Hashmap, si queréis. Tú almacenas cosas por clave, y luego las buscas. Suena simple, y parecido a otras cosas como <a href="http://memcached.org/">memcached</a>, pero tiene la ventaja de que es persistente a disco, y además, es <a href="http://redis.io/commands">supersencillo </a>gracias a librerías como <a href="https://github.com/nicolasff/phpredis">phpredis.</a><br />
Ya que el problema está en el acceso a la base de datos, ¿por qué no modificar el plugin para que use Redis en lugar de SQL? Y allá que nos fuimos&#8230;<br />
La buena noticia es que funciona, y efectivamente, el aumento de rendimiento es más que notable. La mala es que no he tenido mucho tiempo, y el código está meramente funcional. Para dejarlo bonito habría que parametrizarlo más, integrarlo en el menú del plugin para que use Redis o SQL, hacer comprobaciones&#8230; Ahora mismo se asume que todo funciona -¡y sorprendentemente lo hace!- pero es un ejemplo de código a no-seguir, puesto que está cogido con pinzas. Dejo parte del código a modo de ejemplo, para ilustrar como funciona un ranking de posts más vistos. Las preguntas a los comentarios <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> RedisStorage implements PopularStorage<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">//Actualiza un determinado post en todas las tablas (diaria, semanal, mensual, total)</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$general</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mostviewed:&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$totaltable</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$general</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;all&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$monthtable</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$general</span> <span style="color: #339933;">.</span> <span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%Y%m&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$daytable</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$general</span> <span style="color: #339933;">.</span> <span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%Y%m<span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$weektable</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$general</span> <span style="color: #339933;">.</span> <span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%U-%Y&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$redis</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Redis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
        <span style="color: #666666; font-style: italic;">//Actualizamos las cuatro tablas</span>
        <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zIncrBy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$daytable</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zIncrBy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$monthtable</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zIncrBy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$totaltable</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zIncrBy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$weektable</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Cerramos la conexion</span>
        <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/*Sacamos los posts más populares*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPopular<span style="color: #009900;">&#40;</span><span style="color: #000088;">$instance</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$range</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$instance</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;range&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$general</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mostviewed:&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$range</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'all'</span><span style="color: #339933;">:</span>
               <span style="color: #000088;">$set</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$general</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;all&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'yesterday'</span><span style="color: #339933;">:</span>
               <span style="color: #000088;">$set</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$general</span><span style="color: #339933;">.</span><span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%Y%m<span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;yesterday&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'daily'</span><span style="color: #339933;">:</span>
                <span style="color: #000088;">$set</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$general</span><span style="color: #339933;">.</span><span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%Y%m<span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'weekly'</span><span style="color: #339933;">:</span>
                <span style="color: #000088;">$set</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$general</span><span style="color: #339933;">.</span><span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%U-%Y&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'monthly'</span><span style="color: #339933;">:</span>
                <span style="color: #000088;">$set</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$general</span><span style="color: #339933;">.</span><span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%Y%m&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">//daily</span>
                <span style="color: #000088;">$set</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$general</span><span style="color: #339933;">.</span><span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%Y%m<span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
           <span style="color: #009900;">&#125;</span>
          <span style="color: #666666; font-style: italic;">// Miramos con redis los top posts</span>
                <span style="color: #000088;">$redis</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Redis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$ids</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zRevRangeByScore</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$set</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;+inf&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'limit'</span><span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$instance</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;limit&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Omitimos por simplificar la parte de preparar la consulta SQL. Nos basta saber que en el array de ids tenemos los ids de Post que tenemos que sacar de la BBDD.</span>
                        <span style="color: #000088;">$mostpopular</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_results</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span>.ID, <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span>.post_title <span style="color: #006699; font-weight: bold;">$fields</span> FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> WHERE <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span>.post_status = 'publish' AND <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span>.post_password = '' <span style="color: #006699; font-weight: bold;">$idsin</span> <span style="color: #006699; font-weight: bold;">$exclude</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #990000;">usort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mostpopular</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ids</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                <span style="color: #000088;">$posA</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ids</span><span style="color: #339933;">,</span><span style="color: #000088;">$a</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                <span style="color: #000088;">$posB</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ids</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$posA</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$posB</span><span style="color: #339933;">;</span>
                            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$rank</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_reverse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mostpopular</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$instance</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'limit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rank</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ppost</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
           <span style="color: #000088;">$ppost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pageviews</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getViews</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ppost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$rank</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getViews<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$redis</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Redis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$views</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zScore</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mostviewed:all&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$views</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> deleteData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$clear</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$clear</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'cache'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                _e<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Success! The cache table has been cleared!'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wordpress-popular-posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//No tenemos cache, asi que siempre es un exito ;)</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$clear</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'all'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #000088;">$redis</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Redis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">flushDB</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              <span style="color: #000088;">$redis</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              _e<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Success! All data have been cleared!'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wordpress-popular-posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>    
         <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            _e<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid action.'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wordpress-popular-posts'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Como se puede ver, Redis es un sistema muy fácil de usar, y con una documentación excelente. Aquel que tenga más interés en el tema, puede dejarme un comentario o mandarme un correo. Preguntas facilitas, please, que no soy un experto ni mucho menos <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://diegosuarezgarcia.net/blog/?feed=rss2&#038;p=187</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>#notenemosmiedo</title>
		<link>http://diegosuarezgarcia.net/blog/?p=168</link>
		<comments>http://diegosuarezgarcia.net/blog/?p=168#comments</comments>
		<pubDate>Fri, 20 May 2011 17:01:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[#15m]]></category>
		<category><![CDATA[#acampadasol]]></category>
		<category><![CDATA[#botalos]]></category>
		<category><![CDATA[#nolesvotes]]></category>
		<category><![CDATA[#spanish revolution]]></category>
		<category><![CDATA[cambio social]]></category>
		<category><![CDATA[manifestacion]]></category>

		<guid isPermaLink="false">http://diegosuarezgarcia.net/blog/?p=168</guid>
		<description><![CDATA[A pesar de intentar ceñirme al apartado técnico siempre que puedo en el contenido del blog, no deja de ser un blog personal, y a veces apetece compartir con lo demás lo que piensas, aunque no seas tan bueno expresando tus pensamientos como pueden ser Escolar o David Bravo, o sin irnos tan lejos, Guti [...]]]></description>
			<content:encoded><![CDATA[<p>A pesar de intentar ceñirme al apartado técnico siempre que puedo en el contenido del blog, no deja de ser un blog personal, y a veces apetece compartir con lo demás lo que piensas, aunque no seas tan bueno expresando tus pensamientos como pueden ser <a href="http://www.escolar.net/">Escolar</a> o <a href="http://www.filmica.com/david_bravo/">David Bravo</a>, o sin irnos tan lejos, <a href="http://guticr.blogspot.com/">Guti</a> o <a href="http://diary.davidjbrenes.info/">Brenes</a>.</p>
<p>Y es que por primera vez en muchos, muchos años, tengo fe. Fe en la gente. Yo, que soy un escéptico, que veo la política y las revoluciones como Sam Vimes en <a href="http://libros.fnac.es/a349651/Terry-Pratchett-Ronda-de-noche">Ronda de noche</a> (libro por otra parte absolutamente recomendable), estoy anonadado. Asombrado. Viendo que efectivamente las revoluciones no son algo ajeno, de otro tiempo, que sólo puedes ver en las películas.</p>
<p>Porque lo que se ha vivido estos días en Plaza del Sol, en Madrid, supera cualquier cosa que haya visto en 26 años. He visto a toda la gente que normalmente está apoltronada, gente que cree que la política es algo que le pasa a otra gente, salir a la calle a manifestarse porque está harta de tanto mamoneo. De no pintar nada en sus propias vidas, por culpa de cuatro imbéciles que forman parte de un sistema de castas.</p>
<p><img alt="" src="http://dl.dropbox.com/u/6793393/IMG_20110519_010232.jpg" title="Ni la lluvia pudo echarlos, mucho menos la policía" class="alignright" width="420" height="280" />-He visto a los medios entre confusos y asustados, cometiendo en un error de bulto tremendo calculando las proporciones de este movimiento en cobertura informativa. Descolocados, sin saber como reaccionar y como cubrir un caso que no se había producido en España en muchos, muchos años.<br />
-He visto a los políticos aún más confusos. Algunos han intentado aprovechar la oportunidad y sacar ventaja de la situación, porque es lo único que saben hacer. Otros intentando minimizar la situación o hacerle oídos sordos. Alguno totalmente fuera de bolos, como <a href="http://www.que.es/ultimas-noticias/espana/201105191504-cesar-vidal-relaciona-manifestantes-cont.html">César Vidal</a>, <a href="http://www.libertaddigital.com/opinion/juan-morote/cria-cuervos-y-59756/">Juan Morote</a>, o similares, atacando abiertamente a un movimiento del que no entienden absolutamente nada, por mucho que lo crean, porque está fuera de los límites de la manera de ver el mundo de estos señores. O porque están zumbados, sin más. Pero el punto común es que se están dando cuenta de que no tienen el control, y que la vergonzosa presión sobre los <em>miedos</em> de comunicación no está funcionando esta vez, porque la gente está harta de mentiras y ha decidido informarse ella misma a través de las redes sociales (hasta yo he empezado a usar twitter)</p>
<p>El ambiente que se respira estos días en Sol es indescriptible. Es una especie de burbuja de sueños, de ilusión, de gente convencida de que realmente esto es el comienzo de otra cosa. Todo el mundo colaborando, con un buen rollo envidiable, y sin liarla. Vecinos colaborando. Empresas y bares aportando lo que pueden. ¡¡Trabajadores del circo del sol trayendo palomitas!!<br />
El tiempo nos dirá si ha servido de algo, pero para mí, la mera existencia de un movimiento como este simboliza un cambio. Un toque de atención a la clase política. &#8220;Eh, vosotros, estamos hartos, y sabemos que la mejor forma de haceros daño es en las urnas. Empezad a vernos como iguales a los que respetar, y no como a una manada de borregos que solamente simbolizan votos&#8221;.</p>
<!-- ProPlayer by Isa Goksu --><div name="mediaspace" id="mediaspace"><div class="pro-player-container" width="530px" height="253px"><div id="pro-player-168pp-single-4fb85d472bc79"></div></div></div><script type="text/javascript" charset="utf-8">var flashvars = {width: "530",height: "253",autostart: "false",repeat: "false",backcolor: "111111",frontcolor: "cccccc",lightcolor: "66cc00",stretching: "fill",enablejs: "true",mute: "false",skin: "http://diegosuarezgarcia.net/blog/wp-content/plugins/proplayer/players/skins/default.swf",logo: "http://diegosuarezgarcia.net/blog/wp-content/plugins/proplayer/players/watermark.png",image: "http://dl.dropbox.com/u/6793393/IMG_20110518_212409.jpg",plugins: "",javascriptid: "168pp-single-4fb85d472bc79",image: "http://dl.dropbox.com/u/6793393/IMG_20110518_212409.jpg",file: 'http://diegosuarezgarcia.net/blog/wp-content/plugins/proplayer/playlist-controller.php?pp_playlist_id=168pp-single-4fb85d472bc79&sid=1337482567'};var params = {wmode: "transparent",allowfullscreen: "true",allowscriptaccess: "always",allownetworking: "all"};var attributes = {id: "obj-pro-player-168pp-single-4fb85d472bc79",name: "obj-pro-player-168pp-single-4fb85d472bc79"};swfobject.embedSWF("http://diegosuarezgarcia.net/blog/wp-content/plugins/proplayer/players/player.swf", "pro-player-168pp-single-4fb85d472bc79", "530", "253", "9.0.0", false, flashvars, params, attributes);</script>
<p>Creo que el vídeo habla por sí solo. Puede que no sea mayo del 68, pero no es una manifestación de cuatro antisistema (como si oponerse al sistema actual fuese algo malo)</p>
<p><img class="alignleft" title="Listas de lo que se necesitaba (o no se necesitaba)" src="http://dl.dropbox.com/u/6793393/IMG_20110519_211227.jpg" alt="Listas de lo que se necesitaba (o no se necesitaba)" width="300" height="240" />Y a pesar de que algunos medios sensacionalistas opten por decir que no son más que cuatro hippies perroflautas, ayer fui testigo de un despliegue y una organización que deja con el culo al aire a todos los &#8220;profesionales&#8221;. En menos de un día, obviamente con la colaboración ciudadana, habían sacado un &#8220;pueblo&#8221; de la nada. Comisiones de todos los servicios básicos que se necesitaban. Comunicación, infraestructura, alimentación, información. ¡Enfermería! ¡Listas priorizadas y actualizadas en todo momento de lo que hacía falta!</p>
<p>Para ser &#8220;cuatro vagos y parásitos sociales&#8221;, han montado la de Dios ellos solos. Y todo voluntariamente y sin oler un duro. Simplemente por mera camaradería y solidaridad con una situación que es límite no por su gravedad a corto plazo, sino por lo prolongada. El vaso está lleno, y no le caben más gotas.</p>
<p style="text-align: center;"><em><strong>Hoy, no sé lo que durará, pero creo que otro sistema es posible. Porque en #acampadasol, por encima de todo #notenemosmiedo. Y vosotros, políticos, banqueros, y gente que vive bien gracias al hecho de pisotearnos día a día, sabed una cosa. <br/><br />
Deberíais empezar a tenerlo.</strong></em><br />
<img class="aligncenter" src="http://dl.dropbox.com/u/6793393/IMG_20110519_220229.jpg" alt="" width="640" height="400" /></p>
]]></content:encoded>
			<wfw:commentRss>http://diegosuarezgarcia.net/blog/?feed=rss2&#038;p=168</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://dl.dropbox.com/u/6793393/VID_20110519_215628.3gp" length="9221752" type="video/3gpp" />
		</item>
		<item>
		<title>Hoy en trucos de magia: las variables con OR</title>
		<link>http://diegosuarezgarcia.net/blog/?p=163</link>
		<comments>http://diegosuarezgarcia.net/blog/?p=163#comments</comments>
		<pubDate>Sun, 03 Apr 2011 19:06:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[black magic]]></category>
		<category><![CDATA[boolean]]></category>
		<category><![CDATA[operator]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://diegosuarezgarcia.net/blog/?p=163</guid>
		<description><![CDATA[Pues aunque a priori pensé que la primera entrada que publicaría después de venirme a Madrid sería del curro, no lo va a ser. No es que me falten posibilidades porque ya ha habido varios problemas resueltos &#8220;blogables&#8221;, pero bien por pereza o por estar ocupado en ese momento no lo escribo en el blog, [...]]]></description>
			<content:encoded><![CDATA[<p>Pues aunque a priori pensé que la primera entrada que publicaría después de venirme a Madrid sería del curro, no lo va a ser. No es que me falten posibilidades porque ya ha habido varios problemas resueltos &#8220;blogables&#8221;, pero bien por pereza o por estar ocupado en ese momento no lo escribo en el blog, y luego ya es muy tarde <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Este fin de semana, que he decidido darme un descanso y estar tranquilamente en casa, me he puesto a hacer un pequeño scrapper en <a href="http://code.google.com/p/phpquery/">PHPQuery</a> para magiccards.info, de cara a hacer alguna chorriaplicación al respecto en android y sobre todo para no oxidarme.</p>
<p>Al lío, el caso es que en un momento se me presenta el dilema de como almacenar en un objeto una lista de opciones sí/no. El primer pensamiento es usar 4 variables booleanas, que descarto casi inmediatamente por lo inescalable. ¿Un hash quizás? ¿Un array? No me convencían, y entonces me acordé de la técnica arcana de los operadores bitwise, que utilicé para alguna receta vudú en aquellos maravillosos años de carrera. Como almacenar toda la lista en un solo entero. Pasen y vean, señores.</p>
<p>Lo primero, definimos constantes:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LEGAL_VINTAGE'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LEGAL_LEGACY'</span><span style="color: #339933;">,</span>  LEGAL_VINTAGE<span style="color: #339933;">&lt;&lt;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LEGAL_EXTENDED'</span><span style="color: #339933;">,</span>LEGAL_VINTAGE<span style="color: #339933;">&lt;&lt;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LEGAL_STANDARD'</span><span style="color: #339933;">,</span>LEGAL_VINTAGE<span style="color: #339933;">&lt;&lt;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LEGAL_CLASSIC'</span><span style="color: #339933;">,</span> LEGAL_VINTAGE<span style="color: #339933;">&lt;&lt;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Al desplazar un bit cada variable, los valores serian potencias de dos: 1,2,4,8 y 16, en este caso.<br />
Lo siguiente es construir el entero donde almacenaremos la lista de opciones</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$int_legality</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/vintage/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$legality</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$int_legality</span> <span style="color: #339933;">|=</span> LEGAL_VINTAGE<span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/classic/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$legality</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$int_legality</span> <span style="color: #339933;">|=</span> LEGAL_CLASSIC<span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/standard/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$legality</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$int_legality</span> <span style="color: #339933;">|=</span> LEGAL_STANDARD<span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/extended/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$legality</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$int_legality</span> <span style="color: #339933;">|=</span> LEGAL_EXTENDED<span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/legacy/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$legality</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$int_legality</span> <span style="color: #339933;">|=</span> LEGAL_LEGACY<span style="color: #339933;">;</span></pre></div></div>

<p>Aqui inicializamos la variable a 0, y vamos preguntando qué opciones de la lista tenemos que marcar.La función preg_match devuelve un booleano si el patron encaja con la cadena que le pasamos, aunque para el ejemplo no viene al caso <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
El operador |= le sonará a chino a mucha gente, pero es muy sencillo. Igual que a +=b no es más que a = a+b, a|=b no hace otra cosa que a = a|b, que es hacer un OR lógico consigo mismo y el rvalue que pasamos.<br />
Y aquí está la magia. Como cada variable no es más que un 1 desplazado lógicamente respecto a la anterior, no se solapan, y lo que tenemos es un montón de bits actuando de interruptores para las variables que necesitemos. Un montón de booleanos metidos en un entero <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>¿Y como lo traducimos de nuevo cuando lo necesitemos a lenguaje humano? Pues igual de fácil, aprovechándonos de la aritmética booleana:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">legality</span> <span style="color: #339933;">&amp;</span> LEGAL_VINTAGE<span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> LEGAL_VINTAGE<span style="color: #009900;">&#41;</span>   <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;V&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">legality</span> <span style="color: #339933;">&amp;</span> LEGAL_LEGACY<span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> LEGAL_LEGACY<span style="color: #009900;">&#41;</span>     <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;L&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">legality</span> <span style="color: #339933;">&amp;</span> LEGAL_STANDARD<span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> LEGAL_STANDARD<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;S&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">legality</span> <span style="color: #339933;">&amp;</span> LEGAL_EXTENDED<span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> LEGAL_EXTENDED<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;X&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">legality</span> <span style="color: #339933;">&amp;</span> LEGAL_CLASSIC<span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> LEGAL_CLASSIC<span style="color: #009900;">&#41;</span>   <span style="color: #000088;">$str</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">&quot;C&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>No hacemos otra cosa que comprobar si nuestro interruptor de bit está activado, haciendo un AND del &#8220;almacén&#8221; con el que estamos buscando. Por aritmética de boole, si el bit que buscamos está a 1, no importa el estado del resto, puesto que nuestra constante es toda 0s salvo el bit que buscamos, por lo que se cumplirá la igualdad.</p>
<p>Espero que se haya entendido algo, porque es un truco muy chulo y que permite optimizar tanto la eficiencia como la lectura y mantenibilidad del código una vez se comprende.<br />
¿Algún otro truco que queráis compartir? ¡A los comentarios! <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://diegosuarezgarcia.net/blog/?feed=rss2&#038;p=163</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>No he muerto</title>
		<link>http://diegosuarezgarcia.net/blog/?p=159</link>
		<comments>http://diegosuarezgarcia.net/blog/?p=159#comments</comments>
		<pubDate>Fri, 04 Mar 2011 07:39:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sin categoría]]></category>
		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://diegosuarezgarcia.net/blog/?p=159</guid>
		<description><![CDATA[A decir por el ritmo de actualización del blog podría parecer que sí, pero todavía sigo vivito y coleando. Han sido unos meses de parón en general, que han servido para ajustarse bien las correas y coger impulso. Ahora, nueva casa en nuevo lugar, nuevo trabajo, nueva vida. Muchos retos por delante, profesionales y personales. [...]]]></description>
			<content:encoded><![CDATA[<p>A decir por el ritmo de actualización del blog podría parecer que sí, pero todavía sigo vivito y coleando. Han sido unos meses de parón en general, que han servido para ajustarse bien las correas y coger impulso.<br />
Ahora, nueva casa en nuevo lugar, nuevo trabajo, nueva vida. Muchos retos por delante, profesionales y personales. Seguiremos informando <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://diegosuarezgarcia.net/blog/?feed=rss2&#038;p=159</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cambiar imagen de arranque (splash) de Vodafone en HTC Magic</title>
		<link>http://diegosuarezgarcia.net/blog/?p=148</link>
		<comments>http://diegosuarezgarcia.net/blog/?p=148#comments</comments>
		<pubDate>Wed, 27 Oct 2010 10:03:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[fastboot]]></category>
		<category><![CDATA[htc magic]]></category>
		<category><![CDATA[splash image]]></category>
		<category><![CDATA[vodafone]]></category>

		<guid isPermaLink="false">http://www.diegosuarezgarcia.dreamhosters.com/blog/?p=148</guid>
		<description><![CDATA[No he estado en Vodafone nunca. Sin embargo, al comprarme la HTC Magic, pese a ser libre, mantenía el splash de Vodafone, y seamos serios, aparte de ser feo, es confuso si además estás en simyo como yo Así que ayer, aprovechando que cambié la ROM por una SuperFroyo 6.6.2 -que por cierto, va como [...]]]></description>
			<content:encoded><![CDATA[<p>No he estado en Vodafone nunca. Sin embargo, al comprarme la HTC Magic, pese a ser libre, mantenía el splash de Vodafone, y seamos serios, aparte de ser feo, es confuso si además estás en simyo como yo <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Así que ayer, aprovechando que cambié la ROM por una <a href="http://www.htcmania.com/showthread.php?t=140354">SuperFroyo 6.6.2</a> -que por cierto, va como la seda, MUY recomendable-, me decidí a buscar cómo demonios cambiar el splash.<br />
Los requisitos son los siguientes:</p>
<ul>
<li>Tener el SDK de Android instalado, con el driver para nuestro teléfono configurado.</li>
<li>Tener en el teléfono un SPL 1.33 (yo tengo el 1.33.2004)</li>
</ul>
<p>Para cambiarlo seguimos los siguientes pasos.</p>
<ol>
<li>Enchufamos el teléfono al PC</li>
<li>Reiniciamos el teléfono en modo fastboot (apagamos y lo arrancamos pulsando atrás+power)</li>
<li>Nos saldrá algo como esto: <br /><img src="http://www.geekyplanet.com/wp-content/uploads/2010/04/0907_android_workshop_handy_05.jpg" alt="imagen de fastboot de android" /></li>
<li>En nuestro PC, nos vamos al directorio tools del SDK de android y abrimos ahí una consola de texto.</li>
<li>Tecleamos &#8220;fastboot devices&#8221;. Si nos sale una entrada en la lista, el teléfono está siendo reconocido, si no, algo ha fallado <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
<li>Buscamos una imagen que poner. Tiene que estar en un formato determinado, <a href="http://ostebaronen.dk/android/splash/index.php">en este listado hay unas cuantas</a>. Los png o JPG son la imagen tal y como queda, pero la que tenemos que usar es la que acaba en 565. Si no nos gusta ninguna, siempre <a href="http://www.megaupload.com/?d=BSLZ39XT">podemos hacernos una. (password androidsocket)</a> <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </li>
<li>Escribimos lo siguiente: fastboot flash splash /ruta/a/mi_imagen.raw565</li>
</ol>
<p>Y listos. Reiniciamos, y si todo ha ido bien, ya podemos ver que vodafone ha desaparecido de nuestras vidas <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /><br />
<a href="http://www.diegosuarezgarcia.dreamhosters.com/blog/wp-content/uploads/2010/10/Picture-1.jpg"><img src="http://www.diegosuarezgarcia.dreamhosters.com/blog/wp-content/uploads/2010/10/Picture-1-164x300.jpg" alt="Splash de mi HTC magic" title="dontpanic" width="164" height="300" class="alignnone size-medium wp-image-149" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://diegosuarezgarcia.net/blog/?feed=rss2&#038;p=148</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>¿Qué echan en el cine? (II)</title>
		<link>http://diegosuarezgarcia.net/blog/?p=140</link>
		<comments>http://diegosuarezgarcia.net/blog/?p=140#comments</comments>
		<pubDate>Tue, 19 Oct 2010 10:03:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sin categoría]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.diegosuarezgarcia.dreamhosters.com/blog/?p=140</guid>
		<description><![CDATA[En la anterior entrega hicimos un pequeño scraping de Google Movies que nos daba los datos de la cartelera al invocar el programa. Hecho esto, debemos montar un pequeño servicio web para poder consumirlo desde cualquier cliente. En principio la forma ideal de hacerlo sería montar una pequeña aplicación de Rails y APIficarla un poco, [...]]]></description>
			<content:encoded><![CDATA[<p>En la anterior entrega hicimos <a href="http://www.diegosuarezgarcia.dreamhosters.com/blog/2010/10/%C2%BFque-echan-en-el-cine-i/">un pequeño scraping de Google Movies</a> que nos daba los datos de la cartelera al invocar el programa. Hecho esto, debemos montar un pequeño servicio web para poder consumirlo desde cualquier cliente. En principio la forma ideal de hacerlo sería montar una pequeña aplicación de Rails y APIficarla un poco, pero como estamos haciendo un pequeño ejemplo vamos a hacer algo más sencillo: simplemente vamos a cachear la cartelera en una dirección fija.<br />
Esto tiene otro motivo: no sé cual es la política de google respecto al scraping, y prefiero hacer las menos peticiones posibles, no sea que se mosqueen <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_razz.gif' alt=':-P' class='wp-smiley' />  Metemos un trabajo de cron para que se ejecute todos los días el scraper de cartelera y lo deje en una dirección fija, y listo.</p>
<p>Y una vez tenemos el servicio, hay que consumirlo desde android. No voy a explicar cómo montar el entorno de desarrollo porque ya está magníficamente explicado en <a href="http://developer.android.com/sdk/index.html">otros sitios</a>, así que supondré que está montado ya y me iré al código.</p>
<p>Lo primero es el manifest.xml, donde daremos los datos generales de la app.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;manifest</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">     <span style="color: #000066;">package</span>=<span style="color: #ff0000;">&quot;org.me.queechanenelcinedemo&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;activity</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.MainActivity&quot;</span></span>
<span style="color: #009900;">                     <span style="color: #000066;">android:icon</span>=<span style="color: #ff0000;">&quot;@drawable/icon&quot;</span></span>
<span style="color: #009900;">                     <span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;¿Qué echan en el cine?&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.intent.action.MAIN&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.intent.category.LAUNCHER&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/activity<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;android.permission.INTERNET&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/uses-permission<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/manifest<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Con este manifiesto nos limitamos a decir el nombre de la aplicación (android:label=&#8221;¿Qué echan en el cine?&#8221;), ponerle un icono que está en la carpeta res/drawable y se llama icon(.gif), el nombre del paquete Java donde está el código (org.me.queechanenelcinedemo) y el nombre de la actividad principal (el main, vaya), que es MainActivity. También añadimos, importante, que necesita permiso para conectarse a la red, puesto que si no no podríamos conectar con nuestro servidor de pasarela con la cartelera de cine.</p>
<p>Para modelar los cines y las pelis, hacemos un par de POJOs. No tienen ninguna ciencia, porque son código Java normal y corriente:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.me.androidapplication1.model</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Cine <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">List</span>
 pelis<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> nombre<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Cine<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nombre<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nombre</span> <span style="color: #339933;">=</span> nombre<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pelis</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span>
 getPelis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> pelis<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getNombre<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> nombre<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setNombre<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nombre<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nombre</span> <span style="color: #339933;">=</span> nombre<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addPeli<span style="color: #009900;">&#40;</span>Peli peli<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">pelis</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>peli<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        StringBuilder sb <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span>nombre<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>Peli p <span style="color: #339933;">:</span> pelis<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            sb.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> sb.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.me.androidapplication1.model</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Peli <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> titulo<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> info<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> horario<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> puntuacion<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Peli<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> titulo, <span style="color: #003399;">String</span> info, <span style="color: #003399;">String</span> horario, <span style="color: #003399;">String</span> puntuacion<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">titulo</span> <span style="color: #339933;">=</span> titulo<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">info</span> <span style="color: #339933;">=</span> info<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">horario</span> <span style="color: #339933;">=</span> horario<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">puntuacion</span> <span style="color: #339933;">=</span> puntuacion<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getHorario<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> horario<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setHorario<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> horario<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">horario</span> <span style="color: #339933;">=</span> horario<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> info<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setInfo<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> info<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">info</span> <span style="color: #339933;">=</span> info<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getPuntuacion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> puntuacion<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPuntuacion<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> puntuacion<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">puntuacion</span> <span style="color: #339933;">=</span> puntuacion<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getTitulo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> titulo<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTitulo<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> titulo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">titulo</span> <span style="color: #339933;">=</span> titulo<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> titulo<span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">+</span> horario<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Y con eso, solo nos queda la clase con la actividad principal. <strong><span style="text-decoration: underline;">NO</span></strong> es un ejemplo de buenas prácticas. No hay gestión de errores alguna, y construyo la interfaz programáticamente en vez de hacerlo mediante recursos XML, que es como debe de hacerse. Espero me perdonen por haberlo hecho así para mayor claridad del ejemplo <img src='http://diegosuarezgarcia.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
El código está comentado, si alguien tiene alguna duda que me deje un comentario ^^</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">org.me.queechanenelcinedemo</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.graphics.Color</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.HttpClient</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.client.methods.HttpGet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.impl.client.DefaultHttpClient</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.http.util.EntityUtils</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.me.androidapplication1.model.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MainActivity <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle icicle<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>icicle<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">String</span> jsonData <span style="color: #339933;">=</span> retrieveData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            List<span style="color: #339933;">&lt;</span>cine<span style="color: #339933;">&gt;</span> cines <span style="color: #339933;">=</span> parseData<span style="color: #009900;">&#40;</span>jsonData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            initInterface<span style="color: #009900;">&#40;</span>cines<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">//Rellenar con gestión de errores, por favor! :P</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Intentamos conectarnos al servidor de pasarela para que nos de los datos
     * en JSON.
     * @return Lista de peliculas en JSON.
     * @throws IOException Si ocurre algun problema en la conexión
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> retrieveData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> retorno <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
        HttpClient client <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultHttpClient<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        HttpGet get <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HttpGet<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://diegosuarezgarcia.net/pruebas/pelis.js&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        HttpResponse responseGet <span style="color: #339933;">=</span> client.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>get<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        HttpEntity resEntityGet <span style="color: #339933;">=</span> responseGet.<span style="color: #006633;">getEntity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>resEntityGet <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            retorno <span style="color: #339933;">=</span> EntityUtils.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>resEntityGet<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> retorno<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Cogemos un string en JSON que contenga la lista de cines, y la parseamos
     * de manera que devolvamos una lista con todos los objetos Cine, cada uno
     * de los cuales contendrá sus películas. La API de android para JSON es
     * francamente simple, mejor de lo que me esperaba :-)
     * @param jsonData El string serializado en JSON
     * @return La lista de cines contenida en jsonData
     * @throws JSONException Si ocurre algún error.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>cine<span style="color: #339933;">&gt;</span> parseData<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> jsonData<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JSONException <span style="color: #009900;">&#123;</span>
        JSONArray cinesJson <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONArray<span style="color: #009900;">&#40;</span>jsonData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        List<span style="color: #339933;">&lt;</span>cine<span style="color: #339933;">&gt;</span> cines <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>cine<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> cinesJson.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Cine cine <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Cine<span style="color: #009900;">&#40;</span>cinesJson.<span style="color: #006633;">getJSONObject</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nombre&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            JSONArray pelisJson <span style="color: #339933;">=</span> cinesJson.<span style="color: #006633;">getJSONObject</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getJSONArray</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pelis&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> pelisJson.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                JSONObject p <span style="color: #339933;">=</span> pelisJson.<span style="color: #006633;">getJSONObject</span><span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                Peli aux <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Peli<span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;titulo&quot;</span><span style="color: #009900;">&#41;</span>, p.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;info&quot;</span><span style="color: #009900;">&#41;</span>,
                        p.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;horario&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                cine.<span style="color: #006633;">addPeli</span><span style="color: #009900;">&#40;</span>aux<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            cines.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>cine<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> cines<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * En este metodo inicializamos los componentes gráficos que formarán la
     * aplicación.
     * El esquema es muy sencillo: Establecemos un Layout vertical para que
     * todos los componentes se apilen unos encima de otros. Como por defecto
     * android no da scroll cuando los componentes desbordan la pantalla (no
     * entiendo muy bien por qué), incluimos un scrollview, que a su vez solo
     * puede tener un hijo, luego metemos otro Layout, que será donde esté
     * todo el contenido. El contenido no es más que un montón de etiquetas de
     * texto (TextViews), a las que le damos un formato para distinguir los
     * cines de las películas y darle una cierta apariencia de &quot;tabla&quot;.
     * @param cines La lista de objetos cine que presentaremos.
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> initInterface<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>cine<span style="color: #339933;">&gt;</span> cines<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        LinearLayout root <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinearLayout<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        root.<span style="color: #006633;">setOrientation</span><span style="color: #009900;">&#40;</span>LinearLayout.<span style="color: #006633;">VERTICAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ScrollView scrollPanel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ScrollView<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        root.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>scrollPanel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        LinearLayout contenido <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinearLayout<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        contenido.<span style="color: #006633;">setOrientation</span><span style="color: #009900;">&#40;</span>LinearLayout.<span style="color: #006633;">VERTICAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        scrollPanel.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>contenido<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Cine cine <span style="color: #339933;">:</span> cines<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            TextView tv <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextView<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tv.<span style="color: #006633;">setTextColor</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">BLUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tv.<span style="color: #006633;">setBackgroundColor</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">GRAY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tv.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>cine.<span style="color: #006633;">getNombre</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            contenido.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>tv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Peli peli <span style="color: #339933;">:</span> cine.<span style="color: #006633;">getPelis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                TextView tv2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextView<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                tv2.<span style="color: #006633;">setPadding</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                tv2.<span style="color: #006633;">setText</span><span style="color: #009900;">&#40;</span>peli.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                contenido.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>tv2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">setContentView</span><span style="color: #009900;">&#40;</span>root<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Y el resultado final&#8230;<br />
<a href="http://www.diegosuarezgarcia.dreamhosters.com/blog/wp-content/uploads/2010/10/snapshot2.png"><img src="http://www.diegosuarezgarcia.dreamhosters.com/blog/wp-content/uploads/2010/10/snapshot2-300x212.png" alt="" title="snapshot2" width="300" height="212" class="alignnone size-medium wp-image-146" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://diegosuarezgarcia.net/blog/?feed=rss2&#038;p=140</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

