Skip to content

Commit

Permalink
Adding random image web source and making it as default one.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudip Mandal (HCL America Inc) committed May 21, 2020
1 parent e860594 commit 74f0ef4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Reflection;
using System.Linq;
using System.Net;

namespace Common
{
Expand All @@ -29,6 +30,15 @@ internal static string GetExeFolder()
return Path.GetDirectoryName(path);
}

internal static string DownloadUrlToDisk(string url)
{
WebClient wc = new WebClient();
string pathToSave = Utils.GetExeFolder() + "/wallpaper.jpg";
wc.DownloadFile(url, pathToSave);

return pathToSave;
}

static Dictionary<string,Type> GetSourcesMap()
{
Dictionary<string,Type> map = new Dictionary<string, Type>();
Expand Down
19 changes: 17 additions & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static void Main(string[] args)
if(args.Length == 0)
{
//Select a random source and set wallpaper
Wallpaper.SetWallpaperFromSource(Common.Utils.sourceMap.Keys.First());
Wallpaper.SetWallpaperFromSource("RandomFromWeb");
}
else
{
Expand All @@ -28,8 +28,23 @@ static void Main(string[] args)
Console.WriteLine("\t" + sourceName);
}
break;
case "-u":
case "-s":
//set wallpaper from specified source
if (args.Length < 2)
Console.WriteLine("Source not specified");
else if (args.Length > 2)
Console.WriteLine("Invalid number of arguments\n correct format is wallpaper -s SOURCENAME");
else
{
string usrSel = args[1];
//check if this is a valid source name
if (!Common.Utils.sourceMap.Keys.Contains(usrSel))
Console.WriteLine("Soruce {0} not found. \n To view list of valid sources run : wallpaper -ls");
else
Wallpaper.SetWallpaperFromSource(usrSel);
}

break;
default:
//display help
break;
Expand Down
5 changes: 2 additions & 3 deletions src/Sources/Bing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ public string DownloadWallpaper()

JObject result = JObject.Parse(jsonString);
string imgURL = "http://bing.com" + result["images"][0]["url"].ToString();
string pathToSave = Utils.GetExeFolder() + "/wallpaper.jpg";
wc.DownloadFile(imgURL, pathToSave);


return pathToSave;
return Utils.DownloadUrlToDisk(imgURL);
}

public string Name()
Expand Down
52 changes: 52 additions & 0 deletions src/Sources/RandomWeb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Common;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using System.Net.Http;

namespace Sources
{
public class RandomWeb : IWallpaperSource
{
//FUTURE : We can make this user configurable, so that user can define own wallpaper topics to search internet from
string[] topics = new string[] { "Mountains", "Birds", "Nature", "Sunset", "Galaxy", "Rivers", "Flowers", "Animals", "Abstract" };

public string DownloadWallpaper()
{
//Pick a random item from the topics array
Random randomGen = new Random();
int topicNo = randomGen.Next(0, topics.Length - 1);
string topic = topics[topicNo];

//Search Bing images using this topic

string bingImagesUrl = $"https://bing.com/images/search?q={topic}&qft=+filterui:imagesize-wallpaper&FORM=IRFLTR";

HttpClient wc = new HttpClient();
wc.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
wc.DefaultRequestHeaders.Add("Accept", "*/*");
var data = wc.GetStringAsync(bingImagesUrl).Result;

//Scrape out the image urls using regex

Regex regex = new Regex("(?<=murl\\&quot;:\\&quot;)(.|\n)*?(?=\\&quot;,\\&quot;turl)");
var matches = regex.Matches(data);


//Pick a random url

var urls = matches.Select(x => x.Value).Where(p => p.EndsWith("jpg") || p.EndsWith("png") || p.EndsWith("jpeg")).ToList();

var sUrl = urls[randomGen.Next(0, urls.Count - 1)];

//Download file and return path
return Utils.DownloadUrlToDisk(sUrl);
}

public string Name()
{
return "RandomFromWeb";
}
}
}
6 changes: 3 additions & 3 deletions src/wallpaper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<DefineConstants>Linux</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3"/>
<PackageReference Include="System.Drawing.Common" Version="4.7.0"/>
<PackageReference Include="Microsoft.Win32.Registry" Version="4.7.0"/>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
<PackageReference Include="Microsoft.Win32.Registry" Version="4.7.0" />
</ItemGroup>
</Project>

0 comments on commit 74f0ef4

Please sign in to comment.