OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python2 | |
2 # Copyright 2017 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
sfiera
2017/02/16 11:14:23
py-style: please add __future__ imports:
from __f
fhorschig
2017/02/16 17:06:42
Done.
| |
6 import glob | |
7 import json | |
8 import os | |
9 import sys | |
10 import urllib | |
11 import Image | |
12 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.
| |
13 | |
14 # This script downloads the default popular sites and large icons associated | |
15 # with it. If an icon is too large, it will get resized in the process. | |
16 | |
17 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.
| |
18 'suggested_sites_DEFAULT_5.json' | |
19 LARGE_ICON_KEY = 'large_icon_url' | |
20 SITE_TITLE_KEY = 'title' | |
21 MAXIMAL_SIZE = (196, 196) | |
22 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
| |
23 NTP_TILES_RESOURCE_PATH = os.path.join( | |
24 os.path.dirname(os.path.realpath(__file__)), 'resources') | |
25 DEFAULT_POPULAR_SITES_PATH = os.path.join(NTP_TILES_RESOURCE_PATH, | |
26 'default_popular_sites.json') | |
sfiera
2017/02/16 11:14:23
py-style: alignment
fhorschig
2017/02/16 17:06:42
Done.
| |
27 | |
28 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)
| |
29 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.
| |
30 data = json.load(urllib.urlopen(url=DEFAULT_POPULAR_SITES)) | |
31 print("... done. (%d sites found)" % len(data)) | |
32 with open(DEFAULT_POPULAR_SITES_PATH, 'w') as f: | |
33 json.dump(data, f) | |
34 print("JSON was written to " + DEFAULT_POPULAR_SITES_PATH) | |
35 return data | |
36 | |
37 def delete_old_icons(): | |
38 print("Deleting old icons..") | |
39 for f in glob.glob(os.path.join(NTP_TILES_RESOURCE_PATH, | |
40 SITE_ICON_NAME + '[0-9].png')): | |
41 os.remove(os.path.join(f)) | |
42 print("... done.") | |
43 | |
44 def download_image_for_popular_site(site): | |
45 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/
| |
46 return Image.open(BytesIO(image_response_data)) | |
47 | |
48 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
| |
49 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
| |
50 print("... and resizing image from %s to %s ..." % | |
51 (image.size, MAXIMAL_SIZE)); | |
52 image.thumbnail(MAXIMAL_SIZE, Image.ANTIALIAS) | |
53 | |
54 def malformed_json(site): | |
55 return not SITE_TITLE_KEY in site or not LARGE_ICON_KEY in site | |
56 | |
57 def main(): | |
58 delete_old_icons() | |
59 popular_sites = download_popular_sites() | |
60 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.
| |
61 site = popular_sites[i] | |
62 if malformed_json(site): | |
63 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.
| |
64 continue | |
65 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.
| |
66 image = download_image_for_popular_site(site) | |
67 resize_if_too_large(image) | |
68 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.
| |
69 image.save(os.path.join(NTP_TILES_RESOURCE_PATH, image_name), 'PNG') | |
70 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.
| |
71 | |
72 | |
73 if __name__ == '__main__': | |
74 main() | |
OLD | NEW |