Chromium Code Reviews| Index: components/ntp_tiles/update_default_sites_resources.py |
| diff --git a/components/ntp_tiles/update_default_sites_resources.py b/components/ntp_tiles/update_default_sites_resources.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..a000a3b3aa5827626460512ed3face6c3ab4ad42 |
| --- /dev/null |
| +++ b/components/ntp_tiles/update_default_sites_resources.py |
| @@ -0,0 +1,74 @@ |
| +#!/usr/bin/env python2 |
| +# Copyright 2017 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
|
sfiera
2017/02/16 11:14:23
py-style: please add __future__ imports:
from __f
fhorschig
2017/02/16 17:06:42
Done.
|
| +import glob |
| +import json |
| +import os |
| +import sys |
| +import urllib |
| +import Image |
| +from io import BytesIO |
|
sfiera
2017/02/16 11:14:23
py-style: from x import y is not allowed unless y
fhorschig
2017/02/16 17:06:42
Done.
|
| + |
| +# This script downloads the default popular sites and large icons associated |
| +# with it. If an icon is too large, it will get resized in the process. |
| + |
| +DEFAULT_POPULAR_SITES = 'https://www.gstatic.com/chrome/ntp/'\ |
|
sfiera
2017/02/16 11:14:23
py-style: use parens, not backslashes.
fhorschig
2017/02/16 17:06:42
Done.
|
| + 'suggested_sites_DEFAULT_5.json' |
| +LARGE_ICON_KEY = 'large_icon_url' |
| +SITE_TITLE_KEY = 'title' |
| +MAXIMAL_SIZE = (196, 196) |
| +SITE_ICON_NAME = 'icon' |
|
sfiera
2017/02/16 11:14:23
You might prefer `SITE_ICON_FORMAT = "icon%d.png"`
fhorschig
2017/02/16 17:06:41
I like this but now I need a second one for deleti
|
| +NTP_TILES_RESOURCE_PATH = os.path.join( |
| + os.path.dirname(os.path.realpath(__file__)), 'resources') |
| +DEFAULT_POPULAR_SITES_PATH = os.path.join(NTP_TILES_RESOURCE_PATH, |
| + 'default_popular_sites.json') |
|
sfiera
2017/02/16 11:14:23
py-style: alignment
fhorschig
2017/02/16 17:06:42
Done.
|
| + |
| +def download_popular_sites(): |
|
sfiera
2017/02/16 11:14:23
Docstrings, please. For example, it's useful here
fhorschig
2017/02/16 17:06:42
Split the function. (For multiple reasons)
|
| + print("Downloading popular sites... (" + DEFAULT_POPULAR_SITES + ")") |
|
sfiera
2017/02/16 11:14:22
py-style: Proper indentation is 2 spaces.
sfiera
2017/02/16 11:14:23
py-style: You're switching between ' and " quotes.
fhorschig
2017/02/16 17:06:42
Done although your guidelines say 4 [1].
Is there
fhorschig
2017/02/16 17:06:42
Done.
sfiera
2017/02/16 18:54:16
Oh, apparently 2 space is Google internal but 4 is
fhorschig
2017/02/17 16:24:03
Done.
|
| + data = json.load(urllib.urlopen(url=DEFAULT_POPULAR_SITES)) |
| + print("... done. (%d sites found)" % len(data)) |
| + with open(DEFAULT_POPULAR_SITES_PATH, 'w') as f: |
| + json.dump(data, f) |
| + print("JSON was written to " + DEFAULT_POPULAR_SITES_PATH) |
| + return data |
| + |
| +def delete_old_icons(): |
| + print("Deleting old icons..") |
| + for f in glob.glob(os.path.join(NTP_TILES_RESOURCE_PATH, |
| + SITE_ICON_NAME + '[0-9].png')): |
| + os.remove(os.path.join(f)) |
| + print("... done.") |
| + |
| +def download_image_for_popular_site(site): |
| + image_response_data = urllib.urlopen(url=site[LARGE_ICON_KEY]).read() |
|
sfiera
2017/02/16 11:14:23
Please use "with …urlopen() as …" to ensure that t
fhorschig
2017/02/16 17:06:42
Done with hack: https://docs.python.org/2/library/
|
| + return Image.open(BytesIO(image_response_data)) |
| + |
| +def resize_if_too_large(image): |
|
sfiera
2017/02/16 11:14:23
Have we determined yet if resizing is OK? I would
fhorschig
2017/02/16 17:06:42
We have not yet decided. I added a flag to prevent
|
| + if image.size > MAXIMAL_SIZE: |
|
sfiera
2017/02/16 11:14:22
This does not mean what you think it means. Tuple
fhorschig
2017/02/16 17:06:42
I am actually fine with since sizes must be quadra
|
| + print("... and resizing image from %s to %s ..." % |
| + (image.size, MAXIMAL_SIZE)); |
| + image.thumbnail(MAXIMAL_SIZE, Image.ANTIALIAS) |
| + |
| +def malformed_json(site): |
| + return not SITE_TITLE_KEY in site or not LARGE_ICON_KEY in site |
| + |
| +def main(): |
| + delete_old_icons() |
| + popular_sites = download_popular_sites() |
| + for i in range(len(popular_sites)): |
|
sfiera
2017/02/16 11:14:23
for i, site in enumerate(popular_sites):
fhorschig
2017/02/16 17:06:42
Uuuh, nice.
|
| + site = popular_sites[i] |
| + if malformed_json(site): |
| + print("Could not download image for malformed entry: " + str(site)) |
|
sfiera
2017/02/16 11:14:23
"malformed" is not quote the right term here. Some
fhorschig
2017/02/16 17:06:42
Done.
|
| + continue |
| + print("Downloading icon for \"" + site[SITE_TITLE_KEY]) +"\"..." |
|
sfiera
2017/02/16 11:14:23
"Downloading icon for %r..." % site[SITE_TITLE_KEY
fhorschig
2017/02/16 17:06:42
Done.
|
| + image = download_image_for_popular_site(site) |
| + resize_if_too_large(image) |
| + image_name = SITE_ICON_NAME + str(i) + ".png" |
|
sfiera
2017/02/16 11:14:22
In general, using str() looks not very python-y. I
fhorschig
2017/02/16 17:06:42
Gone.
|
| + image.save(os.path.join(NTP_TILES_RESOURCE_PATH, image_name), 'PNG') |
| + print("... done.(Stored as " + image_name + ")"); |
|
sfiera
2017/02/16 11:14:22
Nit: Space between . and (
fhorschig
2017/02/16 17:06:42
Done.
|
| + |
| + |
| +if __name__ == '__main__': |
| + main() |