8
8
using System . Net ;
9
9
//using System.Net;
10
10
using System . Web ;
11
+ using NLog ;
11
12
12
13
namespace BookStore . Models
13
14
{
14
15
public class SearchResult
15
16
{
16
- private static HtmlAgilityPack . HtmlDocument doc = new HtmlAgilityPack . HtmlDocument ( ) ;
17
+ private static HtmlDocument doc = new HtmlDocument ( ) ;
18
+ readonly static log4net . ILog logger = log4net . LogManager . GetLogger ( System . Reflection . MethodBase . GetCurrentMethod ( ) . DeclaringType ) ;
17
19
18
20
public string Title { get ; set ; }
19
21
public string htmlTitle { get ; set ; }
@@ -26,31 +28,31 @@ public static List<string> GetInnerText(List<string> links)
26
28
{
27
29
if ( link . Contains ( "livelib" ) )
28
30
{
29
- doc . LoadHtml ( getRequest ( link ) ) ;
31
+ doc . LoadHtml ( GetRequest ( link ) ) ;
30
32
c = doc . DocumentNode . SelectSingleNode ( "//p[@itemprop='about']" ) ;
31
- if ( c != null ) result . Add ( HttpUtility . HtmlDecode ( c . InnerText ) ) ;
33
+ if ( c != null ) result . Add ( HttpUtility . HtmlDecode ( c . InnerText ) ) ;
32
34
}
33
35
else if ( link . Contains ( "aldebaran" ) )
34
36
{
35
- doc . LoadHtml ( getRequest ( link ) ) ;
37
+ doc . LoadHtml ( GetRequest ( link ) ) ;
36
38
c = doc . DocumentNode . SelectSingleNode ( "//div[@class='annotation clearfix']" ) ;
37
- if ( c != null ) result . Add ( HttpUtility . HtmlDecode ( c . FirstChild . InnerText ) ) ;
39
+ if ( c != null ) result . Add ( HttpUtility . HtmlDecode ( c . FirstChild . InnerText ) ) ;
38
40
}
39
41
else if ( link . Contains ( "loveread" ) )
40
42
{
41
- doc . LoadHtml ( getRequest ( link ) ) ;
43
+ doc . LoadHtml ( GetRequest ( link ) ) ;
42
44
c = doc . DocumentNode . SelectSingleNode ( "//p[@class='span_str']" ) ;
43
45
if ( c != null ) result . Add ( HttpUtility . HtmlDecode ( c . InnerText ) ) ;
44
46
}
45
47
else if ( link . Contains ( "e-reading.club" ) )
46
48
{
47
- doc . LoadHtml ( getRequest ( link ) ) ;
49
+ doc . LoadHtml ( GetRequest ( link ) ) ;
48
50
c = doc . DocumentNode . SelectSingleNode ( "//span[@itemprop='description']" ) ;
49
51
if ( c != null ) result . Add ( HttpUtility . HtmlDecode ( c . InnerText ) ) ;
50
52
}
51
53
else if ( link . Contains ( "litres" ) )
52
54
{
53
- doc . LoadHtml ( getRequest ( link ) ) ;
55
+ doc . LoadHtml ( GetRequest ( link ) ) ;
54
56
c = doc . DocumentNode . SelectSingleNode ( "//div[@itemprop='description']" ) ;
55
57
if ( c != null )
56
58
{
@@ -62,46 +64,65 @@ public static List<string> GetInnerText(List<string> links)
62
64
}
63
65
else if ( link . Contains ( "mybook" ) )
64
66
{
65
- doc . LoadHtml ( getRequest ( link ) ) ;
67
+ doc . LoadHtml ( GetRequest ( link ) ) ;
66
68
c = doc . DocumentNode . SelectSingleNode ( "//div[@class='definition-section']" ) ;
67
69
if ( c != null ) result . Add ( HttpUtility . HtmlDecode ( c . FirstChild . InnerText ) ) ;
68
70
}
69
71
}
70
72
71
73
return result ;
72
74
}
73
- public static string getRequest ( string url )
75
+ public static string GetRequest ( string url )
74
76
{
75
- var httpWebRequest = ( HttpWebRequest ) WebRequest . Create ( url ) ;
76
- httpWebRequest . AllowAutoRedirect = false ; //Запрещаем автоматический редирект
77
- using ( var httpWebResponse = ( HttpWebResponse ) httpWebRequest . GetResponse ( ) )
77
+ try
78
78
{
79
- using ( var stream = httpWebResponse . GetResponseStream ( ) )
79
+ var httpWebRequest = ( HttpWebRequest ) WebRequest . Create ( url ) ;
80
+ httpWebRequest . AllowAutoRedirect = false ; //Запрещаем автоматический редирект
81
+ using ( var httpWebResponse = ( HttpWebResponse ) httpWebRequest . GetResponse ( ) )
80
82
{
81
- using ( var reader = new StreamReader ( stream ) )
83
+ using ( var stream = httpWebResponse . GetResponseStream ( ) )
82
84
{
83
- return reader . ReadToEnd ( ) ;
85
+ using ( var reader = new StreamReader ( stream ) )
86
+ {
87
+ return reader . ReadToEnd ( ) ;
88
+ }
84
89
}
85
90
}
86
91
}
92
+ catch ( WebException ex )
93
+ {
94
+
95
+ logger . Error ( "You exceeded limit" , ex ) ;
96
+ return null ;
97
+ //throw;
98
+ }
99
+
87
100
}
88
- public static IList < SearchResult > getSearch ( string searchText , string cfg = "" )
101
+ public static IList < SearchResult > GetSearch ( string searchText , string cfg = "" )
89
102
{
90
103
string key = "AIzaSyBzcXSZrtK15FFCX8v_Ob-Hcxnc-cVHc-Y" ;
91
104
string cx = "015577388163479462430:16-o3xadmg4" ;
92
105
//string key = "AIzaSyAmaV0ew89918tcxHYXbM0VsVM-G6wRKwY";
93
106
//string cx = "003508446447238917805:tgox65vdhtw";
94
- string google = "https://www.googleapis.com/customsearch/v1?key=" + key + "&cx=" + cx + "&q=" + searchText + "&alt=json" + cfg ;
95
- JObject googleSearch = JObject . Parse ( getRequest ( google ) ) ;
96
- List < JToken > results = googleSearch [ "items" ] . Children ( ) . ToList ( ) ;
97
107
IList < SearchResult > searchResults = new List < SearchResult > ( ) ;
98
- foreach ( JToken result in results )
108
+ string google = "https://www.googleapis.com/customsearch/v1?key=" + key + "&cx=" + cx + "&q=" + searchText + "&alt=json" + cfg ;
109
+ try
110
+ {
111
+ JObject googleSearch = JObject . Parse ( GetRequest ( google ) ) ;
112
+ List < JToken > results = googleSearch [ "items" ] . Children ( ) . ToList ( ) ;
113
+ foreach ( JToken result in results )
114
+ {
115
+ SearchResult searchResult = JsonConvert . DeserializeObject < SearchResult > ( result . ToString ( ) ) ;
116
+ searchResults . Add ( searchResult ) ;
117
+ }
118
+ return searchResults ;
119
+ }
120
+ catch ( Exception )
99
121
{
100
- SearchResult searchResult = JsonConvert . DeserializeObject < SearchResult > ( result . ToString ( ) ) ;
101
- searchResults . Add ( searchResult ) ;
122
+ return searchResults ;
123
+
124
+ //throw;
102
125
}
103
- return searchResults ;
104
126
}
105
-
106
127
}
107
128
}
0 commit comments