add images table, organize time and weather namespace

This commit is contained in:
Ben Harris 2017-12-08 23:39:19 -05:00
parent 0e17e9e357
commit dec0423a99
11 changed files with 232 additions and 78 deletions

20
Commands/Images.cs Normal file
View File

@ -0,0 +1,20 @@
using Discord.Commands;
using dotbot.Core;
using Microsoft.Extensions.Configuration;
using System.Linq;
using System.Threading.Tasks;
namespace dotbot.Commands
{
[Group("img")]
public class Images : ModuleBase<SocketCommandContext>
{
[Command]
[Alias("image", "pic")]
[Summary("get a saved image")]
public async Task GetImage([Summary("image name")] string name)
{
}
}
}

View File

@ -9,15 +9,13 @@ namespace dotbot.Commands
{
public class Location : ModuleBase<SocketCommandContext>
{
private readonly IConfigurationRoot _config;
private string OwmApiUrl = "http://api.openweathermap.org/data/2.5/weather";
private string GeoNamesUrl = "http://api.geonames.org/timezoneJSON";
private string OwmUrl;
private string GeoNamesUrl;
public Location(IConfigurationRoot config)
{
_config = config;
OwmApiUrl += $"?APPID={_config["tokens:owm"]}&units=metric";
GeoNamesUrl += $"?username={_config["tokens:geonames"]}";
OwmUrl = $"{config["endpoints:owm"]}?APPID={config["tokens:owm"]}&units=metric";
GeoNamesUrl = $"{config["endpoints:geonames"]}?username={config["tokens:geonames"]}";
}
@ -25,8 +23,8 @@ namespace dotbot.Commands
[Summary("save your location so benbot can look up your weather and timezone")]
public async Task SaveUserLocation([Remainder] [Summary("location")] string location)
{
var owm = Utils.GetJson<Weather.OwmApiResult>($"{OwmApiUrl}&q={HttpUtility.UrlEncode(location)}");
var geo = Utils.GetJson<Time.GeonamesApiResult>($"{GeoNamesUrl}&lat={owm.coord.lat}&lng={owm.coord.lon}");
var owm = Utils.GetJson<OwmApiResult>($"{OwmUrl}&q={HttpUtility.UrlEncode(location)}");
var geo = Utils.GetJson<GeonamesApiResult>($"{GeoNamesUrl}&lat={owm.coord.lat}&lng={owm.coord.lon}");
using (var db = new DotbotDbContext())
{
if (db.UserLocations.Any(u => u.Id == Context.User.Id))

View File

@ -14,16 +14,17 @@ namespace dotbot.Commands
public class Time : ModuleBase<SocketCommandContext>
{
private readonly IConfigurationRoot _config;
private string OwmApiUrl = "http://api.openweathermap.org/data/2.5/weather";
private string GeoNamesUrl = "http://api.geonames.org/timezoneJSON";
private string OwmUrl;
private string GeoNamesUrl;
public Time(IConfigurationRoot config)
{
_config = config;
OwmApiUrl += $"?APPID={_config["tokens:owm"]}&units=metric";
GeoNamesUrl += $"?username={_config["tokens:geonames"]}";
OwmUrl = $"{_config["endpoints:owm"]}?APPID={_config["tokens:owm"]}&units=metric";
GeoNamesUrl = $"{_config["endpoints:geonames"]}?username={_config["tokens:geonames"]}";
}
[Command]
[Summary("get the time at your location (or bot's location if you don't have one saved)")]
public async Task GetTime()
@ -33,7 +34,7 @@ namespace dotbot.Commands
if (db.UserLocations.Any(u => u.Id == Context.User.Id))
{
var ul = db.UserLocations.Find(Context.User.Id);
await ReplyAsync($"it's {GetTimeFromIanaId(ul.TimeZone):g} in {ul.City}");
await ReplyAsync($"it's {Utils.IanaIdToDateTime(ul.TimeZone):g} in {ul.City}");
}
else await ReplyAsync($"it's {DateTime.Now:g} Eastern Time (where the bot is hosted)\n\nyou can save your location/timezone with `{_config["prefix"]}savelocation <city>`");
}
@ -49,12 +50,9 @@ namespace dotbot.Commands
if (db.UserLocations.Any(u => u.Id == user.Id))
{
var ul = db.UserLocations.Find(user.Id);
await ReplyAsync($"the time for {user.Mention} in {ul.City} is {GetTimeFromIanaId(ul.TimeZone):g}");
}
else
{
await ReplyAsync($"{user.Mention} does not have a saved location\nit's {DateTime.Now:g} Eastern Time (US)");
await ReplyAsync($"the time for {user.Mention} in {ul.City} is {Utils.IanaIdToDateTime(ul.TimeZone):g}");
}
else await ReplyAsync($"{user.Mention} does not have a saved location\nit's {DateTime.Now:g} Eastern Time (US)");
}
}
@ -64,17 +62,17 @@ namespace dotbot.Commands
public async Task LookupTime([Remainder] [Summary("location")] string location)
{
await Context.Channel.TriggerTypingAsync();
var owm = Utils.GetJson<Weather.OwmApiResult>($"{OwmApiUrl}&q={HttpUtility.UrlEncode(location)}");
var owm = Utils.GetJson<OwmApiResult>($"{OwmUrl}&q={HttpUtility.UrlEncode(location)}");
var geo = Utils.GetJson<GeonamesApiResult>($"{GeoNamesUrl}&lat={owm.coord.lat}&lng={owm.coord.lon}");
await ReplyAsync($"it is {GetTimeFromIanaId(geo.timezoneId)} in {owm.name}");
await ReplyAsync($"it is {Utils.IanaIdToDateTime(geo.timezoneId)} in {owm.name}");
}
public class GeonamesApiResult
{
public string timezoneId { get; set; }
}
public static DateTime GetTimeFromIanaId(string tzId) => SystemClock.Instance.GetCurrentInstant().InZone(DateTimeZoneProviders.Tzdb[tzId]).ToDateTimeUnspecified();
}
public class GeonamesApiResult
{
public string timezoneId { get; set; }
}
}

