This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadJson.cs
79 lines (67 loc) · 2.27 KB
/
LoadJson.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Mark König, 03/2022
//
using System;
using System.Text;
using System.Net;
using Newtonsoft.Json;
using System.IO;
namespace LoadBingPicture
{
public class LoadJson
{
public class BingPicture
{
public string baseurl;
public string title;
public string description;
public string copyright;
public string thumb;
}
public BingPicture[] BingPictures;
public LoadJson()
{ }
public bool DownloadJson(string path, string culture)
{
BingPictures = new BingPicture[7];
try
{
WebClient client = new WebClient();
var webData = client.DownloadData("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=7&mkt=" + culture);
string webString = Encoding.UTF8.GetString(webData);
// save JSON
using (StreamWriter writetext = new StreamWriter(Path.Combine(path, "bing.json")))
{
writetext.Write(webString);
}
dynamic stuff = JsonConvert.DeserializeObject(webString);
for (int i = 0; i < 7; i++)
{
BingPictures[i] = new BingPicture();
BingPictures[i].baseurl = "https://www.bing.com" + (string)stuff.images[i].urlbase;
BingPictures[i].title = (string)stuff.images[i].title;
// split descripton / copyright
string desc = (string)stuff.images[i].copyright;
int x = desc.LastIndexOf("(");
if (x > 0)
{
BingPictures[i].description = desc.Substring(0, x - 1);
BingPictures[i].copyright = desc.Substring(x + 1, desc.Length - x - 2);
}
else
{
BingPictures[i].description = desc;
BingPictures[i].copyright = string.Empty; ;
}
}
}
catch
{
// somthing wrong?
return false;
}
// fished OK
return true;
}
}
}