<?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; script</title>
	<atom:link href="http://weblog.destaquenet.com/tag/script/feed/" rel="self" type="application/rss+xml" />
	<link>http://weblog.destaquenet.com</link>
	<description>Blog da equipe Destaquenet.</description>
	<lastBuildDate>Tue, 23 Nov 2010 17:06:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<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" style="font-family:monospace;"><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' /> </p>
<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" style="font-family:monospace;"><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' /> </p>
<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" style="font-family:monospace;"><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" style="font-family:monospace;"><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;</p>
<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>
	</channel>
</rss>

