Automatizando tarefas com Python

29
Lightning Talk Using Python to Automate Repetitive Tasks André Ericson Novembro/2011, Pug-Pe

Transcript of Automatizando tarefas com Python

Page 1: Automatizando tarefas com Python

Lightning TalkUsing Python to Automate Repetitive Tasks

André EricsonNovembro/2011, Pug-Pe

Page 2: Automatizando tarefas com Python

Using Python to AutomateRepetitive TasksRoteiro

• Por que py?• Case of Study 1

• Motivação• Objetivos• Ferramentas

• Case of Study 2• Motivação• Objetivos• Resultados• Ferramentas

• Code• Bonus

Page 3: Automatizando tarefas com Python

Por que Python?

• Simplicidade• Legibilidade do Código• Tempo• Batteries Included• PyPI(17484 packages)

• Não é Java

Page 4: Automatizando tarefas com Python

Por que Python?

• Simplicidade• Legibilidade do Código• Tempo• Batteries Included• PyPI(17484 packages)• Não é Java

Page 5: Automatizando tarefas com Python

Case of Study: Unicap_br

Page 6: Automatizando tarefas com Python

Motivação

Page 7: Automatizando tarefas com Python

Unicap-brObjetivos

• Fazer o login no site da católica• Receber dados dos livros emprestados• Renovar livros• Checar sucesso da renovação• Renovar automaticamente com frequência Crontab

Page 8: Automatizando tarefas com Python

Unicap-brObjetivos

• Fazer o login no site da católica• Receber dados dos livros emprestados• Renovar livros• Checar sucesso da renovação• Renovar automaticamente com frequência Crontab

Page 9: Automatizando tarefas com Python

Unicap-brObjetivos

• Fazer o login no site da católica• Receber dados dos livros emprestados• Renovar livros• Checar sucesso da renovação• Renovar automaticamente com frequência Crontab

Page 10: Automatizando tarefas com Python

Unicap-brObjetivos

• Fazer o login no site da católica• Receber dados dos livros emprestados• Renovar livros• Checar sucesso da renovação• Renovar automaticamente com frequência Crontab

Page 11: Automatizando tarefas com Python

Unicap-brObjetivos

• Fazer o login no site da católica• Receber dados dos livros emprestados• Renovar livros• Checar sucesso da renovação• Renovar automaticamente com frequência Crontab

Page 12: Automatizando tarefas com Python

Unicap-brFerramentas

• BeautifulSoup: HTML/XML parser designed for quick turnaroundprojects like screen-scraping

• Mechanize: Stateful programmatic web browsing in Python

Page 13: Automatizando tarefas com Python

Unicap-brFerramentas

• BeautifulSoup: HTML/XML parser designed for quick turnaroundprojects like screen-scraping

• Mechanize: Stateful programmatic web browsing in Python

Page 14: Automatizando tarefas com Python

Case of Study: CheckSubs

Page 15: Automatizando tarefas com Python

Motivação

Page 16: Automatizando tarefas com Python

Objetivo

• Checar legendas novas para determinadas séries de TV• Notificar quando uma nova legenda estiver disponível.

Page 17: Automatizando tarefas com Python

Objetivo

• Checar legendas novas para determinadas séries de TV• Notificar quando uma nova legenda estiver disponível.

Page 18: Automatizando tarefas com Python

Resultado

Page 19: Automatizando tarefas com Python

Ferramentas

• Python-twitter: A Python wrapper around the Twitter API• bitlyapi: A very thin wrapper for the bit.ly API• pynotify: Python bindings for libnotify(Desktop Notification Library)• feedparser: Parse Atom and RSS feeds in Python

Page 20: Automatizando tarefas com Python

Ferramentas

• Python-twitter: A Python wrapper around the Twitter API• bitlyapi: A very thin wrapper for the bit.ly API• pynotify: Python bindings for libnotify(Desktop Notification Library)• feedparser: Parse Atom and RSS feeds in Python

Page 21: Automatizando tarefas com Python

Ferramentas

• Python-twitter: A Python wrapper around the Twitter API• bitlyapi: A very thin wrapper for the bit.ly API• pynotify: Python bindings for libnotify(Desktop Notification Library)• feedparser: Parse Atom and RSS feeds in Python

Page 22: Automatizando tarefas com Python

Ferramentas

• Python-twitter: A Python wrapper around the Twitter API• bitlyapi: A very thin wrapper for the bit.ly API• pynotify: Python bindings for libnotify(Desktop Notification Library)• feedparser: Parse Atom and RSS feeds in Python

Page 23: Automatizando tarefas com Python

Talk is cheap.Showme the Code.

Page 24: Automatizando tarefas com Python

BeautifulSoup

1 <tr class=’rel2’>2 <td nowrap><input type=’checkbox’3 name=’check_2’ value=’99236167@#1’>4 </td>5 <td nowrap>99236167</td>6 <td nowrap>7 Introducao a administracao:8 edicao compacta.</td>9 <td nowrap>&nbsp;</td>10 <td nowrap>Livros</td>11 <td nowrap>25/10/2011</td>12 <td nowrap>09/11/2011</td>13 <td nowrap>&nbsp;</td>14 <td nowrap>&nbsp;</td>15 <td nowrap>Normal</td>16 <td nowrap>Biblioteca Central</td>17 <td nowrap>0</td>18 <td nowrap>15</td>19 </tr>

1 soup = BeautifulSoup(request)2 books = soup.findAll(’tr’, attrs={’class’:3 re.compile(’rel.*’)})4 return [Book(book) for book in books]5 ############################################6 class Book(object):7 def __init__(self, soup_tag):8 tds = soup_tag.findAll(’td’)9 self.check = str(tds[0].find(’input’)10 [’name’])11 self.title = tds[2].string.strip()

Page 25: Automatizando tarefas com Python

Mechanize

1 <form ... name="form1" ... >2 <input type="text" name="login">3 <input type=’password’ name=’password’>4 </form>

1 browser = mechanize.Browser()2 cj = cookielib.LWPCookieJar()3 browser.set_cookiejar(cj)4 browser.open(LOGIN_PAGE)5 browser.select_form(name=’form1’)6 browser.form[’login’] = login7 browser.form[’password’] = password8 r = browser.submit()

Page 26: Automatizando tarefas com Python

Android(SL4A)

Page 27: Automatizando tarefas com Python

Android(SL4A)

Page 28: Automatizando tarefas com Python

Tasker

Page 29: Automatizando tarefas com Python

Dúvidas?

André Ericsonhttp://www.github.com/aericson

[email protected]@_aericson