4 from flask
import Flask
, request
, redirect
, render_template
, url_for
12 self
.by_country
= False
18 self
.guards_only
= None
20 self
.almost_fast_exits_only
= None
21 self
.exits_only
= False
23 self
.fast_exits_only
= False
24 self
.fast_exits_only_any_network
= False
25 self
.all_relays
= False
43 def parse(output_string
, grouping
=False, sort_key
=None):
47 for id, line
in enumerate(output_string
):
55 This is a super weird hack. When we group by country or AS, the
56 nickname is replaced with '(x relays)' which when split() creates
57 ['(x','relays)']. I need to join this again and then left shift all
58 the elements and delete the last element in the list.
61 values
[6] = "%s %s" % (values
[6], values
[7])
62 for id in xrange(8, len(values
)):
63 values
[id-1] = values
[id]
66 # TODO: change inaccurate value of 10
69 result
.adv_bw
= values
[1]
70 result
.p_guard
= values
[2]
71 result
.p_middle
= values
[3]
72 result
.p_exit
= values
[4]
73 result
.nick
= values
[5]
75 result
.exit
= values
[7]
76 result
.guard
= values
[8]
78 result
.as_no
= values
[10]
79 result
.as_name
= ' '.join(values
[11:])
80 result
.as_name
= re
.sub(r
'\([^)]*\)', '', result
.as_name
)
83 key
= float(getattr(result
, sort_key
)[:-1])
84 if sorted_results
.has_key(key
):
85 sorted_results
[key
].append(result
)
87 sorted_results
[key
] = [result
]
89 results
.append(result
)
90 return results
if results
else sorted_results
94 return render_template('index.html')
96 @app.route('/result', methods
=['GET'])
102 for key
, value
in request
.args
.items():
105 options
.top
= int(value
)
110 elif key
in ["country", "ases"]:
112 setattr(options
, key
, [value
])
114 setattr(options
, key
, None)
116 setattr(options
, value
, True)
118 setattr(options
, key
, value
)
120 stats
= compass
.RelayStats(options
)
121 sorted_groups
= stats
.format_and_sort_groups(stats
.relays
,
122 by_country
=options
.by_country
,
123 by_as_number
=options
.by_as
,
125 output_string
= stats
.print_groups(sorted_groups
, options
.top
,
126 by_country
=options
.by_country
,
127 by_as_number
=options
.by_as
,
130 results
= parse(output_string
, options
.by_country
or options
.by_as
, sort_key
)
133 for key
in sorted(results
.iterkeys(), reverse
=True):
134 for value
in results
[key
]:
139 return render_template('result.html', results
=relays
)
141 if __name__
== '__main__':
142 # Bind to PORT if defined, otherwise default to 5000.
143 port
= int(os
.environ
.get('PORT', 5000))
144 app
.run(host
='0.0.0.0', port
=port
, debug
=True)