| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
| 3 # Copyright 2017 The Chromium Authors. All rights reserved. | 3 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Fetches articles from the server. | 7 """Fetches articles from the server. |
| 8 | 8 |
| 9 Examples: | 9 Examples: |
| 10 $ fetch.py # unauthenticated, no experiments | 10 $ fetch.py # unauthenticated, no experiments |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 creds = storage.get() | 188 creds = storage.get() |
| 189 if not creds or creds.invalid or creds.access_token_expired: | 189 if not creds or creds.invalid or creds.access_token_expired: |
| 190 flow = oauth2client.client.OAuth2WebServerFlow( | 190 flow = oauth2client.client.OAuth2WebServerFlow( |
| 191 client_id=client_id, client_secret=client_secret, | 191 client_id=client_id, client_secret=client_secret, |
| 192 scope=API_SCOPE) | 192 scope=API_SCOPE) |
| 193 oauth2client.tools.run_flow(flow, storage, args) | 193 oauth2client.tools.run_flow(flow, storage, args) |
| 194 creds = storage.get() | 194 creds = storage.get() |
| 195 creds.apply(headers) | 195 creds.apply(headers) |
| 196 | 196 |
| 197 | 197 |
| 198 def PrintShortResponse(r): | 198 def PrintShortResponse(j): |
| 199 now = datetime.datetime.now() | 199 now = datetime.datetime.now() |
| 200 for category in r.json()["categories"]: | 200 for category in j["categories"]: |
| 201 print("%s: " % category["localizedTitle"]) | 201 print("%s: " % category["localizedTitle"]) |
| 202 for suggestion in category["suggestions"]: | 202 for suggestion in category["suggestions"]: |
| 203 attribution = suggestion["attribution"] | 203 attribution = suggestion["attribution"] |
| 204 title = suggestion["title"] | 204 title = suggestion["title"] |
| 205 full_url = suggestion["fullPageUrl"] | 205 full_url = suggestion["fullPageUrl"] |
| 206 amp_url = suggestion.get("ampUrl") | 206 amp_url = suggestion.get("ampUrl") |
| 207 creation_time = suggestion["creationTime"] | 207 creation_time = suggestion["creationTime"] |
| 208 | 208 |
| 209 if len(title) > 40: | 209 if len(title) > 40: |
| 210 title = textwrap.wrap(title, 40)[0] + "…" | 210 title = textwrap.wrap(title, 40)[0] + "…" |
| 211 creation_time = ParseDateTime(creation_time) | 211 creation_time = ParseDateTime(creation_time) |
| 212 age = AbbreviateDuration(now - creation_time) | 212 age = AbbreviateDuration(now - creation_time) |
| 213 | 213 |
| 214 print(" “%s” (%s, %s ago)" % (title, attribution, age)) | 214 print(" “%s” (%s, %s ago)" % (title, attribution, age)) |
| 215 print(" " + (amp_url or full_url)) | 215 print(" " + (amp_url or full_url)) |
| 216 if category["allowFetchingMoreResults"]: | 216 if category["allowFetchingMoreResults"]: |
| 217 print(" [More]") | 217 print(" [More]") |
| 218 | 218 |
| 219 | 219 |
| 220 def ParseDateTime(creation_time): | 220 def ParseDateTime(creation_time): |
| 221 try: | 221 try: |
| 222 return datetime.datetime.strptime(creation_time, "%Y-%m-%dT%H:%M:%SZ") | 222 return datetime.datetime.strptime(creation_time, "%Y-%m-%dT%H:%M:%SZ") |
| 223 except ValueError: | 223 except ValueError: |
| 224 return datetime.datetime.strptime(creation_time, "%Y-%m-%dT%H:%M:%S.%fZ") | 224 return datetime.datetime.strptime(creation_time, "%Y-%m-%dT%H:%M:%S.%fZ") |
| 225 | 225 |
| 226 | 226 |
| 227 if __name__ == "__main__": | 227 if __name__ == "__main__": |
| 228 main() | 228 main() |
| OLD | NEW |