View File

@ -13,14 +13,12 @@ namespace dotbot.Commands
public class Weather : ModuleBase<SocketCommandContext>
{
private IConfigurationRoot _config;
private string OwmApiUrl = "http://api.openweathermap.org/data/2.5/weather";
private string GeoNamesUrl = "http://api.geonames.org/timezoneJSON";
private string OwmUrl;
public Weather(IConfigurationRoot config)
{
_config = config;
OwmApiUrl += $"?APPID={_config["tokens:owm"]}&units=metric";
GeoNamesUrl += $"?username={_config["tokens:geonames"]}";
OwmUrl = $"{_config["endpoints:owm"]}?APPID={_config["tokens:owm"]}&units=metric";
}
@ -32,7 +30,7 @@ namespace dotbot.Commands
{
if (db.UserLocations.Any(u => u.Id == Context.User.Id))
{
var url = $"{OwmApiUrl}&id={db.UserLocations.Find(Context.User.Id).CityId}";
var url = $"{OwmUrl}&id={db.UserLocations.Find(Context.User.Id).CityId}";
await ReplyAsync("", embed: WeatherEmbed(Utils.GetJson<OwmApiResult>(url)));
}
else await ReplyAsync($"you don't have a location saved. look one up with `{_config["prefix"]}weather <search term>` or save a location for yourself with `{_config["prefix"]}savelocation <city>`");
@ -48,7 +46,7 @@ namespace dotbot.Commands
{
if (db.UserLocations.Any(u => u.Id == user.Id))
{
var url = $"{OwmApiUrl}&id={db.UserLocations.Find(user.Id).CityId}";
var url = $"{OwmUrl}&id={db.UserLocations.Find(user.Id).CityId}";
await ReplyAsync("", embed: WeatherEmbed(Utils.GetJson<OwmApiResult>(url)));
}
else await ReplyAsync($"{user.Mention} doesn't have a location saved. look one up with `{_config["prefix"]}weather <search term>` or save a location for yourself with `{_config["prefix"]}savelocation <city>`");
@ -60,53 +58,11 @@ namespace dotbot.Commands
[Summary("look up the weather at a specified location")]
public async Task LookupWeather([Remainder] [Summary("location")] string location)
{
var url = $"{OwmApiUrl}&q={HttpUtility.UrlEncode(location)}";
var url = $"{OwmUrl}&q={HttpUtility.UrlEncode(location)}";
await ReplyAsync("", embed: WeatherEmbed(Utils.GetJson<OwmApiResult>(url)));
}
public class OwmApiResult
{
public int id { get; set; }
public string name { get; set; }
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public Coord coord { get; set; }
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public Weather[] weather { get; set; }
public class WeatherMain
{
public double temp { get; set; }
public double pressure { get; set; }
public double humidity { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
}
public WeatherMain main { get; set; }
public class Wind
{
public double speed { get; set; }
public int deg { get; set; }
}
public Wind wind { get; set; }
public class Sys
{
public string country { get; set; }
public double sunrise { get; set; }
public double sunset { get; set; }
}
public Sys sys { get; set; }
}
private Embed WeatherEmbed(OwmApiResult result)
{
var embed = new EmbedBuilder
@ -128,4 +84,47 @@ namespace dotbot.Commands
}
}
public class OwmApiResult
{
public int id { get; set; }
public string name { get; set; }
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public Coord coord { get; set; }
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public Weather[] weather { get; set; }
public class WeatherMain
{
public double temp { get; set; }
public double pressure { get; set; }
public double humidity { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
}
public WeatherMain main { get; set; }
public class Wind
{
public double speed { get; set; }
public int deg { get; set; }
}
public Wind wind { get; set; }
public class Sys
{
public string country { get; set; }
public double sunrise { get; set; }
public double sunset { get; set; }
}
public Sys sys { get; set; }
}
}

View File

@ -7,6 +7,7 @@ namespace dotbot.Core
public DbSet<Definition> Defs { get; set; }
public DbSet<Email> Emails { get; set; }
public DbSet<UserLocation> UserLocations { get; set; }
public DbSet<Image> Images { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

8
Core/Image.cs Normal file
View File

@ -0,0 +1,8 @@
namespace dotbot.Core
{
public class Image
{
public string Id { get; set; }
public string FilePath { get; set; }
}
}

View File

@ -1,4 +1,6 @@
using Newtonsoft.Json;
using NodaTime;
using System;
using System.Net;
namespace dotbot.Core
@ -13,5 +15,7 @@ namespace dotbot.Core
public static double ConvertCToF(double c) => ((9.0 / 5.0) * c) + 32;
public static DateTime IanaIdToDateTime(string tzId) => SystemClock.Instance.GetCurrentInstant().InZone(DateTimeZoneProviders.Tzdb[tzId]).ToDateTimeUnspecified();
}
}

View File

@ -0,0 +1,80 @@
// <auto-generated />
using dotbot.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using System;
namespace dotbot.Migrations
{
[DbContext(typeof(DotbotDbContext))]
[Migration("20171209043817_AddImageTable")]
partial class AddImageTable
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.0.1-rtm-125");
modelBuilder.Entity("dotbot.Core.Definition", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("Def");
b.HasKey("Id");
b.ToTable("Defs");
});
modelBuilder.Entity("dotbot.Core.Email", b =>
{
b.Property<ulong>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("EmailAddress");
b.HasKey("Id");
b.ToTable("Emails");
});
modelBuilder.Entity("dotbot.Core.Image", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("FilePath");
b.HasKey("Id");
b.ToTable("Images");
});
modelBuilder.Entity("dotbot.Core.UserLocation", b =>
{
b.Property<ulong>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("City");
b.Property<int>("CityId");
b.Property<double>("Lat");
b.Property<double>("Lng");
b.Property<string>("TimeZone");
b.HasKey("Id");
b.ToTable("UserLocations");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
namespace dotbot.Migrations
{
public partial class AddImageTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Images",
columns: table => new
{
Id = table.Column<string>(nullable: false),
FilePath = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Images", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Images");
}
}
}

View File

@ -42,6 +42,18 @@ namespace dotbot.Migrations
b.ToTable("Emails");
});
modelBuilder.Entity("dotbot.Core.Image", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("FilePath");
b.HasKey("Id");
b.ToTable("Images");
});
modelBuilder.Entity("dotbot.Core.UserLocation", b =>
{
b.Property<ulong>("Id")
@ -51,9 +63,9 @@ namespace dotbot.Migrations
b.Property<int>("CityId");
b.Property<float>("Lat");
b.Property<double>("Lat");
b.Property<float>("Lng");
b.Property<double>("Lng");
b.Property<string>("TimeZone");

View File

@ -7,5 +7,9 @@
"gmail": "", // password here
"owm": "",
"geonames": ""
},
"endpoints": {
"owm": "http://api.openweathermap.org/data/2.5/weather",
"geonames": "http://api.geonames.org/timezoneJSON"
}
}