-
Notifications
You must be signed in to change notification settings - Fork 0
/
jornal_da_ciencia_impresso.py
198 lines (142 loc) · 5.18 KB
/
jornal_da_ciencia_impresso.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import csv
import subprocess
import pickle
import requests
import regex
import click
import copy
import pyperclip
import lxml.html
from pynput import keyboard
import csv_utf8
import helpers
logger = helpers.get_logger()
class Jornal_da_ciencia_impresso():
def __init__(self):
# tabela.append([edicao, titulo + ': ' + subtitulo,
# publicacao, link, tags])
# tabela2.append([edicao, titulo, subtitulo, publicacao, link, tags])
self.publicacao = u'Jornal da Ciencia Impresso'
self.url = u'http://jcnoticias.jornaldaciencia.org.br/'
self.tabela = []
self.tabela2 = []
def extract_edicoes(self):
url = self.url + u'category/pdf/page/'
k = 1
while True:
res = requests.get(url + unicode(k))
root = lxml.html.fromstring(res.content)
edicoes = root.xpath(u'//div[@class="pdf-edicao"]')
for edicao in edicoes:
link_c = edicao.xpath(u'.//div[@class="pdf-titulo"]/a')[0]
print link_c.text
print link_c.get(u'href')
res = self.extract_from_edicao(link_c.text, link_c.get(u'href'))
for res1 in res:
self.tabela.append([
res1['edicao'],
res1['titulo'].strip(),
res1.get(u'subtitulo', u'').strip(),
u'Jornal da Ciência Impresso',
res1[u'link'],
res1.get(u'tags', u'')])
title_and_sub = regex.sub(
ur'\s+', u' ',
res1['titulo'].strip() + u': ' + res1.get(u'subtitulo', u'').strip(),
flags=regex.V1 | regex.I)
title_and_sub = regex.sub(
ur'(: $|- )',
u'',
title_and_sub,
flags=regex.V1 | regex.I)
self.tabela2.append([
res1['edicao'],
title_and_sub,
u'Jornal da Ciência Impresso',
res1[u'link'],
regex.sub(
ur'\s+', u' ',
res1.get(u'tags', u''),
flags=regex.V1 | regex.I)])
if len(edicoes):
k += 1
else:
break
def extract_from_edicao(self, titulo, link):
filename = os.path.join(
u'./data/jornal_da_ciencia_impressos', link.split('/')[-1])
if not os.path.exists(filename):
with open(filename, 'wb') as filex:
filex.write(requests.get(link).content)
if os.path.exists(filename + '.p'):
res2 = pickle.load(open(filename + '.p', 'rb'))
for res1 in res2:
res1[u'link'] = link
return res2
# raw_input()
return
p = subprocess.Popen(
[u'evince', filename],
stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'))
global edicao
global link_e
global res
global semi_res
link_e = link
edicao = titulo
res = []
semi_res = None
def on_release(key):
global edicao
global link_e
global res
global semi_res
try:
key = key.char
except:
pass
if key == u'q':
if semi_res is not None:
res.append(copy.copy(semi_res))
semi_res = {
u'edicao': edicao,
u'titulo': pyperclip.paste(),
u'link': link_e}
if key == u'w':
semi_res['subtitulo'] = pyperclip.paste()
if key == u'e':
semi_res['tags'] = pyperclip.paste()
if key == keyboard.Key.esc:
# Stop listener
return False
# Collect events until released
with keyboard.Listener(on_release=on_release) as listener:
listener.join()
p.terminate()
with open(filename + '.p', 'wb') as fpickle:
pickle.dump(res, fpickle)
return res
def extrai_salva(self):
self.extract_edicoes()
with open('./data/%s-%s.csv' % (
self.publicacao, time.strftime(u'%Y-%m-%d')), 'wb') as myfile:
wr = csv_utf8.UnicodeWriter(myfile, quoting=csv.QUOTE_ALL)
wr.writerows(self.tabela)
with open('./data/%s-%s-ajustado.csv' % (
self.publicacao, time.strftime(u'%Y-%m-%d')), 'wb') as myfile:
wr = csv_utf8.UnicodeWriter(myfile, quoting=csv.QUOTE_ALL)
wr.writerows(self.tabela2)
@click.group()
def cli():
pass
@click.option('--inicio', help='Pagina de inicio', default=1)
@cli.command()
def extrai(inicio):
jornal_da_ciencia = Jornal_da_ciencia_impresso()
jornal_da_ciencia.extrai_salva()
if __name__ == '__main__':
cli()