Skip to content
Permalink
Browse files

RIP WUnderground

  • Loading branch information...
golf1052 committed May 7, 2019
1 parent ab7c16f commit faa35b5580a9819783653f8d8c7c788a3010dc6f
Showing with 40 additions and 29 deletions.
  1. +36 −0 .vscode/tasks.json
  2. +4 −29 services.golf1052.com/Controllers/ThemeController.cs
@@ -0,0 +1,36 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/services.golf1052.com/services.golf1052.com.csproj"
],
"problemMatcher": "$tsc"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/services.golf1052.com/services.golf1052.com.csproj"
],
"problemMatcher": "$tsc"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/services.golf1052.com/services.golf1052.com.csproj"
],
"problemMatcher": "$tsc"
}
]
}
@@ -15,35 +15,10 @@ public class ThemeController : Controller
public async Task<JObject> GetTheme([FromQuery] string latitude = null, [FromQuery] string longitude = null)
{
JObject returnObject = new JObject();
HttpClient httpClient = new HttpClient();
HttpResponseMessage response;
if (string.IsNullOrEmpty(latitude) || string.IsNullOrEmpty(longitude))
{
var ip = Request.HttpContext.Connection.RemoteIpAddress;
response = await httpClient.GetAsync($"http://api.wunderground.com/api/{Secrets.WundergroundApiKey}/astronomy/q/autoip.json?geo_ip={ip.ToString()}");
}
else
{
response = await httpClient.GetAsync($"http://api.wunderground.com/api/{Secrets.WundergroundApiKey}/astronomy/q/{latitude},{longitude}.json");
}
JObject responseObject = JObject.Parse(await response.Content.ReadAsStringAsync());
if (responseObject["response"]["error"] != null)
{
Random random = new Random();
if (random.NextDouble() < 0.5)
{
returnObject["theme"] = "light";
}
else
{
returnObject["theme"] = "dark";
}
return returnObject;
}
DateTimeOffset sunrise = DateTimeHelper(int.Parse((string)responseObject["moon_phase"]["sunrise"]["hour"]), int.Parse((string)responseObject["moon_phase"]["sunrise"]["minute"]));
DateTimeOffset sunset = DateTimeHelper(int.Parse((string)responseObject["moon_phase"]["sunset"]["hour"]), int.Parse((string)responseObject["moon_phase"]["sunset"]["minute"]));
DateTimeOffset userTime = DateTimeHelper(int.Parse((string)responseObject["moon_phase"]["current_time"]["hour"]), int.Parse((string)responseObject["moon_phase"]["current_time"]["minute"]));
if (sunrise < userTime && userTime < sunset)
// Weather Underground killed their free API in Feb 2019 so for now just return a random theme until I find
// a new API source.
Random random = new Random();
if (random.NextDouble() < 0.5)
{
returnObject["theme"] = "light";
}

0 comments on commit faa35b5

Please sign in to comment.
You can’t perform that action at this time.