diff --git a/README.md b/README.md index 1086250..e08b5b6 100644 --- a/README.md +++ b/README.md @@ -3,24 +3,41 @@ Google provided us with some impressive wallpapers. * [Chrome OS][Chrome OS link] -* [Google Maps][Maps link] +* [Google Maps][Maps link] (code exported from 3.0.3 version) Now you can use them locally! ## Usage -Just use Python 2.7.+, no dependencies required. +### Chrome OS version +Just use Python 3.4+, no dependencies required. ``` -$ python2 chrome-os/download-wallpapers.py -$ python2 maps/download-wallpapers.py +$ python3 chrome-os/download-wallpapers.py ``` -Wallpapers will be downloaded to relative `wallpapers` directories, -such as `chrome-os/wallpapers` and `maps/wallpapers`. +Wallpapers will be downloaded to `chrome-os/wallpapers`. +Be wary of download sizes: Chrome OS wallpapers are 200+ MB combined. -Be wary of download sizes: Chrome OS wallpapers are 200+ MB combined and Maps are 1.1+ GB. +### Maps version +Just use Python 3.4+, no dependencies required. But, if you want but if you want a progress bar to show the percentage of the downloads, consider installing [`tqdm`][tqdm_link] package. +``` +$ python3 maps/download-wallpapers.py +``` + +Wallpapers will be downloaded to `maps/wallpapers`. +Be wary of download sizes: Maps are 1.8+ GB (up to 3+ GB). + +Maps version now can use two different ways to download the image: +* Official links - links used by the plugin when you use the `Download wallpaper` button (with watermarks). +* Internal links - links used by the plugin to show you the image in new tab (without watermarks). + +Both of this methods provide you with the 1800x1200 images, but the "Official links" method provide you with watermarked images and no optimization, so images will weight more. + +## Code doesn't work? +Check the version of the [Chrome extention][Maps link], if it's bigger than `3.0.3`, then maybe they just updated the API and it's your time to change the code. Look at the code of the extention and extract the links and ids of the images. And don't forget to Pull Request the changes. [Chrome OS link]: https://chrome.google.com/webstore/detail/chrome-os-wallpapers/dkfibabkihblcenahmcdmfepojcejoan [Maps link]: https://chrome.google.com/webstore/detail/earth-view-from-google-ma/bhloflhklmhfpedakmangadcdofhnnoh +[tqdm_link]: https://pypi.org/project/tqdm/ diff --git a/maps/download-wallpapers.py b/maps/download-wallpapers.py index 8baf16a..7f76f94 100644 --- a/maps/download-wallpapers.py +++ b/maps/download-wallpapers.py @@ -1,7 +1,7 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 ''' -Copyright 2014 Artur Dryomov +Copyright 2020 Artur Dryomov & Vladislav Kuleykin Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,81 +20,138 @@ import base64 import json import os -import urllib2 -import urlparse - - -REMOTE_URL = "https://www.gstatic.com/prettyearth/" +import urllib +import urllib.request + +try: + from tqdm import tqdm + use_tqdm = True + write = tqdm.write +except ImportError: + use_tqdm = False + write = print + +REMOTE_URLS = [ + "https://www.gstatic.com/prettyearth/assets/data/v3/{}.json", + "https://earthview.withgoogle.com/download/{}.jpg", +] REMOTE_IDS_PATH = "ids.json" - LOCAL_PATH = "wallpapers" +PROMPT_1 = "Download fullsize images? Fullsize images contain watermarks (y/n) " +PROMPT_2 = "Skip already downloaded wallpapers? (y/n) " -def download_wallpapers(): - create_wallpapers_path() - - print(":: Downloading Google Maps wallpapers.") - - for wallpaper_id in get_wallpaper_ids(): - print(wallpaper_id) - - wallpaper_info = get_wallpaper_info(wallpaper_id) - wallpaper_path = get_wallpaper_path(wallpaper_id) - wallpaper_bytes = get_wallpaper_bytes(wallpaper_info) - - download_wallpaper(wallpaper_path, wallpaper_bytes) - - -def create_wallpapers_path(): +def download_wallpapers(): wallpapers_path = get_wallpapers_path() - - if not os.path.exists(wallpapers_path): - os.makedirs(wallpapers_path) + create_directory(wallpapers_path) + + # Choose the url to download from. + prompt1_res = input(PROMPT_1).lower() == "y" + REMOTE_URL = REMOTE_URLS[prompt1_res] + + # Check the list of already downloaded wallpapers. + downloaded_wallpapers = os.listdir(wallpapers_path) + downloaded_wallpapers_ids = list(map(lambda x: x.rsplit('.', 1)[0], + downloaded_wallpapers)) + # Prompt the user to skip already downloaded wallpapers. + wallpapers_ids = set(get_wallpaper_ids()) + if not wallpapers_ids.isdisjoint(downloaded_wallpapers_ids): + if input(PROMPT_2).lower() == 'y': + print('Skipping downloaded wallpapers.') + wallpapers_ids.difference_update(downloaded_wallpapers_ids) + + # Start the download. + print(":: Downloading Google Maps wallpapers.") + if use_tqdm: + pbar = tqdm.tqdm(total=len(wallpapers_ids)) + for wallpaper_id in sorted(wallpapers_ids): + wallpaper_url = REMOTE_URL.format(wallpaper_id) + + # Download the wallpaper using the chosen URL + try: + wallpaper_bytes = REQ_FUNCS[prompt1_res](wallpaper_url) + except ValueError: + write(f"[WARNING] Could not download the wallpaper with id {wallpaper_id}, retrying with other method.") + try: + wallpaper_url = REMOTE_URLS[not prompt1_res].format(wallpaper_id) + wallpaper_bytes = REQ_FUNCS[not prompt1_res](wallpaper_url) + except ValueError: + write(f"[ERROR] Can't download the wallpaper with id {wallpaper_id} using both methods. Skipping it.") + continue + + wallpaper_path = get_wallpaper_path(wallpapers_path, wallpaper_id) + save_wallpaper(wallpaper_path, wallpaper_bytes) + if use_tqdm: + pbar.desc = "Wallpaper id: {}".format(wallpaper_id) + pbar.update(1) + else: + print('[OK] Downloaded wallpaper with id: {}'.format(wallpaper_id)) + + +def create_directory(dir_path): + """Create the directory in the specified path.""" + if not os.path.exists(dir_path): + os.makedirs(dir_path) def get_wallpapers_path(): + """Return the absolute path to the wallpapers directory.""" wallpapers_path = os.path.dirname(os.path.realpath(__file__)) - return os.path.abspath(os.path.join(wallpapers_path, LOCAL_PATH)) def get_wallpaper_ids(): + """Load all the wallpaper IDs from the file.""" with open(get_wallpaper_ids_path()) as wallpaper_ids_file: return json.load(wallpaper_ids_file) def get_wallpaper_ids_path(): + """Return the absolute path to the wallpapers directory.""" return os.path.join(os.path.dirname(__file__), REMOTE_IDS_PATH) -def get_wallpaper_info(wallpaper_id): - wallpaper_info_url = get_wallpaper_info_url(wallpaper_id) +def download_wallpaper_official(wallpaper_url): + """Download the wallpaper using the official URL.""" + response = urllib.request.urlopen(wallpaper_url) + if not response.headers.get('Content-Type') in ['image/jpeg', 'application/octet-stream']: + raise ValueError("Response should contain image, maybe it's 404 page.") + return response.read() - return json.load(urllib2.urlopen(wallpaper_info_url))["dataUri"] +def get_wallpaper_bytes(wallpaper_info): + """Get the wallpapers from the json response.""" + bytes_start_position = wallpaper_info.index(",") + 1 + return base64.b64decode(wallpaper_info[bytes_start_position:]) -def get_wallpaper_info_url(wallpaper_id): - return urlparse.urljoin(REMOTE_URL, "{id}.json".format(id=wallpaper_id)) +def download_wallpaper_from_plugin(wallpaper_info_url): + """Download the wallpaper as the plugin do.""" + try: + response = urllib.request.urlopen(wallpaper_info_url) + json_data = json.loads(response.read())['dataUri'] + except json.decoder.JSONDecodeError: + raise ValueError("Response should contain json data, maybe it's 404 page.") + return get_wallpaper_bytes(json_data) -def get_wallpaper_path(wallpaper_id): - wallpapers_path = get_wallpapers_path() - wallpaper_filename = "{id}.jpg".format(id=wallpaper_id) +def get_wallpaper_path(wallpapers_path, wallpaper_id): + """Return the path for the wallpaper with specified id.""" + wallpaper_filename = "{id}.jpg".format(id=wallpaper_id) return os.path.join(wallpapers_path, wallpaper_filename) -def get_wallpaper_bytes(wallpaper_info): - bytes_start_position = wallpaper_info.index(",") + 1 - - return base64.b64decode(wallpaper_info[bytes_start_position:]) - - -def download_wallpaper(wallpaper_path, wallpaper_bytes): +def save_wallpaper(wallpaper_path, wallpaper_bytes): + """Save the wallpaper to disk.""" with open(wallpaper_path, "wb") as wallpaper_file: wallpaper_file.write(wallpaper_bytes) +REQ_FUNCS = { + 0: download_wallpaper_from_plugin, + 1: download_wallpaper_official, +} + if __name__ == "__main__": download_wallpapers() diff --git a/maps/ids.json b/maps/ids.json index 0d277e4..ec0f217 100644 --- a/maps/ids.json +++ b/maps/ids.json @@ -1,4 +1,4 @@ -[ +[ "1003", "1004", "1006", @@ -322,6 +322,7 @@ "1523", "1525", "1526", + "1527", "1528", "1529", "1530", @@ -582,6 +583,7 @@ "1923", "1924", "1925", + "1926", "1927", "1934", "1935", @@ -1079,7 +1081,6 @@ "5464", "5474", "5477", - "5478", "5479", "5480", "5481", @@ -1504,12 +1505,14 @@ "7004", "7005", "7006", + "7007", "7008", "7009", "7010", "7011", "7012", "7013", + "7014", "7015", "7016", "7017", @@ -1517,5 +1520,1093 @@ "7019", "7020", "7021", - "7023" -] + "7022", + "7023", + "11935", + "11932", + "13942", + "13735", + "13730", + "13419", + "12078", + "14752", + "14737", + "14727", + "14719", + "14718", + "14669", + "14661", + "14647", + "14642", + "14632", + "14623", + "14613", + "14612", + "14603", + "14602", + "14599", + "14569", + "14560", + "14549", + "14542", + "14517", + "14508", + "14498", + "14489", + "14484", + "14478", + "14456", + "14434", + "14313", + "14312", + "14246", + "14111", + "14108", + "14097", + "14086", + "14068", + "14066", + "14059", + "14048", + "14046", + "14004", + "13986", + "13980", + "13925", + "13868", + "13763", + "13758", + "13678", + "13645", + "13581", + "13222", + "13208", + "13201", + "12986", + "12878", + "12861", + "12801", + "12686", + "12657", + "12623", + "14197", + "14196", + "14190", + "14188", + "14181", + "14171", + "14168", + "14145", + "14793", + "14792", + "14791", + "14790", + "14788", + "14787", + "14786", + "14784", + "14783", + "14782", + "14781", + "11566", + "13192", + "12277", + "12198", + "11573", + "14765", + "14700", + "14646", + "14523", + "14506", + "14504", + "14497", + "14451", + "14446", + "14436", + "14429", + "14362", + "14354", + "14344", + "14340", + "14339", + "14332", + "14309", + "14256", + "14254", + "14126", + "14124", + "14107", + "14105", + "14099", + "14096", + "14091", + "14090", + "14085", + "14084", + "14083", + "14074", + "14073", + "14072", + "14067", + "14060", + "14047", + "14015", + "14011", + "14007", + "14006", + "14005", + "13991", + "13984", + "13974", + "13973", + "13967", + "13933", + "13932", + "13924", + "13911", + "13907", + "13906", + "13897", + "13887", + "13886", + "13885", + "13871", + "13860", + "13859", + "13853", + "13841", + "13839", + "13830", + "13826", + "13824", + "13818", + "13788", + "13773", + "13770", + "13769", + "13765", + "13747", + "13745", + "13731", + "13687", + "13661", + "13660", + "13649", + "13646", + "13643", + "13640", + "13633", + "13631", + "13598", + "13591", + "13580", + "13547", + "13520", + "13519", + "13485", + "13467", + "13439", + "13414", + "13400", + "13394", + "13389", + "13288", + "13281", + "13280", + "13261", + "13249", + "13245", + "13238", + "13212", + "13209", + "13202", + "13200", + "13158", + "13151", + "13149", + "13004", + "12814", + "12773", + "12734", + "12724", + "12706", + "12692", + "12660", + "12658", + "12653", + "12646", + "12524", + "12523", + "12516", + "12496", + "12462", + "12444", + "12435", + "12424", + "12395", + "12390", + "12097", + "11782", + "11575", + "11601", + "12025", + "12024", + "12023", + "11862", + "14774", + "14741", + "14740", + "14739", + "14736", + "14734", + "14733", + "14732", + "14721", + "14686", + "14685", + "14680", + "14666", + "14664", + "14663", + "14653", + "14652", + "14630", + "14617", + "14611", + "14608", + "14597", + "14596", + "14592", + "14589", + "14582", + "14572", + "14561", + "14559", + "14555", + "14552", + "14491", + "14487", + "14479", + "14477", + "14438", + "14195", + "14194", + "14187", + "14186", + "14184", + "14182", + "14179", + "14178", + "14174", + "14169", + "14164", + "14162", + "14156", + "14143", + "14132", + "14129", + "14128", + "13716", + "13708", + "13707", + "13702", + "13195", + "12290", + "12261", + "12260", + "12257", + "11941", + "11939", + "11934", + "11931", + "11766", + "12026", + "12006", + "11653", + "11580", + "11567", + "12050", + "12048", + "12047", + "12039", + "11999", + "14730", + "14729", + "14708", + "14513", + "14511", + "14507", + "14505", + "14503", + "14499", + "14495", + "14455", + "14449", + "14417", + "14399", + "14396", + "14394", + "14390", + "14378", + "14373", + "14366", + "14331", + "14324", + "14322", + "14319", + "14310", + "14301", + "14297", + "14295", + "14294", + "14291", + "14289", + "14264", + "14259", + "14258", + "14244", + "14234", + "14232", + "14229", + "14228", + "14226", + "14211", + "14125", + "14123", + "14122", + "14114", + "14104", + "14102", + "14095", + "14093", + "14092", + "14079", + "14077", + "14071", + "14061", + "14058", + "14057", + "14056", + "14044", + "14043", + "14034", + "14026", + "14018", + "14012", + "14010", + "13997", + "13995", + "13985", + "13983", + "13982", + "13981", + "13978", + "13971", + "13965", + "13948", + "13947", + "13940", + "13939", + "13935", + "13931", + "13923", + "13914", + "13905", + "13896", + "13895", + "13892", + "13884", + "13879", + "13852", + "13844", + "13822", + "13821", + "13819", + "13814", + "13809", + "13799", + "13779", + "13754", + "13749", + "13746", + "13744", + "13728", + "13691", + "13670", + "13653", + "13650", + "13648", + "13642", + "13636", + "13622", + "13618", + "13615", + "13602", + "13600", + "13590", + "13582", + "13566", + "13546", + "13545", + "13543", + "13540", + "13537", + "13534", + "13532", + "13516", + "13515", + "13513", + "13508", + "13493", + "13489", + "13487", + "13472", + "13468", + "13462", + "13454", + "13426", + "13403", + "13402", + "13397", + "13383", + "13374", + "13366", + "13339", + "13332", + "13319", + "13316", + "13301", + "13289", + "13269", + "13252", + "13242", + "13239", + "13236", + "13218", + "13217", + "13207", + "13205", + "13204", + "13203", + "13183", + "13180", + "13161", + "13152", + "13150", + "13145", + "13135", + "13119", + "13117", + "13054", + "13050", + "13037", + "13025", + "13017", + "13012", + "13009", + "12995", + "12994", + "12988", + "12987", + "12968", + "12967", + "12937", + "12914", + "12907", + "12903", + "12891", + "12883", + "12882", + "12859", + "12858", + "12855", + "12854", + "12852", + "12846", + "12844", + "12843", + "12834", + "12833", + "12826", + "12816", + "12813", + "12812", + "12809", + "12807", + "12802", + "12800", + "12799", + "12761", + "12730", + "12728", + "12717", + "12693", + "12679", + "12672", + "12661", + "12659", + "12656", + "12634", + "12629", + "12626", + "12610", + "12606", + "12602", + "12597", + "12525", + "12502", + "12478", + "12455", + "12437", + "12434", + "12432", + "12425", + "12403", + "12400", + "12393", + "12382", + "12348", + "12347", + "12332", + "12321", + "12292", + "12207", + "12133", + "11688", + "11654", + "11603", + "11597", + "13705", + "13299", + "13193", + "12267", + "12156", + "11899", + "11650", + "11639", + "11626", + "11623", + "11698", + "11663", + "11658", + "11589", + "12056", + "12032", + "12031", + "12019", + "12008", + "14772", + "14768", + "14751", + "14747", + "14746", + "14744", + "14742", + "14738", + "14735", + "14726", + "14725", + "14724", + "14723", + "14722", + "14720", + "14677", + "14668", + "14667", + "14665", + "14662", + "14643", + "14641", + "14639", + "14636", + "14634", + "14631", + "14628", + "14616", + "14615", + "14601", + "14600", + "14598", + "14588", + "14587", + "14581", + "14580", + "14578", + "14577", + "14576", + "14575", + "14573", + "14564", + "14554", + "14548", + "14546", + "14543", + "14541", + "14538", + "14531", + "14492", + "14490", + "14483", + "14482", + "14480", + "14472", + "14471", + "14452", + "14443", + "14442", + "14201", + "14200", + "14198", + "14193", + "14192", + "14189", + "14180", + "14175", + "14173", + "14170", + "14166", + "14165", + "14163", + "14161", + "14158", + "14150", + "14149", + "14148", + "14144", + "14142", + "14141", + "14140", + "14134", + "14130", + "14127", + "12012", + "11946", + "11720", + "11651", + "11624", + "11610", + "14714", + "14711", + "14705", + "14703", + "14702", + "14688", + "14529", + "14525", + "14524", + "14521", + "14519", + "14518", + "14509", + "14468", + "14467", + "14466", + "14465", + "14463", + "14454", + "14428", + "14406", + "14402", + "14400", + "14398", + "14397", + "14392", + "14391", + "14386", + "14384", + "14383", + "14376", + "14374", + "14370", + "14360", + "14359", + "14348", + "14345", + "14341", + "14335", + "14334", + "14328", + "14318", + "14317", + "14315", + "14305", + "14303", + "14299", + "14290", + "14283", + "14282", + "14266", + "14260", + "14257", + "14255", + "14250", + "14249", + "14248", + "14247", + "14245", + "14242", + "14240", + "14236", + "14225", + "14218", + "14215", + "14214", + "14212", + "14112", + "14109", + "14098", + "14094", + "14089", + "14088", + "14087", + "14082", + "14081", + "14080", + "14075", + "14065", + "14055", + "14053", + "14052", + "14037", + "14036", + "14035", + "14033", + "14032", + "14031", + "14027", + "14024", + "14022", + "14021", + "14019", + "14017", + "14014", + "14002", + "13999", + "13996", + "13990", + "13977", + "13976", + "13975", + "13972", + "13962", + "13961", + "13960", + "13956", + "13954", + "13950", + "13945", + "13941", + "13936", + "13934", + "13929", + "13926", + "13921", + "13919", + "13917", + "13916", + "13913", + "13910", + "13902", + "13898", + "13891", + "13890", + "13889", + "13880", + "13873", + "13872", + "13870", + "13869", + "13865", + "13861", + "13854", + "13845", + "13842", + "13832", + "13831", + "13827", + "13823", + "13817", + "13816", + "13812", + "13811", + "13810", + "13808", + "13789", + "13786", + "13782", + "13781", + "13771", + "13767", + "13755", + "13750", + "13748", + "13743", + "13742", + "13693", + "13692", + "13676", + "13673", + "13667", + "13664", + "13663", + "13656", + "13654", + "13652", + "13647", + "13644", + "13639", + "13638", + "13634", + "13630", + "13629", + "13623", + "13619", + "13617", + "13614", + "13603", + "13601", + "13599", + "13595", + "13594", + "13592", + "13583", + "13578", + "13577", + "13572", + "13560", + "13553", + "13552", + "13549", + "13544", + "13539", + "13536", + "13531", + "13528", + "13526", + "13523", + "13517", + "13504", + "13492", + "13483", + "13481", + "13475", + "13474", + "13470", + "13466", + "13457", + "13456", + "13441", + "13440", + "13433", + "13430", + "13424", + "13420", + "13418", + "13409", + "13404", + "13401", + "13398", + "13388", + "13386", + "13361", + "13360", + "13358", + "13352", + "13350", + "13340", + "13338", + "13335", + "13331", + "13330", + "13329", + "13328", + "13327", + "13326", + "13321", + "13311", + "13306", + "13295", + "13293", + "13291", + "13286", + "13285", + "13284", + "13282", + "13277", + "13275", + "13270", + "13263", + "13258", + "13251", + "13250", + "13247", + "13246", + "13244", + "13241", + "13237", + "13233", + "13231", + "13228", + "13224", + "13221", + "13215", + "13206", + "13187", + "13184", + "13167", + "13166", + "13162", + "13157", + "13155", + "13147", + "13099", + "13069", + "13059", + "13056", + "13049", + "13048", + "13033", + "13031", + "13028", + "13026", + "13024", + "13023", + "13016", + "13015", + "13013", + "13008", + "13001", + "12989", + "12983", + "12979", + "12977", + "12955", + "12953", + "12951", + "12950", + "12947", + "12939", + "12936", + "12933", + "12932", + "12926", + "12923", + "12922", + "12904", + "12895", + "12892", + "12890", + "12876", + "12873", + "12860", + "12857", + "12850", + "12849", + "12848", + "12845", + "12839", + "12838", + "12835", + "12831", + "12810", + "12797", + "12753", + "12744", + "12723", + "12714", + "12705", + "12691", + "12684", + "12682", + "12680", + "12678", + "12675", + "12671", + "12669", + "12663", + "12655", + "12650", + "12648", + "12643", + "12640", + "12636", + "12635", + "12633", + "12621", + "12616", + "12613", + "12609", + "12608", + "12603", + "12601", + "12600", + "12599", + "12594", + "12593", + "12588", + "12585", + "12584", + "12577", + "12571", + "12570", + "12565", + "12561", + "12559", + "12554", + "12540", + "12533", + "12519", + "12510", + "12501", + "12497", + "12492", + "12488", + "12487", + "12479", + "12463", + "12458", + "12451", + "12441", + "12420", + "12419", + "12411", + "12402", + "12401", + "12397", + "12391", + "12378", + "12363", + "12355", + "12352", + "12346", + "12325", + "12317", + "12316", + "12313", + "12306", + "12303", + "12293", + "12237", + "12232", + "12213", + "12131", + "12127", + "12107", + "12101", + "11783", + "11775", + "11697", + "11691", + "11664", + "11585", + "11582", + "11571", + "13717", + "13706", + "13704", + "13198", + "13196", + "12351", + "12336", + "12279" +] \ No newline at end of file