forked from safal-tyagi/web-scraper-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
70 lines (56 loc) · 2.86 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
using System;
using WebScraperModularized.helpers;
using System.Net.Http;
using WebScraperModularized.parsers;
using WebScraperModularized.data;
using System.Collections.Generic;
using Z.Dapper.Plus;
using WebScraperModularized.wrappers;
namespace WebScraperModularized
{
class Program
{
private static readonly HttpClient client = new HttpClient();
static void Main(string[] args)
{
//do dapper entitiy mapping to map objects to DB tables
DapperPlusManager.Entity<URL>().Table("url").Identity(x => x.id);
DapperPlusManager.Entity<Property>().Table("property").Identity(x => x.id);
DapperPlusManager.Entity<PropertyType>().Table("propertytype").Identity(x => x.id);
URL myUrl;//URL to be parsed
//get url from url helper and do basic null checks
while((myUrl = URLHelper.getNextURL())!=null && myUrl.url!=null && myUrl.url.Length>0){
Console.WriteLine("Parsing URL {0}", myUrl.url);//print the current url
try{
var response = client.GetAsync(myUrl.url).Result;//make an HTTP call and get the html for this URL
string content = response.Content.ReadAsStringAsync().Result;//save HTML into string
if(myUrl.urltype == (int)URL.URLType.PROPERTY_URL){
//if the url is of property type, instantiate property parser
PropertyParser parser = new PropertyParser(content, myUrl);
//parse the html
PropertyData propData = parser.parse();
//insert into DB
DBHelper.insertParsedProperties(propData);
Console.WriteLine("Stored {0} properties",
(propData!=null && propData.urlList!=null)?propData.urlList.Count:0);
}
else if(myUrl.urltype == (int)URL.URLType.APARTMENT_URL){
//if the url is of apartment type, instantiate apartment parser
ApartmentParser parser = new ApartmentParser(content, myUrl);
//call the parse method
ApartmentData apartmentData = parser.parse();
DBHelper.insertParsedApartment(apartmentData);
Console.WriteLine("Stored data for property id {0}!", myUrl.property);
}
else{
Console.WriteLine("Unknown URL Type");
}
DBHelper.markURLDone(myUrl);//update the status of URL as done in DB
}
catch(Exception e){
ExceptionHelper.printException(e);
}
}
}
}
}