Last active
October 21, 2015 21:33
-
-
Save Marcus10110/756783b0a2fa875487a8 to your computer and use it in GitHub Desktop.
Linqpad Script to scrape every DSCOVR photo of earth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<Reference><ProgramFilesX86>\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies\System.Web.Helpers.dll</Reference> | |
<Namespace>System.Net</Namespace> | |
<Namespace>System.Threading</Namespace> | |
<Namespace>System.Threading.Tasks</Namespace> | |
<Namespace>System.Web.Helpers</Namespace> | |
</Query> | |
//Requires the following additional namespace imports: | |
//System.Net | |
//System.Threading | |
//System.Threading.Tasks | |
//System.Web.Helpers | |
//Requires the following additional reference: | |
//System.Web.Helpers.dll | |
//This script scrapes images of earth from http://epic.gsfc.nasa.gov/, becuase I could not find a way to download them automatically. | |
//Ideally used to fuel a folder with desktop wallpaper. | |
//You can get Linqpad here: http://www.linqpad.net/ | |
void Main() | |
{ | |
WebClient client = new WebClient(); | |
string destination_folder = Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + @"\DSCOVR\"; | |
if (Directory.Exists(destination_folder) == false) | |
Directory.CreateDirectory(destination_folder); | |
int image_count = 0; | |
int days_old = 1; | |
string format_str = "yyyyMMddHHmmss"; | |
do | |
{ | |
DateTime target_date = DateTime.Today.AddDays(0 - days_old); //yesterday. | |
days_old++; | |
String api_url = "http://epic.gsfc.nasa.gov/api/images.php?date=" + target_date.ToString("yyyy-MM-dd"); | |
var raw_jason = client.DownloadString(api_url); | |
var json_list = Json.Decode(raw_jason) as IEnumerable<dynamic>; | |
var image_names = json_list.Select(x => (string)x.image); | |
image_names.Dump(); | |
image_count = image_names.Count(); | |
if (image_names.Any()) | |
{ | |
foreach (string image_name in image_names) | |
{ | |
string image_url = @"http://epic.gsfc.nasa.gov/epic-archive/png/" + image_name + ".png"; | |
string file_name = image_name + ".png"; | |
string full_file_name = destination_folder + file_name; | |
if (File.Exists(full_file_name) == false) | |
{ | |
client.DownloadFile(image_url, full_file_name); | |
("Finished downloading " + file_name).Dump(); | |
string date_str = full_file_name.Substring(full_file_name.LastIndexOf("epic_1b_") + "epic_1b_".Length); | |
date_str = date_str.Substring(0, date_str.IndexOf("_")); | |
var date = DateTime.ParseExact(date_str, format_str, System.Globalization.CultureInfo.InvariantCulture); | |
File.SetCreationTimeUtc( full_file_name, date ); | |
} | |
} | |
} | |
} | |
while (image_count > 0); | |
"Done".Dump(); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment