-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfoto.py
62 lines (46 loc) · 2.76 KB
/
foto.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Autore: Cristian Consonni <[email protected]>
#
# The code is released with an MIT license
# please see the LICENSE file for details.
import os
import requests
TEXT_MALE = '''{nome} {cognome}, politico italiano'''
TEXT_FEMALE = '''{nome} {cognome}, politica italiana'''
if __name__ == '__main__':
url = 'http://dati.camera.it/sparql?default-graph-uri=&query=SELECT+DISTINCT+%3Fpersona+%3Fcognome+%3Fnome+%0D%0A%3FdataNascita++%3Fnato+%3FluogoNascita+%3Fgenere+%3Ffoto%0D%0A++%0D%0AWHERE+{%0D%0A%3Fpersona+ocd%3Arif_mandatoCamera+%3Fmandato%3B+a+foaf%3APerson.%0D%0A%0D%0A%23%23+deputato%0D%0A%3Fd+a+ocd%3Adeputato%3B+ocd%3Aaderisce+%3Faderisce%3B%0D%0Aocd%3Arif_leg+%3Chttp%3A%2F%2Fdati.camera.it%2Focd%2Flegislatura.rdf%2Frepubblica_17%3E%3B%0D%0Aocd%3Arif_mandatoCamera+%3Fmandato.%0D%0A%0D%0A%23%23anagrafica%0D%0A%3Fd+foaf%3Asurname+%3Fcognome%3B+foaf%3Agender+%3Fgenere%3Bfoaf%3AfirstName+%3Fnome%3B+foaf%3Adepiction+%3Ffoto.%0D%0AOPTIONAL{%0D%0A%3Fpersona+%3Chttp%3A%2F%2Fpurl.org%2Fvocab%2Fbio%2F0.1%2FBirth%3E+%3Fnascita.%0D%0A%3Fnascita+%3Chttp%3A%2F%2Fpurl.org%2Fvocab%2Fbio%2F0.1%2Fdate%3E+%3FdataNascita%3B+%0D%0Ardfs%3Alabel+%3Fnato%3B+ocd%3Arif_luogo+%3FluogoNascitaUri.+%0D%0A%3FluogoNascitaUri+dc%3Atitle+%3FluogoNascita.+%0D%0A}%0D%0A%0D%0A}&format=text%2Fhtml&timeout=0&debug=on&format=json'
res = requests.get(url)
if res.ok:
data = res.json()
for person in data['results']['bindings']:
identifier = person['persona']['value'].split('/')[-1]
data_nascita = person['dataNascita']['value']
nome = person['nome']['value']
cognome = person['cognome']['value']
foto_url = person['foto']['value']
genere = person['genere']['value']
print 'Processing {nome} {cognome} ...'.format(
nome=nome,
cognome=cognome), 'requesting: ', foto_url
filename = nome + '_' + cognome + '_' + 'daticamera.jpg'
filename = os.path.join('foto', filename)
# with open(filename, 'wb') as handle:
# response = requests.get(foto_url, stream=True)
# if not response.ok:
# # Something went wrong
# continue
# for block in response.iter_content(1024):
# if not block:
# break
# handle.write(block)
txt_filename = nome + '_' + cognome + '_' + 'daticamera.txt'
txt_filename = os.path.join('testi', txt_filename)
if genere == 'male':
text = TEXT_MALE.format(nome=nome.title(), cognome=cognome.title())
else:
text = TEXT_FEMALE.format(nome=nome.title(), cognome=cognome.title())
with open(txt_filename, 'w+') as output:
output.write(text)
exit(0)