My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# ----------------------------------------------------------------------------
# Leitor de Feeds RSS e Atom.
# Se informar a URL de um feed, são mostradas suas últimas notícias.
# Se informar a URL de um site, mostra a URL do(s) Feed(s).
# Obs.: Use a opção -n para limitar o número de resultados (Padrão é 10).
#
# Uso: zzfeed [-n número] URL...
# Ex.: zzfeed http://aurelio.net/feed/
# zzfeed -n 5 aurelio.net/feed/ # O http:// é opcional
# zzfeed aurelio.net funcoeszz.net # Mostra URL dos feeds
#
# Autor: Aurelio Marinho Jargas, www.aurelio.net
# Desde: 2011-05-03
# Versão: 1
# Licença: GPL
# Requisitos: zzxml zzunescape
# ----------------------------------------------------------------------------
zzfeed ()
{
zzzz -h feed "$1" && return

local url formato tag_mae
local limite=10
local tmp="$ZZTMP.feed.$$"

# Opções de linha de comando
if test "$1" = '-n'
then
limite=$2
shift
shift
fi

# Verificação dos parâmetros
[ "$1" ] || { zztool uso feed; return 1; }

# Verificação básica
if ! zztool testa_numero "$limite"
then
echo "Número inválido para a opção -n: $limite"
return 1
fi

#-----------------------------------------------------------------
# ATOM:
# <?xml version="1.0" encoding="utf-8"?>
# <feed xmlns="http://www.w3.org/2005/Atom">
# <title>Example Feed</title>
# <subtitle>A subtitle.</subtitle>
# <link href="http://example.org/" />
# ...
# <entry>
# <title>Atom-Powered Robots Run Amok</title>
# <link href="http://example.org/2003/12/13/atom03" />
# ...
# </entry>
# </feed>
#-----------------------------------------------------------------
# RSS:
# <?xml version="1.0" encoding="UTF-8" ?>
# <rss version="2.0">
# <channel>
# <title>RSS Title</title>
# <description>This is an example of an RSS feed</description>
# <link>http://www.someexamplerssdomain.com/main.html</link>
# ...
# <item>
# <title>Example entry</title>
# <link>http://www.wikipedia.org/</link>
# ...
# </item>
# </channel>
# </rss>
#-----------------------------------------------------------------

# Para cada URL que o usuário informou...
for url
do
# Só mostra a url se houver mais de uma
[ $# -gt 1 ] && zztool eco "* $url"

# Baixa e limpa o conteúdo do feed
$ZZWWWHTML "$url" | zzxml --tidy > "$tmp"

# Tenta identificar o formato: <feed> é Atom, <rss> é RSS
formato=$(grep -e '^<feed[ >]' -e '^<rss[ >]' "$tmp")

# Afinal, isso é um feed ou não?
if test -n "$formato"
then
### É um feed, vamos mostrar as últimas notícias.
# Atom ou RSS, as manchetes estão sempre na tag <title>,
# que por sua vez está dentro de <item> ou <entry>.

if zztool grep_var '<rss' "$formato"
then
tag_mae='item'
else
tag_mae='entry'
fi

# Extrai as tags <title> e formata o resultado
cat "$tmp" |
zzxml --tag $tag_mae |
zzxml --tag 'title' --untag |
sed "$limite q" |
zzunescape --html |
zztool trim
else
### Não é um feed, pode ser um site normal.
# Vamos tentar descobrir o endereço do(s) Feed(s).
# <link rel="alternate" type="application/rss+xml" href="http://...">

cat "$tmp" |
grep -i \
-e '^<link .*application/rss+xml' \
-e '^<link .*application/atom+xml' |
# Se não tiver href= não vale (o site do Terra é um exemplo)
grep -i 'href=' |
# Extrai a URL, apagando o que tem ao redor
sed "
s/.*[Hh][Rr][Ee][Ff]=//
s/[ >].*//
s/['\"]//g"
fi

rm -f "$tmp"

# Linha em branco para separar resultados
[ $# -gt 1 ] && echo

# Tem mais de um site pra procurar?
continue
done
}

Change log

r710 by aureliojargas on Mar 29, 2012   Diff
SEGURANÇA: $1 colocado entre aspas na
chamada padrão zzzz -h de todas as
funções.
Go to: 
Sign in to write a code review

Older revisions

r655 by aureliojargas on Mar 23, 2012   Diff
s/Aurélio/Aurelio/ *
r524 by aureliojargas on May 23, 2011   Diff
COMBO: Agora todas as chamadas ao
zztool uso por falha na verificação
dos parâmetros possuem um return 1
depois. Assim outros scripts que usam
as funções podem detectar erros.
r477 by aureliojargas on May 20, 2011   Diff
COMBO:
Arrumado o return de várias funções.
Agora usando return 1 quando algum
erro ocorre e return 0 quando é uma
saída OK. Outros casos onde pode haver
...
All revisions of this file

File info

Size: 3724 bytes, 135 lines
Powered by Google Project Hosting