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
44 def parse(output_string
, grouping
=False, sort_key
=None):
48 for id, line
in enumerate(output_string
):
56 This is a super weird hack. When we group by country or AS, the
57 nickname is replaced with '(x relays)' which when split() creates
58 ['(x','relays)']. I need to join this again and then left shift all
59 the elements and delete the last element in the list.
62 values
[6] = "%s %s" % (values
[6], values
[7])
63 for id in xrange(8, len(values
)):
64 values
[id-1] = values
[id]
67 # TODO: change inaccurate value of 10
70 result
.adv_bw
= values
[1]
71 result
.p_guard
= values
[2]
72 result
.p_middle
= values
[3]
73 result
.p_exit
= values
[4]
74 result
.nick
= values
[5]
76 result
.exit
= values
[7]
77 result
.guard
= values
[8]
79 result
.as_no
= values
[10]
80 result
.as_name
= ' '.join(values
[11:])
81 result
.as_name
= re
.sub(r
'\([^)]*\)', '', result
.as_name
)
82 result
.as_info
= "%s %s" % (result
.as_no
, result
.as_name
)
85 key
= float(getattr(result
, sort_key
)[:-1])
86 if sorted_results
.has_key(key
):
87 sorted_results
[key
].append(result
)
89 sorted_results
[key
] = [result
]
91 results
.append(result
)
92 return results
if results
else sorted_results
96 return render_template('index.html')
98 @app.route('/result', methods
=['GET'])
104 for key
, value
in request
.args
.items():
107 options
.top
= int(value
)
112 elif key
in ["country", "ases"]:
114 setattr(options
, key
, [value
])
116 setattr(options
, key
, None)
118 setattr(options
, value
, True)
120 setattr(options
, key
, value
)
122 stats
= compass
.RelayStats(options
)
123 sorted_groups
= stats
.format_and_sort_groups(stats
.relays
,
124 by_country
=options
.by_country
,
125 by_as_number
=options
.by_as
,
127 output_string
= stats
.print_groups(sorted_groups
, options
.top
,
128 by_country
=options
.by_country
,
129 by_as_number
=options
.by_as
,
132 results
= parse(output_string
, options
.by_country
or options
.by_as
, sort_key
)
135 for key
in sorted(results
.iterkeys(), reverse
=True):
136 for value
in results
[key
]:
141 return render_template('result.html', results
=relays
)
143 if __name__
== '__main__':
144 # Bind to PORT if defined, otherwise default to 5000.
145 port
= int(os
.environ
.get('PORT', 5000))
146 app
.run(host
='0.0.0.0', port
=port
, debug
=True)