Skip to content

Commit

Permalink
First C# version of MikuWeather
Browse files Browse the repository at this point in the history
  • Loading branch information
lzcapp committed Nov 10, 2019
1 parent bc3de85 commit 8ada3c8
Show file tree
Hide file tree
Showing 15 changed files with 490 additions and 320 deletions.
25 changes: 19 additions & 6 deletions MikuWeather_CS/App.config
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<appSettings>
<add key="apikey_baidu" value="edUWu66ddGavrmj9a6vcsa75"/>
<add key="apikey_caiyun" value="0df993c66c0c636e29ecbb5344252a4a"/>
<add key="apikey_baidu" value="edUWu66ddGavrmj9a6vcsa75" />
<add key="apikey_caiyun" value="0df993c66c0c636e29ecbb5344252a4a" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>
92 changes: 58 additions & 34 deletions MikuWeather_CS/DataQuery.cs
Original file line number Diff line number Diff line change
@@ -1,77 +1,101 @@
using System;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;

namespace MikuWeather
{
internal static class DataQuery
{
public static Dictionary<string, string> UpdateData_Baidu(string city)
{
Dictionary<string, string> resultDict = new Dictionary<string, string>();
namespace MikuWeather {

internal static class DataQuery {

public static Dictionary<string, string> UpdateData_Baidu(string city) {
var resultDict = new Dictionary<string, string>();
const string url = "http://api.map.baidu.com/telematics/v3/weather?location=";
var key = ConfigurationManager.AppSettings["apikey_baidu"];
var requestUrl = url + city + "&output=json&ak=" + key;
var request = (HttpWebRequest) WebRequest.Create(requestUrl);
var request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Method = "GET";
string result;
try
{
var response = (HttpWebResponse) request.GetResponse();
try {
var response = (HttpWebResponse)request.GetResponse();
using (var reader =
new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException(),
Encoding.UTF8))
{
Encoding.UTF8)) {
result = reader.ReadToEnd();
}
}
catch (Exception exception)
{
resultDict.Add("Exception", exception.Message);
catch (Exception exception) {
resultDict.Add("exception", exception.Message);
return resultDict;
}

var resultJo = JObject.Parse(result);
var status = (string) resultJo.SelectToken("error");
if (status != "0")
{
resultDict.Add("Exception", "error code");
var status = (string)resultJo.SelectToken("error");
if (status != "0") {
resultDict.Add("exception", "error code");
return resultDict;
}
var pm25Str = resultJo.SelectToken("pm25").ToString();
var resultToken = resultJo.SelectToken("results")[0];
var pm25 = resultToken.SelectToken("pm25").ToString();
resultDict.Add("pm25", pm25);

var weatherToken = resultToken.SelectToken("weather_data");

var todayToken = weatherToken[0];
var todayTemp = todayToken.SelectToken("temperature").ToString();
var todayTempFormat = FormatTemp(todayTemp);
var todayWeather = todayToken.SelectToken("weather").ToString();
var todayDayPicUrl = todayToken.SelectToken("dayPictureUrl").ToString();
var todayNightPicUrl = todayToken.SelectToken("nightPictureUrl").ToString();
resultDict.Add("today temp", todayTempFormat);
resultDict.Add("today weather", todayWeather);
resultDict.Add("today day pic url", todayDayPicUrl);
resultDict.Add("today night pic url", todayNightPicUrl);

var tomorrowToken = weatherToken[1];
var tomorrowTemp = tomorrowToken.SelectToken("temperature").ToString();
var tomorrowTempFormat = FormatTemp(tomorrowTemp);
var tomorrowWeather = tomorrowToken.SelectToken("weather").ToString();
var tomorrowDayPicUrl = tomorrowToken.SelectToken("dayPictureUrl").ToString();
var tomorrowNightPicUrl = tomorrowToken.SelectToken("nightPictureUrl").ToString();
resultDict.Add("tomorrow temp", tomorrowTempFormat);
resultDict.Add("tomorrow weather", tomorrowWeather); ;
resultDict.Add("tomorrow day pic url", tomorrowDayPicUrl);
resultDict.Add("tomorrow night pic url", tomorrowNightPicUrl);

return resultDict;
}

public static string GetLocation()
{
private static string FormatTemp(string tempStr) {
tempStr = tempStr.Replace("", "");
var tempSplit = tempStr.Split('~');
var tempLow = tempSplit[1];
var tempHigh = tempSplit[0];
var result = tempLow + " ~ " + tempHigh + "";
return result;
}

public static string GetLocation() {
const string url = "http://api.map.baidu.com/location/ip?ak=";
var key = ConfigurationManager.AppSettings["apikey_baidu"];
var requestUrl = url + key;
var request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Method = "GET";
string result;
try
{
try {
var response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException(), Encoding.UTF8))
{
using (var reader = new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException(), Encoding.UTF8)) {
result = reader.ReadToEnd();
}
}
catch (Exception)
{
catch (Exception) {
return "Exception";
}
var resultJo = JObject.Parse(result);
var status = (string)resultJo.SelectToken("status");
if (status != "0")
{
if (status != "0") {
return "status code ≠ 0";
}
var contentToken = resultJo.SelectToken("content").SelectToken("address_detail");
Expand All @@ -80,4 +104,4 @@ public static string GetLocation()
return cityName;
}
}
}
}
72 changes: 43 additions & 29 deletions MikuWeather_CS/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 28 additions & 18 deletions MikuWeather_CS/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@
using System.Drawing;
using System.Windows.Forms;

namespace MikuWeather
{
public partial class Form1 : Form
{
namespace MikuWeather {

public partial class Form1 : Form {

// ReSharper disable once FieldCanBeMadeReadOnly.Local
private readonly FormShow _frmShow = new FormShow();

public Form1()
{

private string _city;

public Form1() {
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
private void Form1_Load(object sender, EventArgs e) {
BackColor = Color.FloralWhite;
TransparencyKey = BackColor;

var intScreenY = Screen.PrimaryScreen.WorkingArea.Bottom;
SetBounds(Screen.PrimaryScreen.WorkingArea.Width - 272,
intScreenY - Size.Height + 13,
Size.Width, Size.Height);
var city = DataQuery.GetLocation();
MessageBox.Show(city);
_city = DataQuery.GetLocation();
Update();
}

private void Form1_MouseHover(object sender, EventArgs e) {
_frmShow.SetBounds(Location.X - _frmShow.Width / 2 + Width / 2,
Location.Y - _frmShow.Height,
_frmShow.Width,
_frmShow.SetBounds(Location.X - _frmShow.Width / 2 + Width / 2 - 15,
Location.Y - _frmShow.Height,
_frmShow.Width,
_frmShow.Height);
_frmShow.Show();
}
Expand All @@ -40,15 +40,25 @@ private void Form1_MouseLeave(object sender, EventArgs e) {
_frmShow.Hide();
}

private void CmWebsite_Click(object sender, EventArgs e)
{
private void CmWebsite_Click(object sender, EventArgs e) {
Process.Start("https://github.com/RainySummerLuo/MikuWeather_Windows");
}

private void CmExit_Click(object sender, EventArgs e)
{
private void CmExit_Click(object sender, EventArgs e) {
Close();
Environment.Exit(0);
}

private void CmUpdate_Click(object sender, EventArgs e) {
Update();
}

private new void Update() {
var dict = DataQuery.UpdateData_Baidu(_city);
_frmShow.SetTemp(dict["today temp"], dict["tomorrow temp"]);
_frmShow.SetWeather(dict["today weather"], dict["tomorrow weather"]);
var bitmapPic = _frmShow.SetPic(dict["today day pic url"], dict["today night pic url"], dict["tomorrow day pic url"], dict["tomorrow night pic url"]);
picBox.Image = bitmapPic;
}
}
}
Loading

0 comments on commit 8ada3c8

Please sign in to comment.