Endpoint which returns a list of shortest paths between two Wikipedia pages. Args: source: The title of the page at which to start the search. target: The title of the page at which to end the search. Returns: dict: A JSON-ified dictionary containing the shortest paths (r
()
| 82 | |
| 83 | @app.route('/paths', methods=['POST']) |
| 84 | def shortest_paths_route(): |
| 85 | """Endpoint which returns a list of shortest paths between two Wikipedia pages. |
| 86 | |
| 87 | Args: |
| 88 | source: The title of the page at which to start the search. |
| 89 | target: The title of the page at which to end the search. |
| 90 | |
| 91 | Returns: |
| 92 | dict: A JSON-ified dictionary containing the shortest paths (represented by a list of lists of |
| 93 | page IDs) and the corresponding pages data (represented by a dictionary of page IDs). |
| 94 | |
| 95 | Raises: |
| 96 | InvalidRequest: If either of the provided titles correspond to pages which do not exist. |
| 97 | """ |
| 98 | start_time = time.time() |
| 99 | |
| 100 | # Look up the IDs for each page. |
| 101 | try: |
| 102 | (source_page_id, source_page_title, |
| 103 | is_source_redirected) = database.fetch_page(request.json['source']) |
| 104 | except ValueError: |
| 105 | raise InvalidRequest( |
| 106 | 'Start page "{0}" does not exist. Please try another search.'.format(request.json['source'])) |
| 107 | |
| 108 | try: |
| 109 | (target_page_id, target_page_title, |
| 110 | is_target_redirected) = database.fetch_page(request.json['target']) |
| 111 | except ValueError: |
| 112 | raise InvalidRequest( |
| 113 | 'End page "{0}" does not exist. Please try another search.'.format(request.json['target'])) |
| 114 | |
| 115 | # Compute the shortest paths. |
| 116 | paths = database.compute_shortest_paths(source_page_id, target_page_id) |
| 117 | |
| 118 | response = { |
| 119 | 'sourcePageTitle': source_page_title, |
| 120 | 'targetPageTitle': target_page_title, |
| 121 | 'isSourceRedirected': is_source_redirected, |
| 122 | 'isTargetRedirected': is_target_redirected, |
| 123 | } |
| 124 | |
| 125 | # No paths found. |
| 126 | if len(paths) == 0: |
| 127 | logging.info('No paths found from {0} to {1}'.format(source_page_id, target_page_id)) |
| 128 | response['paths'] = [] |
| 129 | response['pages'] = [] |
| 130 | # Paths found |
| 131 | else: |
| 132 | # Get a list of all IDs. |
| 133 | page_ids_set = set() |
| 134 | for path in paths: |
| 135 | for page_id in path: |
| 136 | page_ids_set.add(str(page_id)) |
| 137 | |
| 138 | response['paths'] = paths |
| 139 | response['pages'] = fetch_wikipedia_pages_info(list(page_ids_set), database) |
| 140 | |
| 141 |
nothing calls this directly
no test coverage detected