<?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>Destaqueblog &#187; python</title>
	<atom:link href="http://weblog.destaquenet.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://weblog.destaquenet.com</link>
	<description>Blog da equipe Destaquenet.</description>
	<lastBuildDate>Sun, 15 Aug 2010 03:14:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
		<item>
		<title>Django Para Dispositivos Móveis</title>
		<link>http://weblog.destaquenet.com/2010/07/12/django-para-dispositivos-moveis/</link>
		<comments>http://weblog.destaquenet.com/2010/07/12/django-para-dispositivos-moveis/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 11:10:21 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[Português]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[Tutoriais]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[configuração]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[dispositivo]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[móvel]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[wsgi]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=944</guid>
		<description><![CDATA[Estamos vivendo o que alguns chamam de &#8220;explosão mobile&#8221;, um tempo onde um número cada vez maior de dispositivos móveis &#8212; como tablets e smartphones &#8212; se tornam parte significante da Web. Esta é a razão pela qual ter seu &#8230; <a href="http://weblog.destaquenet.com/2010/07/12/django-para-dispositivos-moveis/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Estamos vivendo o que alguns chamam de &#8220;explosão mobile&#8221;, um tempo onde um número cada vez maior de dispositivos móveis &#8212; como tablets e smartphones &#8212; se tornam parte significante da Web. Esta é a razão pela qual ter seu website preparado para tais dispositivos é cada vez mais importante.</p>
<p>Felizmente, <a href="http://djangoproject.com/">Django</a> é um dos poucos frameworks web que tornam este um problema simples de solucionar.</p>
<p><span id="more-944"></span></p>
<h3>Como resolvemos este problema</h3>
<p>Um jeito simples é criar um módulo <code>settings.py</code> separado para a versão &#8220;mobile&#8221;. Isso nos permite rodar dois sites diferentes reusando a mesma base de código. Além disso, cada versão pode ter seu próprio diretório de templates, configurações de URL, etc.</p>
<p>Vamos fazer isso. No diretório-raíz do projeto, crie um arquivo <code>m_settings.py</code> com o seguinte conteúdo:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;"># Arquivo: myproject/m_settings.py</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;Configurações para a versão &quot;mobile&quot; do website.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Carrega as configurações padrão</span>
<span style="color: #ff7700;font-weight:bold;">from</span> settings <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Domínio &quot;m.mydomain.com&quot;</span>
SITE_ID = <span style="color: #ff4500;">2</span>
PREPEND_WWW = <span style="color: #008000;">False</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Usado em outras partes do código para determinar qual versão está rodando</span>
DEFAULT_VERSION = <span style="color: #008000;">False</span>
MOBILE_VERSION  = <span style="color: #008000;">True</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># URLs para a versão mobile</span>
ROOT_URLCONF  = <span style="color: #483d8b;">'myproject.m_urls'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Templates para a versão mobile</span>
TEMPLATE_DIRS = <span style="color: black;">&#40;</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">normpath</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'/template/mobile'</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Outras configurações...</span></pre></div></div>

<p>O fato de os dois sites compartilharem a mesma base de código não significa que eles devem ter as mesmas configurações de URL. Aliás, o normal é que versões próprias para dispositivos móveis sejam mais simples, com uma quantidade reduzida de mapeamentos de URLs.</p>
<p>Para definir as configurações de URL da versão mobile, crie um arquivo <code>m_urls.py</code> com o seguinte conteúdo:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;"># Arquivo: myproject/m_urls.py</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;Mapeamentos de URL para a versão &quot;mobile&quot;.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span>.<span style="color: black;">urls</span>.<span style="color: black;">defaults</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #808080; font-style: italic;"># from django.contrib import admin</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># admin.autodiscover()</span>
&nbsp;
handler404 = <span style="color: #483d8b;">'website.views.error_404_handler'</span>
handler500 = <span style="color: #483d8b;">'website.views.error_500_handler'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># URLs</span>
urlpatterns = patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>,
    <span style="color: #808080; font-style: italic;"># I18N</span>
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^i18n/'</span>, include<span style="color: black;">&#40;</span><span style="color: #483d8b;">'django.conf.urls.i18n'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
&nbsp;
    <span style="color: #808080; font-style: italic;"># Apps internas</span>
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^contact/'</span>, include<span style="color: black;">&#40;</span><span style="color: #483d8b;">'contact.urls'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
&nbsp;
    <span style="color: #808080; font-style: italic;"># (r'^admin/doc/', include('django.contrib.admindocs.urls')),</span>
    <span style="color: #808080; font-style: italic;"># (r'^admin/', include(admin.site.urls)),</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Outras configurações...</span>
<span style="color: black;">&#41;</span></pre></div></div>

<p>Perceba que, neste exemplo em particular, a <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/">app Admin</a> não é acessível através da versão mobile. Isso apenas serve para mostrar que temos total liberdade para estruturar ambas as versões de diferentes formas, sem muitas dificuldades.</p>
<h3>Desenvolvendo</h3>
<p>Você certamente já sabe como rodar o servidor de desenvolvimento para a versão padrão. O que a maioria dos desenvolvedores desconhece, entretanto, é que é possível escolher qual a porta e/ou módulo <code>settings.py</code> deve ser usado pelo script, o que nos permite rodar um servidor para cada versão do nosso site:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #666666; font-style: italic;"># Terminal 1: versão padrão</span>
$ python manage.py runserver <span style="color: #000000;">8000</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Terminal 2: versão mobile</span>
$ python manage.py runserver <span style="color: #660033;">--settings</span>=m_settings <span style="color: #000000;">8001</span></pre></div></div>

<p>Pronto, agora é só continuar com o desenvolvimento da forma que você já está acostumado.</p>
<h3>Testando</h3>
<p>Você provavelmente notou que temos duas configurações que podem ser usadas para determinar qual versão do site está rodando, <code>DEFAULT_VERSION</code> e <code>MOBILE_VERSION</code>. </p>
<p>Por isso, é só uma questão de checar tais configurações quando for necessário fazer coisas diferentes em cada uma das versões do site. Isso é particularmente útil quando você tem uma app usada em ambas as versões e quer criar testes para cada uma das versões:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;"># Arquivo: myproject/myapp/tests/__init__.py</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;Testes para MyApp.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>settings, <span style="color: #483d8b;">'DEFAULT_VERSION'</span>, <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Testa a versão padrão</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> default_models_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> default_navigation_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>settings, <span style="color: #483d8b;">'MOBILE_VERSION'</span>, <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Testa a versão mobile</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> mobile_models_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> mobile_navigation_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span></pre></div></div>

<p>Você pode rodar os testes para cada versão de uma forma semelhante a que fizemos com o comando <code>runserver</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #666666; font-style: italic;"># Versão padrão</span>
$ python manage.py <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>appname...<span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Versão mobile</span>
$ python manage.py <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">--settings</span>=m_settings <span style="color: #7a0874; font-weight: bold;">&#91;</span>appname...<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<h3>Colocando em produção</h3>
<p>Apesar do que você possa achar, esta é a parte fácil.</p>
<p>Uma vez que a forma recomendada de <a href="http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/">colocar Django em produção</a> é com <a href="http://apache.org">Apache</a> e <a href="http://code.google.com/p/modwsgi/">mod_wsgi</a>, é possível que você já tenha um arquivo WSGI parecido com o seguinte:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>
&nbsp;
APPS_PATH   = <span style="color: #483d8b;">'/home/user/wsgi_apps'</span>
MYPROJECT_PATH = <span style="color: #483d8b;">'%s/myproject'</span> <span style="color: #66cc66;">%</span> APPS_PATH
&nbsp;
<span style="color: #808080; font-style: italic;"># Eggs de terceiros</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">site</span>
<span style="color: #dc143c;">site</span>.<span style="color: black;">addsitedir</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/home/user/.python/lib'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span> += <span style="color: black;">&#91;</span>APPS_PATH, MYPROJECT_PATH<span style="color: black;">&#93;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'DJANGO_SETTINGS_MODULE'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'myproject.settings'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>
application = django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>.<span style="color: black;">WSGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Tudo que precisamos fazer é configurar um outro sub-domínio WSGI no nosso servidor (ex: m.mywebsite.com) que referencia o seguinte arquivo de configurações WSGI:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>
&nbsp;
APPS_PATH   = <span style="color: #483d8b;">'/home/user/wsgi_apps'</span>
MYPROJECT_PATH = <span style="color: #483d8b;">'%s/myproject'</span> <span style="color: #66cc66;">%</span> APPS_PATH
&nbsp;
<span style="color: #808080; font-style: italic;"># Eggs de terceiros</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">site</span>
<span style="color: #dc143c;">site</span>.<span style="color: black;">addsitedir</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/home/user/.python/lib'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span> += <span style="color: black;">&#91;</span>APPS_PATH, MYPROJECT_PATH<span style="color: black;">&#93;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'DJANGO_SETTINGS_MODULE'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'myproject.m_settings'</span> <span style="color: #808080; font-style: italic;"># Esta linha</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>
application = django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>.<span style="color: black;">WSGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>A única diferença entre esses dois arquivos WSGI é que cada um aponta para um módulo <code>settings.py</code> diferente. E não, você não precisa ter uma cópia do site para cada versão, afinal estamos usando a mesma base de código para ambas as versões, lembra? <img src='http://weblog.destaquenet.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h3>Minimizando o tráfego</h3>
<p>Por incrível que pareça, nem todo mundo dispõe de conexões ultra-rápidas. Por isso, é muito importante que você mantenha seus sites tão leves quanto possível.</p>
<p>Existem vários projetos open source que podem ajudar nessa parte. Um deles é o <a href="http://code.google.com/p/django-compress/">django-compress</a>, uma app Django que fornece um sistema automatizado para compressão de arquivos CSS e JavaScript.</p>
<p>Aliás, não importa se seus websites são para dispositivos móveis ou não; você deve <em>sempre</em> tentar minimizar o tráfego.</p>
<h3>O que falta?</h3>
<p>Ainda temos um pequeno problema: quando alguém visita a versão padrão do site através de um dispositivo móvel, ou vice-versa, eles não são redirecionados para a versão apropriada. Mas isso é assunto para um próximo artigo!</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2010/07/12/django-para-dispositivos-moveis/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Django For Mobile Devices</title>
		<link>http://weblog.destaquenet.com/2010/07/12/django-for-mobile-devices/</link>
		<comments>http://weblog.destaquenet.com/2010/07/12/django-for-mobile-devices/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 11:00:00 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[deploy]]></category>
		<category><![CDATA[device]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[wsgi]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=932</guid>
		<description><![CDATA[We are living what some people call the &#8220;mobile explosion&#8221;, a time where an increasing number of portable devices &#8212; like tablets and smartphones &#8212; are becoming a significant part of the Web. This is the reason why having your &#8230; <a href="http://weblog.destaquenet.com/2010/07/12/django-for-mobile-devices/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We are living what some people call the &#8220;mobile explosion&#8221;, a time where an increasing number of portable devices &#8212; like tablets and smartphones &#8212; are becoming a significant part of the Web. This is the reason why having your website tailored for these devices is becoming increasingly important.</p>
<p>Fortunately, <a href="http://djangoproject.com/">Django</a> is one of the few web frameworks that makes this an easy problem to solve.</p>
<p><span id="more-932"></span></p>
<h3>How we solved it</h3>
<p>A simple way is to create a separate settings module for the mobile version. Doing this, we&#8217;re able to run two different webapps while reusing the existing codebase. Also, each version can have its own template directory, URL root configuration, etc.</p>
<p>Let&#8217;s do this. In the project root directory, create a file <code>m_settings.py</code> with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;"># File: myproject/m_settings.py</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;Django settings for the mobile-specific website.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Load the default settings</span>
<span style="color: #ff7700;font-weight:bold;">from</span> settings <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Domain &quot;m.mydomain.com&quot;</span>
SITE_ID = <span style="color: #ff4500;">2</span>
PREPEND_WWW = <span style="color: #008000;">False</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Used elsewhere to determine which version is running</span>
DEFAULT_VERSION = <span style="color: #008000;">False</span>
MOBILE_VERSION  = <span style="color: #008000;">True</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># URLs for the mobile version</span>
ROOT_URLCONF  = <span style="color: #483d8b;">'myproject.m_urls'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Templates for the mobile version</span>
TEMPLATE_DIRS = <span style="color: black;">&#40;</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">normpath</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'/template/mobile'</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Other settings...</span></pre></div></div>

<p>These two webapps share the same codebase, but that doesn&#8217;t mean they have to use the same URL root configuration. In fact, websites suitable for mobile devices are usually simpler, with fewer URL mappings.</p>
<p>To define separate URL root configuration for the mobile version, create a file <code>m_urls.py</code> with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;"># File: myproject/m_urls.py</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;Mobile-specific URL definition.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span>.<span style="color: black;">urls</span>.<span style="color: black;">defaults</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #808080; font-style: italic;"># from django.contrib import admin</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># admin.autodiscover()</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Error page handlers</span>
handler404 = <span style="color: #483d8b;">'website.views.error_404_handler'</span>
handler500 = <span style="color: #483d8b;">'website.views.error_500_handler'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Root URL patterns</span>
urlpatterns = patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>,
    <span style="color: #808080; font-style: italic;"># I18N</span>
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^i18n/'</span>, include<span style="color: black;">&#40;</span><span style="color: #483d8b;">'django.conf.urls.i18n'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
&nbsp;
    <span style="color: #808080; font-style: italic;"># Internal apps</span>
    <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^contact/'</span>, include<span style="color: black;">&#40;</span><span style="color: #483d8b;">'contact.urls'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
&nbsp;
    <span style="color: #808080; font-style: italic;"># (r'^admin/doc/', include('django.contrib.admindocs.urls')),</span>
    <span style="color: #808080; font-style: italic;"># (r'^admin/', include(admin.site.urls)),</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Other settings...</span>
<span style="color: black;">&#41;</span></pre></div></div>

<p>Note that, in this particular example, the <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/">Admin app</a> isn&#8217;t accessible from mobile version. This just shows that we can structure each webapp differently without too much trouble.</p>
<h3>Developing</h3>
<p>You already know how to run the development server for the default version. Most people don&#8217;t know, however, that it&#8217;s possible to choose a different port number and settings module, which allows us to launch one development server for each webapp version:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #666666; font-style: italic;"># Terminal window 1: default version</span>
$ python manage.py runserver <span style="color: #000000;">8000</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Terminal window 2: mobile version</span>
$ python manage.py runserver <span style="color: #660033;">--settings</span>=m_settings <span style="color: #000000;">8001</span></pre></div></div>

<p>Just develop your stuff and see the results in both versions right away. Easy peasy.</p>
<h3>Testing</h3>
<p>You probably noticed that we have two settings that can be used to identify what webapp version is being run, <code>DEFAULT_VERSION</code> and <code>MOBILE_VERSION</code>. </p>
<p>So it&#8217;s just a matter of checking those settings when you need to do different things in each webapp version. This is particularly useful when you use an app in both versions and want to create separate tests for each version:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;"># File: myproject/myapp/tests/__init__.py</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;Unit tests module for MyApp.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>settings, <span style="color: #483d8b;">'DEFAULT_VERSION'</span>, <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Tests the default version</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> default_models_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> default_navigation_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>settings, <span style="color: #483d8b;">'MOBILE_VERSION'</span>, <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Tests the mobile version</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> mobile_models_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> mobile_navigation_tests <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span></pre></div></div>

<p>We can run the tests for each version in a similar way we did before with the <code>runserver</code> command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #666666; font-style: italic;"># Default version</span>
$ python manage.py <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>appname...<span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Mobile version</span>
$ python manage.py <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">--settings</span>=m_settings <span style="color: #7a0874; font-weight: bold;">&#91;</span>appname...<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<h3>Deploying</h3>
<p>Despite what you may think, this is the easy part.</p>
<p>Since <a href="http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/">deploying Django</a> with <a href="http://apache.org/">Apache</a> and <a href="http://code.google.com/p/modwsgi/">mod_wsgi</a> is the recommended way to get Django into production, it&#8217;s likely that you already have a WSGI file like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>
&nbsp;
APPS_PATH   = <span style="color: #483d8b;">'/home/user/wsgi_apps'</span>
MYPROJECT_PATH = <span style="color: #483d8b;">'%s/myproject'</span> <span style="color: #66cc66;">%</span> APPS_PATH
&nbsp;
<span style="color: #808080; font-style: italic;"># Third-party eggs</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">site</span>
<span style="color: #dc143c;">site</span>.<span style="color: black;">addsitedir</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/home/user/.python/lib'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span> += <span style="color: black;">&#91;</span>APPS_PATH, MYPROJECT_PATH<span style="color: black;">&#93;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'DJANGO_SETTINGS_MODULE'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'myproject.settings'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>
application = django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>.<span style="color: black;">WSGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>All we need to do is configure another WSGI-enabled subdomain on our server, e.g. m.mywebsite.com, that refers to another WSGI file like this one:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>
&nbsp;
APPS_PATH   = <span style="color: #483d8b;">'/home/user/wsgi_apps'</span>
MYPROJECT_PATH = <span style="color: #483d8b;">'%s/myproject'</span> <span style="color: #66cc66;">%</span> APPS_PATH
&nbsp;
<span style="color: #808080; font-style: italic;"># Third-party eggs</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">site</span>
<span style="color: #dc143c;">site</span>.<span style="color: black;">addsitedir</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/home/user/.python/lib'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span> += <span style="color: black;">&#91;</span>APPS_PATH, MYPROJECT_PATH<span style="color: black;">&#93;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'DJANGO_SETTINGS_MODULE'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'myproject.m_settings'</span> <span style="color: #808080; font-style: italic;"># This line</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>
application = django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>.<span style="color: black;">WSGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The only difference between these two WSGI files is that each one points to a different settings module. And no, you don&#8217;t need to have a separate copy for each version. We are using the same codebase for both versions, remember? <img src='http://weblog.destaquenet.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h3>Minimizing the traffic</h3>
<p>Amazingly, not everyone has access ultra-fast internet connections on their devices, so it&#8217;s very important to keep your sites as lightweight as possible.</p>
<p>There are several open source projects that try to address this problem. One of them is <a href="http://code.google.com/p/django-compress/">django-compress</a>, a Django app that provides an automated system for compressing CSS and JavaScript files.</p>
<p>In fact, doesn&#8217;t matter if your websites target mobile devices or not; you should <em>always</em> try to minimize the amount of traffic.</p>
<h3>What&#8217;s left?</h3>
<p>There&#8217;s one small problem though: when someone visit the regular website from a mobile device, or vice-versa, they are not redirected to the appropriate version. But this is a topic for another article!</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2010/07/12/django-for-mobile-devices/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Django-Flash e o Novo Framework de Mensagens</title>
		<link>http://weblog.destaquenet.com/2010/05/21/django-flash-e-o-novo-framework-de-mensagens/</link>
		<comments>http://weblog.destaquenet.com/2010/05/21/django-flash-e-o-novo-framework-de-mensagens/#comments</comments>
		<pubDate>Fri, 21 May 2010 02:36:14 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[Português]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[django-flash]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=890</guid>
		<description><![CDATA[No começo da semana, depois de vários meses de trabalho duro e algum atraso, a versão 1.2 do Django foi finalmente lançada. Este talvez seja o lançamento mais aguardado do framework desde seu surgimento, e inclui recursos há muito aguardados, &#8230; <a href="http://weblog.destaquenet.com/2010/05/21/django-flash-e-o-novo-framework-de-mensagens/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>No começo da semana, depois de vários meses de trabalho duro e algum atraso, a versão 1.2 do Django <a href="http://www.djangoproject.com/download/">foi finalmente lançada</a>. Este talvez seja o lançamento mais aguardado do framework desde seu surgimento, e inclui recursos há muito aguardados, como <a href="http://docs.djangoproject.com/en/dev/releases/1.2/#support-for-multiple-databases">suporte a múltiplos bancos de dados</a> e um <a href="http://docs.djangoproject.com/en/dev/releases/1.2/#messages-framework">framework de &#8220;mensagens&#8221;</a>.</p>
<p>Sim, eu disse que o Django 1.2 traz incluso um framework de &#8220;mensagens&#8221;. Bom, né? Django realmente precisava vir acompanhado de uma app para resolver essa questão. Isso não significa, no entanto, que <a href="http://djangoflash.destaquenet.com/">Django-Flash</a> será descontinuado ou abandonado.</p>
<p>Então, se você usa Django-Flash em seus projetos, não se preocupe! Nada mudará. Django-Flash já é compatível com Django 1.2, e nós ocontinuaremos a mantê-lo por algumas razões.</p>
<p>A primeira delas é que nos importamos com quem usa nosso software. Nós não quebraremos seus projetos se você decidir fazer o upgrade para uma versão mais recente do Django &#8211; ou se decidir continuar usando uma versão anterior.</p>
<p>A outra é a liberdade de escolha. Você sabe, não existe uma única forma de se resolver todos os problemas. As pessoas devem ser livres para escolher a ferramenta apropriada a uma determinada situação.</p>
<p>Por exemplo, baseado nas minhas primeiras impressões, existem algumas coisas que eu realmente não gostei no novo framework de &#8220;mensagens&#8221;:</p>
<ol>
<li>Ele é <em>verboso</em> mesmo nos casos de uso mais simples</li>
<li>Ele associa mensagens a números de prioridade, encorajando os usuários a usá-lo como um framework de logging/debugging. Quero dizer, um framework de &#8220;mensagens&#8221; não é um framework de logging/debugging, certo?</li>
</ol>
<p>Mas essa é apenas a minha opinião. Talvez seja isso que faz do open source algo tão formidável!</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2010/05/21/django-flash-e-o-novo-framework-de-mensagens/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django-Flash and Django&#8217;s New Messages Framework</title>
		<link>http://weblog.destaquenet.com/2010/05/21/django-flash-and-djangos-new-messages-framework/</link>
		<comments>http://weblog.destaquenet.com/2010/05/21/django-flash-and-djangos-new-messages-framework/#comments</comments>
		<pubDate>Fri, 21 May 2010 02:31:59 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[django-flash]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=881</guid>
		<description><![CDATA[Earlier this week, after several months of hard work and some delay, Django 1.2 was finally released. This is probably the most exciting release since Django&#8217;s debut, and brings long waited features, like multi-db support and a user &#8220;messages&#8221; framework. &#8230; <a href="http://weblog.destaquenet.com/2010/05/21/django-flash-and-djangos-new-messages-framework/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Earlier this week, after several months of hard work and some delay, <a href="http://www.djangoproject.com/download/">Django 1.2 was finally released</a>. This is probably the most exciting release since Django&#8217;s debut, and brings long waited features, like <a href="http://docs.djangoproject.com/en/dev/releases/1.2/#support-for-multiple-databases">multi-db support</a> and a <a href="http://docs.djangoproject.com/en/dev/releases/1.2/#messages-framework">user &#8220;messages&#8221; framework</a>.</p>
<p>Yes, I said Django 1.2 comes with built-in user &#8220;messages&#8221; framework. Great, isn&#8217;t it? Django really needed a built-in contrib app to solve this problem. This doesn&#8217;t mean, however, that <a href="http://djangoflash.destaquenet.com/">Django-Flash</a> will be discontinued or abandoned.</p>
<p>So, if you use Django-Flash in your projects, don&#8217;t worry! Nothing will change. Django-Flash is already compatible with Django 1.2, and we&#8217;ll keep improving Django-Flash for a number of reasons.</p>
<p>First of all, we care about the people who use our software. We won&#8217;t break your stuff if you decide to upgrade to a newer version of Django &#8211; or if you decide to keep using the previous version.</p>
<p>Also, freedom of choice is a good thing. You know, there&#8217;s not only one true way to solve every problem. People should be free to choose whatever tool they think is right in a given situation.</p>
<p>For example, based on my first impressions, there are a couple of things I didn&#8217;t like at all about the new user &#8220;messages&#8221; framework:</p>
<ol>
<li>It&#8217;s verbose even for the simple cases</li>
<li>It ties messages to priority numbers, encouraging you to use it like a logging/debugging framework. I mean, a user &#8220;messages&#8221; framework is not a logging/debugging framework, right?</li>
</ol>
<p>But that&#8217;s just my opinion. I guess this is what makes open source so great!</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2010/05/21/django-flash-and-djangos-new-messages-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python para S60: Trabalho em Andamento?</title>
		<link>http://weblog.destaquenet.com/2010/02/17/python-para-s60-trabalho-em-andamento/</link>
		<comments>http://weblog.destaquenet.com/2010/02/17/python-para-s60-trabalho-em-andamento/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 12:30:29 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[Português]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[celular]]></category>
		<category><![CDATA[nokia]]></category>
		<category><![CDATA[pys60]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python for s60]]></category>
		<category><![CDATA[s60]]></category>
		<category><![CDATA[smartphone]]></category>
		<category><![CDATA[symbian]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=846</guid>
		<description><![CDATA[Eu fui um dos sortudos que perderam o celular a alguns meses atrás, durante aquele show maluco do AC/DC. Surpreendentemente, isso não me deixou aborrecido; apesar de não ser um heavy user, essa foi a oportunidade perfeita para eu comprar &#8230; <a href="http://weblog.destaquenet.com/2010/02/17/python-para-s60-trabalho-em-andamento/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Eu fui um dos sortudos que perderam o celular a alguns meses atrás, durante aquele <a href="http://weblog.destaquenet.com/2009/10/05/use-web-scraping-para-acompanhar-seus-pedidos-na-ticketmaster/">show maluco do AC/DC</a>. Surpreendentemente, isso não me deixou aborrecido; apesar de não ser um heavy user, essa foi a oportunidade perfeita para eu comprar meu primeiro smartphone.</p>
<p>No fim das contas eu acabei com um <a href="http://europe.nokia.com/find-products/devices/nokia-5800-xpressmusic">Nokia 5800 XpressMusic</a>, um smartphone relativamente barato e com features interessantes. Especificamente, o fato de poder rodar aplicações <a href="http://python.org">Python</a> nesse aparelho foi um fator decisivo!</p>
<p>Mas, o que exatamente podemos fazer com <a href="https://garage.maemo.org/projects/pys60/">Python para S60</a>? Eu decidi implementar uma aplicação não trivial para ver exatamente o que pode ser feito com ele. A aplicação em questão é um controle remoto Bluetooth chamado <a href="http://github.com/danielfm/pytriloquist">Pytriloquist</a>. (veja também <a href="http://www.flickr.com/photos/daniel_tritone/4313716091/in/set-72157623183881993/">screenshots</a> e <a href="http://www.youtube.com/watch?v=2IVXMAJHwI8">vídeo</a> de demonstração.)</p>
<p><span id="more-846"></span></p>
<h3>O que é Bom</h3>
<p><strong>Independência de Plataforma.</strong> A <a href="http://www.forum.nokia.com/Tools_Docs_and_Code/Tools/Platforms/S60_Platform_SDKs/">SDK para S60</a> só roda em Windows (que novidade). Isso obviamente é ruim para quem costuma usar outros sistemas operacionais. Felizmente, você não precisa de uma SDK gigante para desenvolver e distribuir aplicações para S60 se você escrevê-las em Python; tudo o que você precisa é do Python para S60, um editor de texto e um aparelho para testes.</p>
<p><strong>Linguagem e Biblioteca Padrão.</strong> Python é uma linguagem excelente: compacta, bonita, poderosa e tem uma biblioteca padrão <em>sensacional</em>. A boa notícia é que a maioria dos módulos rodam bem no S60. Por exemplo, é possível até localizar suas aplicações usando o módulo <code>gettext</code>!</p>
<h3>O que é Ruim</h3>
<p><strong>Trabalho em Andamento.</strong> Python para S60, até onde me consta, é um trabalho em andamento. Isso significa que a linguagem em si funciona bem, mas você pode dar falta de certos módulos ou <a href="https://garage.maemo.org/tracker/?atid=3201&#038;group_id=854&#038;func=browse">encontrar bugs</a> na biblioteca padrão. Por exemplo, enquanto eu escrevia o código Bluetooth para minha applicação, eu dei de cara com um <a href="https://garage.maemo.org/tracker/index.php?func=detail&#038;aid=3947&#038;group_id=854&#038;atid=3201">bug muito chato</a> onde você só pode chamar <code>btsocket.bt_discovert()</code> uma vez!</p>
<p><strong>Código Multi-Módulos.</strong> Se você está testando sua aplicação com Python ScriptShell, tudo irá funcionar de cara desde que você mantenha todo o código dentro de um único &#8212; e enorme &#8212; módulo. Se você quebrar seu código em vários módulos (ou pior, pacotes), você não poderá ter módulos importando uns aos outros, a menos que você ajuste o <code>PYTHON_PATH</code> de acordo.</p>
<p><strong>Documentação Incompleta e Desatualizada.</strong> Existem alguns PDFs e algumas documentações de API disponíveis, mas elas estão incompletas e desatualizadas, o que é ruim quando se trata de uma linguagem dinâmica; nós não temos um compilador para nos dizer o que há de errado com nosso código. Esse é o motivo pelo qual desenvolvedores Python são tão cuidadosos com documentação pra começo de conversa.</p>
<h3>O que é Feio</h3>
<p><strong>Acesso Limitado a Recursos Nativos.</strong> Especialmente em se tratando do sistema de UI nativo: você não pode mudar o texto das duas soft keys padrão, nem adicionar soft keys, nem adicionar mais que duas abas, nem ter um <code>Listbox</code> vazio, nem um monte de outras coisas. Você pode escrever UIs simples sem muito esforço, isso é verdade, mas essas deficiências todas tornam Python para S60 simplesmente inutilizável em projetos mais complexos.</p>
<p><strong>Inatividade do Projeto.</strong> O projeto parece estar em modo &#8220;stand by&#8221;. Novos bug reports são enviados de tempos em tempos, mas nenhum bug foi fechado há mais de <a href="https://garage.maemo.org/project/stats/?group_id=854">seis meses</a>.</p>
<h3>Conclusão</h3>
<p>Espero que as coisas mudem para melhor após o último <a href="http://www.symbian.org/symbian-feature-set/symbian-is-open-source">anúncio</a> feito pela Fundação Symbian, mas, enquanto isso, se você pretende criar aplicações sérias para S60, o faça em C++.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2010/02/17/python-para-s60-trabalho-em-andamento/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python for S60: A Work in Progress?</title>
		<link>http://weblog.destaquenet.com/2010/02/17/python-for-s60-a-work-in-progress/</link>
		<comments>http://weblog.destaquenet.com/2010/02/17/python-for-s60-a-work-in-progress/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 12:10:28 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[cellphone]]></category>
		<category><![CDATA[nokia]]></category>
		<category><![CDATA[pys60]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python for s60]]></category>
		<category><![CDATA[smartphone]]></category>
		<category><![CDATA[symbian]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=827</guid>
		<description><![CDATA[I was one of the lucky ones who lost their cellphones a few months ago, during that crazy AC/DC concert. Surprisingly, that didn&#8217;t upset me at all; although I&#8217;m not a heavy user, that was the perfect opportunity for me &#8230; <a href="http://weblog.destaquenet.com/2010/02/17/python-for-s60-a-work-in-progress/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was one of the lucky ones who lost their cellphones a few months ago, during that <a href="http://weblog.destaquenet.com/2009/10/12/using-web-scraping-to-follow-your-orders-on-ticketmaster/">crazy AC/DC concert</a>. Surprisingly, that didn&#8217;t upset me at all; although I&#8217;m not a heavy user, that was the perfect opportunity for me to get my first smartphone.</p>
<p>I ended up with a <a href="http://europe.nokia.com/find-products/devices/nokia-5800-xpressmusic">Nokia 5800 XpressMusic</a>, a relatively cheap smartphone with a nice feature set. Specifically, what took me for granted was the ability to run <a href="http://python.org">Python</a> applications in it!</p>
<p>But what exactly can we do with <a href="https://garage.maemo.org/projects/pys60/">Python for S60</a>? Well, to answer that question, I decided to implement a non-trivial app to see exactly what it has to offer. The app in question is a Bluetooth remote control called <a href="http://github.com/danielfm/pytriloquist">Pytriloquist</a>. (see also <a href="http://www.flickr.com/photos/daniel_tritone/4313716091/in/set-72157623183881993/">screenshots</a> and a <a href="http://www.youtube.com/watch?v=2IVXMAJHwI8">video</a> demonstration.)</p>
<p><span id="more-827"></span></p>
<h3>What&#8217;s Good</h3>
<p><strong>Platform Independence.</strong> The <a href="http://www.forum.nokia.com/Tools_Docs_and_Code/Tools/Platforms/S60_Platform_SDKs/">S60 SDK</a> is for Windows only (what a surprise). This is obviously bad for those who use other operating systems. Fortunately, you don&#8217;t need a massive SDK to develop and deploy S60 apps if you write them in Python; all you need is Python for S60, a text editor, and a device for testing.</p>
<p><strong>Language and Standard Library.</strong> Python is a terrific language: compact, beautiful, powerful, and have an <em>amazing</em> standard library. The good news is that the most popular modules work great on S60. For example, it&#8217;s even possible to localize your apps using the <code>gettext</code> module!</p>
<h3>What&#8217;s Bad</h3>
<p><strong>Work in Progress.</strong> Python for S60, as long as I know, is a work in progress. This means the language itself works fine, but you can expect missing modules or <a href="https://garage.maemo.org/tracker/?atid=3201&#038;group_id=854&#038;func=browse">some bugs</a> in standard library. For example, when writing the Bluetooth client code for my app, I encountered <a href="https://garage.maemo.org/tracker/index.php?func=detail&#038;aid=3947&#038;group_id=854&#038;atid=3201">a very annoying bug</a> in which you can only call <code>btsocket.bt_discover()</code> once!</p>
<p><strong>Multi-Module Code.</strong> If you are testing your app with Python ScriptShell, everything will work out-of-the-box as long as keep your whole code inside a single massive module. If you break up your code into several modules (or worse, packages) you won&#8217;t be able to have modules importing each other unless you adjust the <code>PYTHON_PATH</code> accordingly.</p>
<p><strong>Incomplete and Outdated Documentation.</strong> There are some PDFs and API documentation pages available, but they are incomplete and outdated, and this is a bad thing when it comes to dynamic languages; we don&#8217;t have a compiler to tell us what&#8217;s wrong with our code. This is why Python programmers are so careful about documentation in the first place.</p>
<h3>What&#8217;s Ugly</h3>
<p><strong>Limited Access to Native Features.</strong> Specially regarding the native UI system: you cannot change the label of the two standard soft keys, nor add other soft keys, nor add more than two tabs in a tabbed UI, nor have an empty <code>Listbox</code>, nor lots of other things. You can write simple UIs without much effort, that&#8217;s true, but those shortcomings just make Python for S60 unusable for more complex projects.</p>
<p><strong>Project Inactivity.</strong> The project appears to be on hold. New bug reports are submitted every now and then, but no bugs are closed for <a href="https://garage.maemo.org/project/stats/?group_id=854">six months</a>.</p>
<h3>Conclusion</h3>
<p>Let&#8217;s hope things change for the better after the last <a href="http://www.symbian.org/symbian-feature-set/symbian-is-open-source">announcement</a> made by the Symbian Foundation, but in the meantime, if you pretend to create serious apps for S60, do it in C++.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2010/02/17/python-for-s60-a-work-in-progress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using web scraping to follow your orders on Ticketmaster</title>
		<link>http://weblog.destaquenet.com/2009/10/12/using-web-scraping-to-follow-your-orders-on-ticketmaster/</link>
		<comments>http://weblog.destaquenet.com/2009/10/12/using-web-scraping-to-follow-your-orders-on-ticketmaster/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 16:07:10 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Off Topic]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[acdc]]></category>
		<category><![CDATA[brasil]]></category>
		<category><![CDATA[concert]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[pynotify]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scraping]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[ticketmaster]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=760</guid>
		<description><![CDATA[The legendary rock&#8217;n'roll band AC/DC will land on brazilian soil for the second time in November, for what is considered to be the show of the decade around here! The tickets for their single concert, sold by the brazilian Ticketmaster, &#8230; <a href="http://weblog.destaquenet.com/2009/10/12/using-web-scraping-to-follow-your-orders-on-ticketmaster/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The legendary rock&#8217;n'roll band <a href="http://acdc.com/">AC/DC</a> will land on brazilian soil for the second time in November, for what is considered to be the show of the decade around here! The tickets for their single concert, sold by the <a href="http://www.ticketmaster.com.br/">brazilian Ticketmaster</a>, ran out in less than 48 hours. This concert is going to be amazing!</p>
<p>A lot of things happened during those 48 hous. The Ticketmaster staff had trouble to keep the website on-line, and due to the enormous load, it&#8217;s possible that only a few thousands of people had enough luck to buy their tickets via the website.</p>
<p>Also, Ticketmaster admits that the notification e-mails may not reach, which means we cannot rely on those to follow our orders. In fact, they recomend that customers check their orders manually once a day. Oh boy&#8230;</p>
<p>I hate to do certain tasks manually, specially the ones that can be automated. So, in this post I&#8217;ll show you a very simple <a href="http://www.python.org">Python</a>-based command-line tool that displays the status of your Ticketmaster<sup class='footnote'><a href='#fn-760-1' id='fnref-760-1'>1</a></sup> orders on the screen<sup class='footnote'><a href='#fn-760-2' id='fnref-760-2'>2</a></sup>, using a technique known as <a href="http://en.wikipedia.org/wiki/Web_scraping">web scraping</a>.</p>
<p><span id="more-760"></span></p>
<p>The code, which is also <a href="http://github.com/danielfm/ticketmaster-order-status">on Github</a>, can be seen below:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;">#-*- coding:utf-8 -*-</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
<span style="color: #ff7700;font-weight:bold;">import</span> pynotify
&nbsp;
&nbsp;
_BASE_URL  = <span style="color: #483d8b;">'https://ticketing.ticketmaster.com.br'</span>
_LOGIN_URI = <span style="color: #483d8b;">'%s/shwLogin.cfm'</span> <span style="color: #66cc66;">%</span> _BASE_URL
_ORDER_URI = <span style="color: #483d8b;">'%s/shwCompraDetalhe.cfm?pedidoID=%%s'</span> <span style="color: #66cc66;">%</span> _BASE_URL
&nbsp;
_ORDERS_LINK = <span style="color: #483d8b;">'&lt;a href=&quot;%s&quot;&gt;More...&lt;/a&gt;'</span> <span style="color: #66cc66;">%</span> _LOGIN_URI
&nbsp;
_PRIORITY_LOW      = pynotify.<span style="color: black;">URGENCY_LOW</span>
_PRIORITY_NORMAL   = pynotify.<span style="color: black;">URGENCY_NORMAL</span>
_PRIORITY_CRITICAL = pynotify.<span style="color: black;">URGENCY_CRITICAL</span>
&nbsp;
_NOTIFICATION_TITLE = <span style="color: #483d8b;">'Ticketmaster Order %s'</span>
_STATUS_CODES = <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'Livre'</span>      : <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Not processed yet'</span>      , _PRIORITY_LOW<span style="color: black;">&#41;</span>,
    <span style="color: #483d8b;">'StdReserva'</span> : <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Reserving your tickets'</span> , _PRIORITY_NORMAL<span style="color: black;">&#41;</span>,
    <span style="color: #483d8b;">'StdCobranca'</span>: <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Charging your tickets'</span>  , _PRIORITY_NORMAL<span style="color: black;">&#41;</span>,
    <span style="color: #483d8b;">'VendaOk'</span>    : <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Billed'</span>                 , _PRIORITY_CRITICAL<span style="color: black;">&#41;</span>,
    <span style="color: #483d8b;">'Recusada'</span>   : <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Rejected'</span>               , _PRIORITY_CRITICAL<span style="color: black;">&#41;</span>
<span style="color: black;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> initialize_notification_system<span style="color: black;">&#40;</span>app_title<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> pynotify.<span style="color: black;">init</span><span style="color: black;">&#40;</span>app_title<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> display_message<span style="color: black;">&#40;</span>title, message, priority=_PRIORITY_CRITICAL<span style="color: black;">&#41;</span>:
    message = pynotify.<span style="color: black;">Notification</span><span style="color: black;">&#40;</span>title, message<span style="color: black;">&#41;</span>
    message.<span style="color: black;">set_urgency</span><span style="color: black;">&#40;</span>priority<span style="color: black;">&#41;</span>
    message.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> check_orders<span style="color: black;">&#40;</span><span style="color: #dc143c;">email</span>, password, order_ids<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        opener = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">build_opener</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">urllib2</span>.<span style="color: black;">HTTPCookieProcessor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        login<span style="color: black;">&#40;</span>opener, <span style="color: #dc143c;">email</span>, password<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> order_id <span style="color: #ff7700;font-weight:bold;">in</span> order_ids:
                show_order_status<span style="color: black;">&#40;</span>opener, order_id<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span>:
            title = _NOTIFICATION_TITLE <span style="color: #66cc66;">%</span> order_id
            display_message<span style="color: black;">&#40;</span>title, <span style="color: #483d8b;">'Cannot check status'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
        display_message<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Ticketmaster'</span>, <span style="color: #483d8b;">'Login failed'</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> login<span style="color: black;">&#40;</span>opener, <span style="color: #dc143c;">email</span>, password<span style="color: black;">&#41;</span>:
    params = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlencode</span><span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'email'</span>: <span style="color: #dc143c;">email</span>, <span style="color: #483d8b;">'senha'</span>: password<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
    response = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s?tentaLogin=1'</span> <span style="color: #66cc66;">%</span> _LOGIN_URI, params<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Invalid credentials check</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> response.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'senha incorreta'</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">0</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> show_order_status<span style="color: black;">&#40;</span>opener, order_id<span style="color: black;">&#41;</span>:
    response = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>_ORDER_URI <span style="color: #66cc66;">%</span> order_id<span style="color: black;">&#41;</span>
    content = response.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Search for the order status codes</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> key, value <span style="color: #ff7700;font-weight:bold;">in</span> _STATUS_CODES.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> content.<span style="color: black;">find</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">0</span>:
            title = _NOTIFICATION_TITLE <span style="color: #66cc66;">%</span> order_id
            message = <span style="color: #483d8b;">'%s. %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>value<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, _ORDERS_LINK<span style="color: black;">&#41;</span>
            display_message<span style="color: black;">&#40;</span>title, message, value<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        display_message<span style="color: black;">&#40;</span>_NOTIFICATION_TITLE <span style="color: #66cc66;">%</span> order_id, <span style="color: #483d8b;">'Status not found'</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    initialize_notification_system<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Ticketmaster Order Status&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">optparse</span>
    p = <span style="color: #dc143c;">optparse</span>.<span style="color: black;">OptionParser</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Required command line options</span>
    p.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'--email'</span>    , <span style="color: #483d8b;">'-e'</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'E-mail address used in your Ticketmaster credentials.'</span><span style="color: black;">&#41;</span>
    p.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'--password'</span> , <span style="color: #483d8b;">'-p'</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'Password used in your Ticketmaster credentials.'</span><span style="color: black;">&#41;</span>
    p.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'--order-ids'</span>, <span style="color: #483d8b;">'-o'</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'Order IDs to be checked, separated by collons.'</span><span style="color: black;">&#41;</span>
&nbsp;
    options = p.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> options.<span style="color: #dc143c;">email</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> options.<span style="color: black;">password</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> options.<span style="color: black;">order_ids</span>:
        p.<span style="color: black;">print_help</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        order_ids = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> s: s.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, options.<span style="color: black;">order_ids</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">','</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        check_orders<span style="color: black;">&#40;</span>options.<span style="color: #dc143c;">email</span>, options.<span style="color: black;">password</span>, order_ids<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The reason I&#8217;ve used web scrapping is because the brazilian Ticketmaster doesn&#8217;t seem to provide an API the way <a href="http://flickr.com">Flickr</a> does, for example. Nevertheless, the code is quite simple.</p>
<p>As you can see, it first fires a HTTP request to the login URI in order to validate the user credentials. If the authentication succeeds, our cookie-aware <code>opener</code> will get the session cookies from the server and use them on the following requests, which extract the current order status for each order ID.</p>
<p>Now, let&#8217;s give it a spin:</p>
<div id="attachment_763" class="wp-caption aligncenter" style="width: 310px"><a href="http://weblog.destaquenet.com/wp-content/uploads/2009/10/order_status1.png"><img src="http://weblog.destaquenet.com/wp-content/uploads/2009/10/order_status1-300x104.png" alt="Current order status" title="order_status" width="300" height="104" class="size-medium wp-image-763" /></a><p class="wp-caption-text">Current order status</p></div>
<p>Finally, add a new entry to your <code>crontab</code> to run the script whenever you want (e.g. every 10 minutes):</p>
<pre>*/10 * * * * python ticketmaster_order_status.py --email=your@email.com --password=passwd --order-ids=1234</pre>
<p>Good luck and let there be rock! <img src='http://weblog.destaquenet.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-760-1'>The script only works for the <a href="http://www.ticketmaster.com.br/">brazilian Ticketmaster</a> website. <span class='footnotereverse'><a href='#fnref-760-1'>&#8617;</a></span></li>
<li id='fn-760-2'>The script uses <a href="http://www.galago-project.org/news/index.php">libnotify</a> to display notifications on the screen, so don&#8217;t forget to install it. <span class='footnotereverse'><a href='#fnref-760-2'>&#8617;</a></span></li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2009/10/12/using-web-scraping-to-follow-your-orders-on-ticketmaster/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Use web scraping para acompanhar seus pedidos na Ticketmaster</title>
		<link>http://weblog.destaquenet.com/2009/10/05/use-web-scraping-para-acompanhar-seus-pedidos-na-ticketmaster/</link>
		<comments>http://weblog.destaquenet.com/2009/10/05/use-web-scraping-para-acompanhar-seus-pedidos-na-ticketmaster/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 12:30:43 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[Off Topic]]></category>
		<category><![CDATA[Português]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[acdc]]></category>
		<category><![CDATA[brasil]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[pynotify]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scraping]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[show]]></category>
		<category><![CDATA[ticketmaster]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=710</guid>
		<description><![CDATA[A legendária banda de rock&#8217;n'roll AC/DC irá pousar em solo brasileiro pela segunda vez em Novembro, para o que é considerado o show da década por aqui! Os ingressos para a única apresentação, vendidos pela Ticketmaster, se esgotaram em menos &#8230; <a href="http://weblog.destaquenet.com/2009/10/05/use-web-scraping-para-acompanhar-seus-pedidos-na-ticketmaster/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A legendária banda de rock&#8217;n'roll <a href="http://acdc.com/">AC/DC</a> irá pousar em solo brasileiro pela segunda vez em Novembro, para o que é considerado o show da década por aqui! Os ingressos para a única apresentação, vendidos pela <a href="http://www.ticketmaster.com.br/">Ticketmaster</a>, se esgotaram em menos de 48 horas. Esse show será incrível!</p>
<p>Muita coisa aconteceu nessas 48 horas. O pessoal da Ticketmaster teve trabalho para manter o website no ar e, em razão da enorme carga, é possível que apenas poucos milhares de fãs tenham sido sortudos o bastante para comprar seus ingressos através da internet.</p>
<p>Além disso, a Ticketmaster admite que os e-mails de notificação podem não ser enviados, o que significa que não podemos depender deles para acompanhar nossos pedidos. Na verdade, eles até recomendam que seus clientes chequem os pedidos diariamente, na mão. Caramba&#8230;</p>
<p>Eu odeio fazer certas tarefas manualmente, especialmente as que podem ser automatizadas. Então, neste post eu mostrarei uma ferramenta escrita em <a href="http://www.python.org">Python</a> que mostra a situação dos seus pedidos Ticketmaster na tela<sup class='footnote'><a href='#fn-710-1' id='fnref-710-1'>1</a></sup>, usando uma técnica conhecida como <a href="http://en.wikipedia.org/wiki/Web_scraping">web scraping</a>.</p>
<p><span id="more-710"></span></p>
<p>O código, que também está disponível <a href="http://github.com/danielfm/ticketmaster-order-status">no Github</a>, pode ser visto abaixo:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;">#-*- coding:utf-8 -*-</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
<span style="color: #ff7700;font-weight:bold;">import</span> pynotify
&nbsp;
&nbsp;
_BASE_URL  = <span style="color: #483d8b;">'https://ticketing.ticketmaster.com.br'</span>
_LOGIN_URI = <span style="color: #483d8b;">'%s/shwLogin.cfm'</span> <span style="color: #66cc66;">%</span> _BASE_URL
_ORDER_URI = <span style="color: #483d8b;">'%s/shwCompraDetalhe.cfm?pedidoID=%%s'</span> <span style="color: #66cc66;">%</span> _BASE_URL
&nbsp;
_ORDERS_LINK = <span style="color: #483d8b;">'&lt;a href=&quot;%s&quot;&gt;More...&lt;/a&gt;'</span> <span style="color: #66cc66;">%</span> _LOGIN_URI
&nbsp;
_PRIORITY_LOW      = pynotify.<span style="color: black;">URGENCY_LOW</span>
_PRIORITY_NORMAL   = pynotify.<span style="color: black;">URGENCY_NORMAL</span>
_PRIORITY_CRITICAL = pynotify.<span style="color: black;">URGENCY_CRITICAL</span>
&nbsp;
_NOTIFICATION_TITLE = <span style="color: #483d8b;">'Ticketmaster Order %s'</span>
_STATUS_CODES = <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'Livre'</span>      : <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Not processed yet'</span>      , _PRIORITY_LOW<span style="color: black;">&#41;</span>,
    <span style="color: #483d8b;">'StdReserva'</span> : <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Reserving your tickets'</span> , _PRIORITY_NORMAL<span style="color: black;">&#41;</span>,
    <span style="color: #483d8b;">'StdCobranca'</span>: <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Charging your tickets'</span>  , _PRIORITY_NORMAL<span style="color: black;">&#41;</span>,
    <span style="color: #483d8b;">'VendaOk'</span>    : <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Billed'</span>                 , _PRIORITY_CRITICAL<span style="color: black;">&#41;</span>,
    <span style="color: #483d8b;">'Recusada'</span>   : <span style="color: black;">&#40;</span><span style="color: #483d8b;">'Rejected'</span>               , _PRIORITY_CRITICAL<span style="color: black;">&#41;</span>
<span style="color: black;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> initialize_notification_system<span style="color: black;">&#40;</span>app_title<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> pynotify.<span style="color: black;">init</span><span style="color: black;">&#40;</span>app_title<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> display_message<span style="color: black;">&#40;</span>title, message, priority=_PRIORITY_CRITICAL<span style="color: black;">&#41;</span>:
    message = pynotify.<span style="color: black;">Notification</span><span style="color: black;">&#40;</span>title, message<span style="color: black;">&#41;</span>
    message.<span style="color: black;">set_urgency</span><span style="color: black;">&#40;</span>priority<span style="color: black;">&#41;</span>
    message.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> check_orders<span style="color: black;">&#40;</span><span style="color: #dc143c;">email</span>, password, order_ids<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        opener = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">build_opener</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">urllib2</span>.<span style="color: black;">HTTPCookieProcessor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        login<span style="color: black;">&#40;</span>opener, <span style="color: #dc143c;">email</span>, password<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> order_id <span style="color: #ff7700;font-weight:bold;">in</span> order_ids:
                show_order_status<span style="color: black;">&#40;</span>opener, order_id<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span>:
            title = _NOTIFICATION_TITLE <span style="color: #66cc66;">%</span> order_id
            display_message<span style="color: black;">&#40;</span>title, <span style="color: #483d8b;">'Cannot check status'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
        display_message<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Ticketmaster'</span>, <span style="color: #483d8b;">'Login failed'</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> login<span style="color: black;">&#40;</span>opener, <span style="color: #dc143c;">email</span>, password<span style="color: black;">&#41;</span>:
    params = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlencode</span><span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'email'</span>: <span style="color: #dc143c;">email</span>, <span style="color: #483d8b;">'senha'</span>: password<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
    response = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s?tentaLogin=1'</span> <span style="color: #66cc66;">%</span> _LOGIN_URI, params<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Invalid credentials check</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> response.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'senha incorreta'</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">0</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> show_order_status<span style="color: black;">&#40;</span>opener, order_id<span style="color: black;">&#41;</span>:
    response = opener.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>_ORDER_URI <span style="color: #66cc66;">%</span> order_id<span style="color: black;">&#41;</span>
    content = response.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Search for the order status codes</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> key, value <span style="color: #ff7700;font-weight:bold;">in</span> _STATUS_CODES.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> content.<span style="color: black;">find</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">0</span>:
            title = _NOTIFICATION_TITLE <span style="color: #66cc66;">%</span> order_id
            message = <span style="color: #483d8b;">'%s. %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>value<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, _ORDERS_LINK<span style="color: black;">&#41;</span>
            display_message<span style="color: black;">&#40;</span>title, message, value<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        display_message<span style="color: black;">&#40;</span>_NOTIFICATION_TITLE <span style="color: #66cc66;">%</span> order_id, <span style="color: #483d8b;">'Status not found'</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    initialize_notification_system<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Ticketmaster Order Status&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">optparse</span>
    p = <span style="color: #dc143c;">optparse</span>.<span style="color: black;">OptionParser</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Required command line options</span>
    p.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'--email'</span>    , <span style="color: #483d8b;">'-e'</span>, \
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'E-mail address used in your Ticketmaster credentials.'</span><span style="color: black;">&#41;</span>
    p.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'--password'</span> , <span style="color: #483d8b;">'-p'</span>, \
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'Password used in your Ticketmaster credentials.'</span><span style="color: black;">&#41;</span>
    p.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'--order-ids'</span>, <span style="color: #483d8b;">'-o'</span>, \
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'Order IDs to be checked, separated by collons.'</span><span style="color: black;">&#41;</span>
&nbsp;
    options = p.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> options.<span style="color: #dc143c;">email</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> options.<span style="color: black;">password</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> options.<span style="color: black;">order_ids</span>:
        p.<span style="color: black;">print_help</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        order_ids = <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> s: s.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, options.<span style="color: black;">order_ids</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">','</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        check_orders<span style="color: black;">&#40;</span>options.<span style="color: #dc143c;">email</span>, options.<span style="color: black;">password</span>, order_ids<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>A razão pela qual eu optei por web scrapping é porque a Ticketmaster não fornece uma API da mesma forma que o <a href="http://flickr.com">Flickr</a> fornece, por exemplo. Ainda assim, o código é bastante simples.</p>
<p>Como você pode ver, ele primeiro envia uma requisição HTTP para a URI de login com o objetivo de validar as credenciais do usuário. Se a autenticação for bem sucedida, nosso <code>opener</code> receberá os cookies de sessão do servidor e os usará nas requisições seguintes, que buscam a situação de cada um dos pedidos informados.</p>
<p>Rodando o script agora:</p>
<div id="attachment_763" class="wp-caption aligncenter" style="width: 310px"><a href="http://weblog.destaquenet.com/wp-content/uploads/2009/10/order_status1.png"><img src="http://weblog.destaquenet.com/wp-content/uploads/2009/10/order_status1-300x104.png" alt="Current order status" title="order_status" width="300" height="104" class="size-medium wp-image-763" /></a><p class="wp-caption-text">Current order status</p></div>
<p>Finalmente, inclua uma nova entrada no seu <code>crontab</code> para que o script rode sempre que necessário (ex: a cada 10 minutos):</p>
<pre>*/10 * * * * python ticketmaster_order_status.py --email=seu@email.com --password=senha --order-ids=1234</pre>
<p>Boa sorte e <em>let there be rock</em>! <img src='http://weblog.destaquenet.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-710-1'>O script usa a biblioteca <a href="http://www.galago-project.org/news/index.php">libnotify</a> para mostrar as notificações na tela, portanto não esqueça de instalá-la. <span class='footnotereverse'><a href='#fnref-710-1'>&#8617;</a></span></li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2009/10/05/use-web-scraping-para-acompanhar-seus-pedidos-na-ticketmaster/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Linux + Python: Script para checar o status dos seus sites</title>
		<link>http://weblog.destaquenet.com/2009/07/20/linux-python-script-para-checar-o-status-dos-seus-sites/</link>
		<comments>http://weblog.destaquenet.com/2009/07/20/linux-python-script-para-checar-o-status-dos-seus-sites/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 16:54:01 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[Português]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[pynotify]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[site]]></category>
		<category><![CDATA[status]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=624</guid>
		<description><![CDATA[Os serviços de hosting que você utiliza estão meio ruim das pernas? Vez ou outra você descobre que seus sites estão fora do ar e ninguém te notificou a respeito? Infelizmente, coisas assim estão acontecendo por aqui com uma certa &#8230; <a href="http://weblog.destaquenet.com/2009/07/20/linux-python-script-para-checar-o-status-dos-seus-sites/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Os serviços de <em>hosting</em> que você utiliza <a href="http://statusblog.locaweb.com.br/">estão meio ruim das pernas</a>? Vez ou outra você descobre que seus sites estão fora do ar e <em>ninguém</em> te notificou a respeito?</p>
<p>Infelizmente, coisas assim estão acontecendo por aqui com uma certa frequência. Por isso, resolvi escrever o <code>script</code> abaixo, cuja função é mostrar uma mensagem de notificação sempre que algum dos sites pré-determinados saiam do ar: <sup class='footnote'><a href='#fn-624-1' id='fnref-624-1'>1</a></sup></p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">httplib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> pynotify
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> display_message<span style="color: black;">&#40;</span>title, message<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> pynotify.<span style="color: black;">init</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Website Status'</span><span style="color: black;">&#41;</span>:
        n = pynotify.<span style="color: black;">Notification</span><span style="color: black;">&#40;</span>title, message<span style="color: black;">&#41;</span>
        n.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> check_status<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>websites, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
    timeout = kwargs.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'timeout'</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> website <span style="color: #ff7700;font-weight:bold;">in</span> websites:
        conn = <span style="color: #dc143c;">httplib</span>.<span style="color: black;">HTTPConnection</span><span style="color: black;">&#40;</span>website, timeout=timeout<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            conn.<span style="color: black;">request</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'GET'</span>, <span style="color: #483d8b;">'/'</span><span style="color: black;">&#41;</span>
            response = conn.<span style="color: black;">getresponse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> response.<span style="color: black;">status</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">200</span>:
                <span style="color: #ff7700;font-weight:bold;">raise</span> Error<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span>:
           display_message<span style="color: black;">&#40;</span>website, <span style="color: #483d8b;">'Web site could not be reached!'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    check_status<span style="color: black;">&#40;</span><span style="color: #483d8b;">'www.awebsite.com'</span>, <span style="color: #483d8b;">'www.anotherwebsite.com'</span><span style="color: black;">&#41;</span></pre></div></div>

<p><span id="more-624"></span></p>
<p>O <em>script</em> em funcionamento:</p>
<div id="attachment_628" class="wp-caption aligncenter" style="width: 463px"><a href="http://weblog.destaquenet.com/wp-content/uploads/2009/07/notification.png"><img src="http://weblog.destaquenet.com/wp-content/uploads/2009/07/notification.png" alt="Exemplo de notificação" title="Exemplo de notificação" width="453" height="166" class="size-full wp-image-628" /></a><p class="wp-caption-text">Exemplo de notificação</p></div>
<p>Para completar, se você deseja que esse <code>script</code> seja executado uma vez a cada 5 minutos, basta inserir a linha abaixo no seu <code>crontab</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold;">*/</span><span style="color: #000000;">5</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>script.py</pre></div></div>

<p>Pronto! Agora, se o seu serviço de <em>hosting</em> continuar deixando a desejar, talvez a melhor solução seja mesmo mudar para outro lugar&#8230;
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-624-1'>A biblioteca <code>python-notify</code> precisa estar instalada para que o <em>script</em> funcione. <span class='footnotereverse'><a href='#fnref-624-1'>&#8617;</a></span></li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2009/07/20/linux-python-script-para-checar-o-status-dos-seus-sites/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Fabric + VirtualEnv: uma combinação explosiva (no bom sentido)</title>
		<link>http://weblog.destaquenet.com/2009/07/01/fabric-virtualenv-uma-combinacao-explosiva-no-bom-sentido/</link>
		<comments>http://weblog.destaquenet.com/2009/07/01/fabric-virtualenv-uma-combinacao-explosiva-no-bom-sentido/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 04:20:01 +0000</pubDate>
		<dc:creator>Daniel Martins</dc:creator>
				<category><![CDATA[Português]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[Tutoriais]]></category>
		<category><![CDATA[automatização]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[django-flash]]></category>
		<category><![CDATA[fabric]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[pypi]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[setuptools]]></category>
		<category><![CDATA[teste]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[virtualenv]]></category>

		<guid isPermaLink="false">http://weblog.destaquenet.com/?p=554</guid>
		<description><![CDATA[Escreveu uma super-hiper-biblioteca ou aplicação em Python mas quer se certificar de que ela funciona em diferentes versões do interpretador? Embora isso possa ser resolvido através da execução manual dos testes com os vários interpretadores, isso é chato e improdutivo. &#8230; <a href="http://weblog.destaquenet.com/2009/07/01/fabric-virtualenv-uma-combinacao-explosiva-no-bom-sentido/">Continue lendo <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Escreveu uma super-hiper-biblioteca ou aplicação em <a href="http://python.org">Python</a> mas quer se certificar de que ela funciona em diferentes versões do interpretador? Embora isso possa ser resolvido através da execução <em>manual</em> dos testes com os vários interpretadores, isso é chato e improdutivo. Além do mais, cada versão do Python pode ter diferentes bibliotecas instaladas, o que pode acabar influenciando no resultado dos testes.</p>
<p>Temos portanto dois problemas a serem resolvidos. O primeiro problema, relacionado a automatização de tarefas repetitivas, pode ser facilmente solucionado com <a href="http://docs.fabfile.org/">Fabric</a>, uma ferramenta muito útil para criação de scripts de build e deployment de aplicações. Já escrevemos <a href="http://weblog.destaquenet.com/2008/10/05/deployment-de-aplicacoes-python-com-fabric/">um texto introdutório</a> sobre a mesma, não deixe de conferir.</p>
<p>O segundo problema, entretanto, é um pouco mais complicado de se resolver, mas não é nenhum bicho de sete cabeças. Com <a href="http://pypi.python.org/pypi/virtualenv/">VirtualEnv</a>, é possível criar ambientes Python isolados com o objetivo de testar e executar aplicações em uma espécie de sandbox onde temos total controle sobre as bibliotecas ali instaladas.</p>
<p><span id="more-554"></span></p>
<h3>Instalando as diferentes versões do Python</h3>
<p>O primeiro passo é instalar as versões do Python a serem usadas nos testes. Rode o comando abaixo para instalar os diferentes interpretadores, supondo que você utilize <a href="http://www.debian.org/">Debian</a> e sua aplicação precise funcionar nas versões 2.4, 2.5 e 2.6 do Python:</p>
<pre>$ sudo apt-get install python2.4 python2.4-dev \
    python2.5 python2.5-dev \
    python2.6 python2.6-dev</pre>
<p>A versão atual do Ubuntu já inclui o Python 2.6 por padrão. Veja também que aproveitamos para instalar os arquivos de desenvolvimento para cada versão, pois eventualmente precisamos deles para compilar bibliotecas com extensões escritas em C.</p>
<h3>Instalando Fabric e VirtualEnv</h3>
<p>Da mesma forma:</p>
<pre>$ sudo apt-get install python-setuptools</pre>
<p>Após a instalação do <a href="http://pypi.python.org/pypi/setuptools/">SetupTools</a>, execute o seguinte comando para instalar o Fabric e o VirtualEnv:</p>
<pre>$ sudo easy_install Fabric virtualenv</pre>
<h3>Configurando ambientes isolados para sua aplicação</h3>
<p>O próximo passo é criar ambientes de execução isolados para sua aplicação, sendo que cada ambiente deve usar uma versão diferente do interpretador do Python. Para fazer isso, execute os comandos abaixo, substituindo o <code>APP</code> pelo nome da sua aplicação ou biblioteca:</p>
<pre>$ mkdir ~/.virtualenvs
$ cd ~/.virtualenvs
$ virtualenv --python=/usr/bin/python2.6 --no-site-packages APP-py2.6
$ virtualenv --python=/usr/bin/python2.5 --no-site-packages APP-py2.5
$ virtualenv --python=/usr/bin/python2.4 --no-site-packages APP-py2.4</pre>
<p>Primeiramente criamos o diretório <code>~/.virtualenvs</code>, que é por convenção o diretório padrão para armazenamento dos ambientes. Em seguida, criamos três ambientes para nossa aplicação <code>APP</code>. A flag <code>--no-site-packages</code> é <em>opcional</em> e indica que as bibliotecas instaladas no diretório <code>site-packages</code> do Python em questão não devem ser visíveis ao ambiente sendo criado.</p>
<p>Finalmente, não esqueça de instalar, em cada um dos ambientes, as dependências exigidas pela sua aplicação:</p>
<pre>$ source ~/.virtualenvs/APP-py2.6/bin/activate
(APP-py2.6) $ easy_install biblioteca1 biblioteca2 ... bibliotecaN
(APP-py2.6) $ source ~/.virtualenvs/APP-py2.5/bin/activate
(APP-py2.5) $ easy_install biblioteca1 biblioteca2 ... bibliotecaN
(APP-py2.5) $ source ~/.virtualenvs/APP-py2.4/bin/activate
(APP-py2.4) $ easy_install biblioteca1 biblioteca2 ... bibliotecaN</pre>
<p>Supondo que sua aplicação utilize o SetupTools para a configuração dos metadados do projeto (através do script <code>setup.py</code>), os comandos abaixo devem indicar se a aplicação funciona nos ambientes que criamos:</p>
<pre>$ source ~/.virtualenvs/APP-py2.6/bin/activate
(APP-py2.6) $ python setup.py test
...
(APP-py2.6) $ source ~/.virtualenvs/APP-py2.5/bin/activate
(APP-py2.5) $ python setup.py test
...
(APP-py2.5) $ source ~/.virtualenvs/APP-py2.4/bin/activate
(APP-py2.4) $ python setup.py test
...</pre>
<h3>Automatizando!</h3>
<p>Se tudo funcionou até aqui, estamos prontos para criar um script que nos permita rodar os testes de forma automática, nos diferentes ambientes.</p>
<p>Supondo que sua aplicação utilize o SetupTools para a configuração dos metadados do projeto, o exemplo abaixo resolve o problema:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Environment info</span>
config.<span style="color: black;">project</span> = <span style="color: #483d8b;">'APP'</span>
config.<span style="color: black;">virtualenv_dir</span> = <span style="color: #483d8b;">'~/.virtualenvs'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Supported Python versions</span>
config.<span style="color: black;">versions</span> = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'2.4'</span>, <span style="color: #483d8b;">'2.5'</span>, <span style="color: #483d8b;">'2.6'</span>,<span style="color: black;">&#41;</span>
config.<span style="color: black;">default_version</span> = <span style="color: #483d8b;">'2.6'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> setup<span style="color: black;">&#40;</span>command, version=config.<span style="color: black;">default_version</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Executes the given setup command with a virtual Python installation.
    &quot;&quot;&quot;</span>
    local<span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s/%s-py%s/bin/python setup.py %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>config.<span style="color: black;">virtualenv_dir</span>, config.<span style="color: black;">project</span>, version, command<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">test</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Runs all tests in different Python versions.
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> version <span style="color: #ff7700;font-weight:bold;">in</span> config.<span style="color: black;">versions</span>:
        setup<span style="color: black;">&#40;</span><span style="color: #483d8b;">'test'</span>, version<span style="color: black;">&#41;</span></pre></div></div>

<p>Agora é só alegria:</p>
<pre>$ fab test
Fabric v. 0.1.1.
Running test...
[localhost] run: ~/.virtualenvs/APP-py2.4/bin/python setup.py test
...
[localhost] run: ~/.virtualenvs/APP-py2.5/bin/python setup.py test
...
[localhost] run: ~/.virtualenvs/APP-py2.6/bin/python setup.py test
...</pre>
<p>Devo lembrar que o script mostrado é só um exemplo. Você pode (e <strong>deve</strong>) modificá-lo para que este atenda às suas necessidades.</p>
<h3>(Bônus!) Distribuindo sua aplicação para diferentes versões do Python</h3>
<p>Com mais algumas linhas de código, podemos criar tasks que nos permitam gerar arquivos para distribuição da aplicação para diferentes versões do Python:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> dist_src<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Create source archive.
    &quot;&quot;&quot;</span>
    setup<span style="color: black;">&#40;</span><span style="color: #483d8b;">'sdist'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> dist_eggs<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Create binary archives, one for each Python version.
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> version <span style="color: #ff7700;font-weight:bold;">in</span> config.<span style="color: black;">versions</span>:
        setup<span style="color: black;">&#40;</span><span style="color: #483d8b;">'bdist_egg'</span>, version<span style="color: black;">&#41;</span></pre></div></div>

<h3>(Bônus!) Automatizando geração de documentação e upload para o PyPI</h3>
<p>Ainda acha pouco? <a href="http://github.com/danielfm/django-flash/blob/master/fabfile.py">Este outro exemplo</a> de script vai além e mostra, numa aplicação real, como automatizar o registro e upload dos pacotes para o <a href="http://pypi.python.org/pypi/">PyPI</a>, atualizar site de documentação, entre outras coisas.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.destaquenet.com/2009/07/01/fabric-virtualenv-uma-combinacao-explosiva-no-bom-sentido/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
