Skip to content

Commit

Permalink
[ongoing] MikuWeather C#
Browse files Browse the repository at this point in the history
  • Loading branch information
lzcapp committed Nov 10, 2019
1 parent 131ec76 commit bc3de85
Show file tree
Hide file tree
Showing 54 changed files with 1,174 additions and 133 deletions.
26 changes: 26 additions & 0 deletions MikuWeather_CS/.idea/.idea.MikuWeather/.idea/contentModel.xml

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

4 changes: 4 additions & 0 deletions MikuWeather_CS/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
<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"/>
</appSettings>
</configuration>
83 changes: 83 additions & 0 deletions MikuWeather_CS/DataQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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>();
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);
request.Method = "GET";
string result;
try
{
var response = (HttpWebResponse) request.GetResponse();
using (var reader =
new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException(),
Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
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");
return resultDict;
}
var pm25Str = resultJo.SelectToken("pm25").ToString();
var resultToken = resultJo.SelectToken("results")[0];
var weatherToken = resultToken.SelectToken("weather_data");
return resultDict;
}

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
{
var response = (HttpWebResponse)request.GetResponse();
using (var reader = new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException(), Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
catch (Exception)
{
return "Exception";
}
var resultJo = JObject.Parse(result);
var status = (string)resultJo.SelectToken("status");
if (status != "0")
{
return "status code ≠ 0";
}
var contentToken = resultJo.SelectToken("content").SelectToken("address_detail");
var cityToken = contentToken.SelectToken("city");
var cityName = cityToken.ToString().Split(Convert.ToChar(""))[0];
return cityName;
}
}
}
65 changes: 54 additions & 11 deletions MikuWeather_CS/Form1.Designer.cs

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

33 changes: 30 additions & 3 deletions MikuWeather_CS/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace MikuWeather
{
public partial class Form1 : Form
{
// ReSharper disable once FieldCanBeMadeReadOnly.Local
private readonly FormShow _frmShow = new FormShow();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// throw new System.NotImplementedException();
BackColor = Color.FloralWhite;
TransparencyKey = BackColor;

var intScreenY = Screen.PrimaryScreen.WorkingArea.Bottom;

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

private void Form1_MouseHover(object sender, EventArgs e) {
_frmShow.SetBounds(Location.X - _frmShow.Width / 2 + Width / 2,
Location.Y - _frmShow.Height,
_frmShow.Width,
_frmShow.Height);
_frmShow.Show();
}

private void Form1_MouseLeave(object sender, EventArgs e) {
_frmShow.Hide();
}

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

private void CmExit_Click(object sender, EventArgs e)
{
Close();
Environment.Exit(0);
}
}
}
6 changes: 6 additions & 0 deletions MikuWeather_CS/Form1.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="cmMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="pictureBox1.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down Expand Up @@ -1211,4 +1214,7 @@
UgAAAABJRU5ErkJggg==
</value>
</data>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>62</value>
</metadata>
</root>
Loading

0 comments on commit bc3de85

Please sign in to comment.