-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdf.py
166 lines (138 loc) · 4.59 KB
/
tdf.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
# -*- encoding: utf-8 -*-
'''
this module is used to make a web site
to give information about the Tour de France
of your birth year
'''
import sys
import requests
from bs4 import BeautifulSoup
import wikipedia
from bottle import Bottle, run
from bottle import template
from bottle import request, static_file
wikipedia.set_lang("fr")
APP = Bottle()
def scrapping_wp():
'''
This function is scrapping the wikipedia page
of le Tour de France
where the palmares is.
'''
url = 'https://fr.wikipedia.org/wiki/Palmar%C3%A8s_du_Tour_de_France'
# Scrape the HTML at the url
req = requests.get(url)
# Turn the HTML into a Beautiful Soup object
soup = BeautifulSoup(req.text, 'lxml')
# Create an object of the first object that is class=dataframe
table = soup.find(class_='wikitable sortable')
return table
@APP.route("/credits")
def display_credits():
'''
display the credits page with the good template
'''
return template('static/templated-retrospect/credits.tpl')
@APP.route('/static/:path#.+#', name='static')
def static(path):
'''
return the static file
'''
return static_file(path, root='static')
@APP.get('/')
def tdf():
'''
principal app to launch tdf
'''
htm = '''
<form value="form" action="/" method="post">
<div id='container'>
<label for="year">Renseignez l'année de votre naissance</label>
<p><select name="year" id="year">'''
table = scrapping_wp()
for single in table.find_all('tr')[1:]:
col = single.find_all('td')
column_1 = col[0]
col1 = column_1.text.split(" ")
anneecol = col1[0]
htmopt = '<option value="{0}">{1}</option>'.format(anneecol, anneecol)
htm = htm + htmopt
htm = htm + ('''</select></div>
<input type="submit" value="Envoyer"''' +
''' class="button big special">
</form>
<p>Votre année de naissance n'est pas dans la liste ?''' +
'''C'est qu'il n'y a pas eu de Tour de France cette année là. Désolé.</p>''')
return template('static/templated-retrospect/index.tpl',
title="Le Tour de votre naissance", body=htm)
@APP.post('/')
def do_tdf():
'''
create the tdf app
'''
year = request.forms.get('year')
htm_yellow = ''
htm_mountain = ''
htm_green = ''
table = scrapping_wp()
for single in table.find_all('tr')[1:]:
col = single.find_all('td')
col1 = col[0]
col1 = col1.text.split(" ")
anneecol = col1[0]
yellow = col[1].text
mountain = col[8].text
green = col[9].text
if year == anneecol:
if 'non attribu' in yellow:
htm_yellow = (htm_yellow +
"<h1>En {0} </h1><p>".format(year) +
"Le maillot jaune n'a pas été attribué cette année là.</p>" +
"<p>Les titres de Lance Armstrong gagné entre 1999 et 2005" +
" ont été révoqués par l'UCI le 22 octobre 2012 pour dopage.</p>")
else:
sentence = wiki_request(yellow)
htm_yellow = (htm_yellow +
"<h1>En {0} </h1><h2>le vainqueur du tour était {1}.</h2><p>{2}</p>".
format(year, yellow, sentence))
if 'non attribu' in mountain or mountain == '':
htm_mountain = (htm_mountain +
"<p>Le grand prix de la Montagne n'a pas été attribué cette année là.</p>")
else:
sentence = wiki_request(mountain)
htm_mountain = (htm_mountain +
"<h2>Le meilleur grimpeur était {0}.</h2><p>{1}</p>".
format(mountain, sentence))
if 'non attribu' in green or green == '':
htm_green = (htm_green +
"<p>Le prix du meilleur sprinter n'a pas été" +
" attribué cette année là.</p>")
else:
sentence = wiki_request(green)
htm_green = (htm_green +
"<h2>Le meilleur sprinter était {0}.</h2><p>{1}</p>".
format(green, sentence))
title = "Le Tour de votre naissance: {0}".format(year)
return template('static/templated-retrospect/result.tpl',
title=title, bodyYellow=htm_yellow,
bodyMountain=htm_mountain,
bodyGreen=htm_green)
return "Vous êtes né en ", year
def wiki_request(name):
'''
This method will return the 2 first sentences of the wikipedia page
of the given name
'''
try:
name_wpp = wikipedia.page(name)
name_wpp = name_wpp.title
sentence = wikipedia.summary(name_wpp, sentences=2)
except wikipedia.exceptions.DisambiguationError:
name_wpp = wikipedia.page(name + ' cyclisme')
name_wpp = name_wpp.title
sentence = wikipedia.summary(name_wpp, sentences=2)
except wikipedia.exceptions.PageError:
sentence = "Pas de fiche WP"
return sentence
run(APP, host='0.0.0.0', port=sys.argv[1])
#run(APP, host='0.0.0.0')