-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocfinder.py
76 lines (59 loc) · 2.32 KB
/
docfinder.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
"""Script to find doctor availability in apss trentino website."""
import sys
import requests
from bs4 import BeautifulSoup
base_url = "http://89.190.163.118/Pubblico/RicercaMedici/RicercaPerNomeMedico.rails"
search_url_base = "http://89.190.163.118/%s/Pubblico/RicercaMedici/RicercaPerNomeMedico.rails"
status_url_base = "http://89.190.163.118/%s/Pubblico/RicercaMedici/%s"
def get_cookie():
"""Get cookie from the site"""
response = requests.get(base_url)
cookie = response.url.split('/')[3]
return cookie
def search_doctor(name, cookie):
"""Search list of doctors with that name"""
query = {'richiesta.TipoRicerca': '1',
'richiesta.TipoMedico': '1',
'richiesta.NominativoMedico': name}
search_url = search_url_base % cookie
response = requests.post(search_url, data=query)
soup = BeautifulSoup(response.text)
doctor_list = soup.findAll('a', {'class': 'medico'})
return doctor_list
def get_doctor_details(medico, cookie):
"""Get doctor details - not finished"""
detail_link = medico.find('a').get('href')
detail_link = status_url_base % (cookie, detail_link)
response = requests.get(detail_link)
soup = BeautifulSoup(response.text)
table = soup.find('table')
hours = [td for td in table.findAll('td')]
i=0
for th in table.findAll('th'):
print th.string + " %s" % hours[i]
i+=1
def get_doctor_status(lista, cookie, details=False):
"""Search status"""
for link in lista:
full_link = status_url_base % (cookie, link.get('href'))
response = requests.get(full_link)
soup = BeautifulSoup(response.text)
medico = soup.find('div', {'class': 'medico-choice'})
if details:
get_doctor_details(medico, cookie)
for td in medico.findAll('td'):
if td.has_attr('class') and 'disponibilita' in td.get('class')[0]:
return td.string
def main():
"""Main entry point for the script."""
if len((sys.argv)) == 1:
print "Missing name! A name is required"
return 0
# Get cookie from the web
cookie = get_cookie()
name = sys.argv[1:][0]
lista = search_doctor(name, cookie)
status = get_doctor_status(lista, cookie, details=False)
print "Status %s: %s" % (name, status)
if __name__ == '__main__':
sys.exit(main())