Skip to content

Commit 417f7f9

Browse files
committed
removed rosetta/ and '' from possible random choices
1 parent bfa2550 commit 417f7f9

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

lib/routing.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,31 +109,48 @@ def _get_page_dict(self, query, topic_type, request_options=None):
109109
return self._adapter[topic_type]\
110110
.get_page_dict(query, request_options=request_options)
111111

112-
113-
def is_random_request(self,topic):
114-
print("Entrato in is_random_request con {}".format(topic))
112+
113+
def handle_if_random_request(self,topic):
114+
"""
115+
Return topic type for `topic` or "unknown" if topic can't be determined.
116+
"""
117+
118+
def __select_random_topic(stripped_topic,topic_list):
119+
#Here we remove the special pages if present
120+
if ":list" in topic_list: topic_list.remove(":list")
121+
if "rosetta/" in topic_list: topic_list.remove("rosetta/")
122+
random_topic = random.choice(topic_list)
123+
print("Il random_topic è {}".format(stripped_topic + random_topic))
124+
return stripped_topic + random_topic
125+
126+
print("Entrato in handle_if_random_request con {}".format(topic))
115127
if topic.endswith('/:random') or topic.lstrip('/') == ':random':
116128
#We strip the :random part and see if the query is valid by running a get_topics_list()
117-
topic = topic[:-8]
118-
topic_list = [x[len(topic):]
119-
for x in self.get_topics_list()
120-
if x.startswith(topic + "/")]
129+
130+
# Here we take the cheat.sh/x/ part
131+
stripped_topic = topic[:-7]
132+
133+
topic_list = [x[len(stripped_topic):]
134+
for x in self.get_topics_list()
135+
if x.startswith(stripped_topic)]
136+
137+
if '' in topic_list: topic_list.remove('')
121138
if topic_list:
122139
#This is a correct formatted query like /cpp/:random
123140
print("La richiesta random è giusta")
124141
print("Questa è la topic_list della richiesta :random = {}".format(topic_list))
125-
return True
126-
return False
127-
128-
def select_random_topic(self,topic):
129-
print("Entrato in select_random_topic con {}".format(topic))
130-
topic = topic[:-8]
131-
topic_list = [x[len(topic):]
132-
for x in self.get_topics_list()
133-
if x.startswith(topic + "/")]
134-
if "/:list" in topic_list: topic_list.remove("/:list")
135-
random_topic = topic + random.choice(topic_list)
136-
return random_topic
142+
random_topic = __select_random_topic(stripped_topic,topic_list)
143+
return random_topic
144+
else:
145+
#This is wrongly random formatted
146+
# It is not a valid random request, we just strip the /:random
147+
wrongly_formatted_random = topic[:-8]
148+
print("La richiesta random è sbagliata")
149+
print("Eseguo lo stripping = {}".format(wrongly_formatted_random))
150+
return wrongly_formatted_random
151+
#Here if not a random requst, we just forward the topic
152+
return topic
153+
137154

138155

139156
def get_answer_dict(self, topic, request_options=None):
@@ -146,9 +163,8 @@ def get_answer_dict(self, topic, request_options=None):
146163
Returns:
147164
answer_dict: the answer dictionary
148165
"""
149-
if self.is_random_request(topic):
150-
topic = self.select_random_topic(topic)
151-
166+
167+
topic = self.handle_if_random_request(topic)
152168
topic_type = self.get_topic_type(topic)
153169

154170
# 'question' queries are pretty expensive, that's why they should be handled

0 commit comments

Comments
 (0)