PDA

View Full Version : Simple C# program using MonoDevelop to download HQ Chromecast Wallpapers



SeanKD
December 12th, 2016, 08:45 PM
I wrote a small program in C# using MonoDevelop to download high quality Chromecast wallpapers.
This is an updated and improved version from a previous attempt.
I have tested the code on Ubuntu GNOME 16.04 LTS.
Just be sure to change the directory string to the location you wish the wallpapers to be saved to.
Enjoy! Any suggestions for improvement are welcome.

*NOTE: I have attached a text file with the source code that you can download, so that you don't
have to bother copying and pasting it from this post.

*UPDATE: Upon further testing, I notice the program was failing due to an occassional
Internal Server Error 500. I have amended the code to account for this and it should work
just fine now.



//Sean K. Davis
//Email:sean.k.davis@gmail.com
//
//Chromecast Wallpapers Downloader
//
//This program downloads the current set of Chromecast wallpapers
//and saves them to a directory of your choice
//
//I claim no copyright on this code,feel free to use/reuse it as you please.
//

using System;
using System.IO;
using System.Net;

namespace ChromecastWallpapers{

class ChromecastWallpapers{

static void Main(string[]args){

using(WebClient client = new WebClient()){
//Set the save directory here
string directory = "/home/username/Pictures/";

//Create the directory if it does not already exist
if(!Directory.Exists(directory))
Directory.CreateDirectory(directory);

//Download the chromecast screensaver html
string htmlCode = client.DownloadString("https://clients3.google.com/cast/chromecast/home");

//Separators used to split html code
string[] separator = {"\\x22"};

//Split the html code
string[] lines = htmlCode.Split(separator,StringSplitOptions.Remove EmptyEntries);

foreach (string line in lines){
//Check whether we have a line with an image URL
if(line.Contains("lh3.googleusercontent.com")){

//Cleanup the file URL so that we can use it to download the image
string fileURL = line.Replace("u003ds1280-w1280-h720-p-k-no-nd-mv", "")
.Replace("u003ds1280-w1280-h720-n-k-no-nd-mv", "")
.Replace("\\", "");

try{
WebRequest request = WebRequest.Create(fileURL);
request.Method="HEAD";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if(response.StatusCode.ToString() == "OK"){
//Downloadtheimage data
byte[] data = client.DownloadData(fileURL);

//String to hold the image file name
string fileName = "";

//Get the image file name
if(!String.IsNullOrEmpty(client.ResponseHeaders["Content-Disposition"])){
fileName = client.ResponseHeaders["Content-Disposition"]
.Substring(client.ResponseHeaders["Content-Disposition"]
.IndexOf("filename=")+10).Replace("\"","");
}

//If the file does not already exist,then save it
if(!File.Exists(fileName) && !String.IsNullOrEmpty(fileName)){
File.WriteAllBytes(directory + fileName, data);
}
}
}
catch{
}
}
}
}
}
}
}