-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
183 lines (154 loc) · 6.29 KB
/
Program.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ApiGeoYaMap
{
class Program
{
public static string appRoot;
static void Main(string[] args)
{
try
{
appRoot = AppDomain.CurrentDomain.BaseDirectory;
Console.WriteLine("Программа запущена");
EventLog("************************");
EventLog("Программа запущена");
System.AppDomain.CurrentDomain.UnhandledException += UnhandeldExceptionTrapper;
string api_url = ConfigurationManager.AppSettings["api_url"];
string api_key = ConfigurationManager.AppSettings["api_key"];
string sco = ConfigurationManager.AppSettings["sco"];
var list = Get();
int count = list.Count();
if (count == 0) throw new Exception("В документе нет данных для проверки");
EventLog("Прочитаны строки из файла. Общее количество: " + count);
int index = 1;
foreach (var i in list)
{
try
{
string geo = i.ToString();
string url = api_url + api_key + "&geocode=" + geo + "&format=json&sco=" + sco;
string result = GetJsonGeo(url).Result;
JObject o2 = JObject.Parse(result);
var json = o2.Value<JObject>("response").Value<JObject>("GeoObjectCollection").Value<JArray>("featureMember");
var first = json[0].Value<JObject>("GeoObject").Value<JObject>("metaDataProperty").Value<JObject>("GeocoderMetaData");
string address = first.Value<string>("text");
StatusLine(i.ToString() + " : " + index + " : 1");
Write(i.ToString() + ";" + address);
Console.WriteLine("Обработана строка " + index + " из " + count + " успешно");
}
catch
{
Write(i.ToString() + ";" + "Ошибка при получении данных");
StatusLine(i.ToString() + " : " + index + " : 1");
Console.WriteLine("Обработана строка " + index + " из " + count + " с ошибкой");
}
finally
{
index++;
}
}
EventLog("Программа отработала без ошибок");
}
catch(Exception ex)
{
ErrorLog(ex.Message);
EventLog("Программа отработана с ошибкой");
}
finally
{
CloseApp();
}
}
public static async Task<string> GetJsonGeo(string url)
{
Console.WriteLine("Получаем значение из яндекс апи....");
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.GetAsync(url);
var responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Значение получено");
return responseBody;
}
static void UnhandeldExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
{
try
{
ErrorLog(e.ExceptionObject.ToString());
}
catch
{
}
finally
{
CloseApp();
}
}
public static List<object> Get()
{
var list = new List<object>();
using (StreamReader streamReader = new StreamReader(appRoot + "координаты на проверку.txt", encoding: Encoding.GetEncoding(1251)))
{
string line;
while (!streamReader.EndOfStream)
{
line = streamReader.ReadLine();
list.Add(line);
}
}
return list;
}
public static void Write(string t)
{
using (StreamWriter streamWriter = new StreamWriter(appRoot + "результат проверки.txt", append: true, encoding: Encoding.GetEncoding(1251)))
{
streamWriter.WriteLine(t);
}
}
public static void EventLog(string eventLog)
{
using (StreamWriter streamWriter = new StreamWriter(appRoot + "события.txt", append: true, encoding: Encoding.GetEncoding(1251)))
{
streamWriter.WriteLine(DateTime.Now + " : " + eventLog);
}
}
public static void StatusLine(string statusLine)
{
using (StreamWriter streamWriter = new StreamWriter(appRoot + "статуслайны.txt", append: true, encoding: Encoding.GetEncoding(1251)))
{
streamWriter.WriteLine(statusLine);
}
}
public static void ErrorLog(string Exception)
{
using (StreamWriter streamWriter = new StreamWriter(appRoot + "ошибки.txt", append: true, encoding: Encoding.GetEncoding(1251)))
{
streamWriter.WriteLine(DateTime.Now + " : " + Exception);
streamWriter.WriteLine();
}
}
public static void KillProccess(string proccessName)
{
Process[] processList = Process.GetProcessesByName(proccessName);
foreach (var process in processList)
{
process.Kill();
}
}
public static void CloseApp()
{
Environment.Exit(1);
}
}
}