-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
125 lines (107 loc) · 5.72 KB
/
Program.cs
File metadata and controls
125 lines (107 loc) · 5.72 KB
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
using System;
using System.IO;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using CLReaderWeb;
namespace CLReader
{
class Program
{
static void Main(string[] args)
{
//Dictionary to hold all matches (key is title)
ContextBag contextBag = new ContextBag();
contextBag.Matches=JsonConvert.DeserializeObject<Matches>(File.ReadAllText("LastMatches.json")); //this is so that last matches persists
Matches lastMatches;
//Start up web server to server resulting html files
StartWebServer(contextBag);
//Loop forever
while(true)
{
lastMatches=contextBag.Matches;
contextBag.Matches = new Matches();
//Read json with search term list (but first get default)
SearchTerm defaultSearchTerm = JsonConvert.DeserializeObject<SearchTerm>(File.ReadAllText("CLSearchDefaultTerm.json"));
List<SearchTerm> searchTermList = JsonConvert.DeserializeObject<List<SearchTerm>>(File.ReadAllText("CLSearchTerms.json"));
Console.WriteLine($"{DateTime.Now}: Woke up and am searching again...");
//Loop through and search for each entry in the search term list
foreach(SearchTerm st in searchTermList)
{
//Load empty slots w/ default search term
st.SetDefaults(defaultSearchTerm);
//Craigslist??
if(st.CLSearch != null)
{
Console.WriteLine($"CL: Price: {st.MinPrice}-{st.MaxPrice} Years: {st.MinYear}-{st.MaxYear} Search: {st.CLSearch}");
//Search all US and CA cities asked for
if(st.USCities != null && st.USCities.Length>0)
CLScraper.Scrape(st,st.USCities,".org",contextBag.Matches,lastMatches);
if(st.CACities != null && st.CACities.Length>0)
CLScraper.Scrape(st,st.CACities,".ca",contextBag.Matches,lastMatches);
}
//Samba??
if(st.SambaSearch != null)
{
Console.WriteLine($"Samba: Price: {st.MinPrice}-{st.MaxPrice} Years: {st.MinYear}-{st.MaxYear} Search: {st.SambaSearch}");
RSSScraper.SearchSamba(st,contextBag.Matches,lastMatches);
}
//Ebay??
if(st.EbaySearch != null)
{
Console.WriteLine($"Ebay: Price: {st.MinPrice}-{st.MaxPrice} Years: {st.MinYear}-{st.MaxYear} Search: {st.EbaySearch}");
RSSScraper.SearchEbay(st,contextBag.Matches,lastMatches);
}
//RV Trader
if(st.RVTraderSearch != null)
{
Console.WriteLine($"RVTrader: Price: {st.MinPrice}-{st.MaxPrice} Years: {st.MinYear}-{st.MaxYear} Search: {st.RVTraderSearch}");
//RVTraderScraper.Scrape(st,contextBag.Matches,lastMatches);
}
//RTV
if(st.RTVSearch != null)
{
Console.WriteLine($"RTV: Price: {st.MinPrice}-{st.MaxPrice} Years: {st.MinYear}-{st.MaxYear} Search: {st.RTVSearch}");
RTVScraper.Scrape(st,contextBag.Matches,lastMatches);
}
//Sports mobile
if(st.SportsMobileSearch != null)
{
Console.WriteLine($"SportsMobile: Price: {st.MinPrice}-{st.MaxPrice} Years: {st.MinYear}-{st.MaxYear} Search: {st.SportsMobileSearch}");
SportsMobileScraper.Scrape(st,contextBag.Matches,lastMatches);
}
}
//Save current ignore
contextBag.Matches.SaveNewIgnoreList();
//Save current matches (these become "last" on startup)
contextBag.Matches.Save();
//System.IO.File.WriteAllText("LastMatches.json", JsonConvert.SerializeObject(contextBag.Matches, Formatting.Indented));
//dump
//matches.DumpItems();
//wait for 3 hours during the day
int hoursToWait = 2;
if(DateTime.Now.Hour >= 22)
hoursToWait = 6;
TimeSpan timeToWait = new TimeSpan(hoursToWait,0,0);
Console.WriteLine($"{DateTime.Now}: Waiting {hoursToWait} hours....");
Thread.Sleep(timeToWait);
Console.WriteLine($"{DateTime.Now}: ok.....done with sleeping...");
}
}
static void StartWebServer(ContextBag contextBag)
{
Console.WriteLine($"{DateTime.Now}: Starting Kestrel...");
var webHostBuilder = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
.UseStartup<Startup>()
.ConfigureServices(services => services.AddSingleton<ContextBag>(contextBag))
.UseUrls("http://*:5001");
var host = webHostBuilder.Build();
host.Start();
}
}
}