0
|
1 |
use curl::easy::{Easy, WriteError};
|
|
2 |
use curl::Error;
|
|
3 |
use dbus::{BusType, Connection, Message};
|
|
4 |
use log::{debug, info, warn};
|
|
5 |
use scraper::{Html, Selector};
|
|
6 |
use std::fs;
|
|
7 |
use std::str;
|
|
8 |
use std::thread;
|
|
9 |
use std::time;
|
|
10 |
|
1
|
11 |
struct StateInfo {
|
|
12 |
url: String,
|
|
13 |
previous_title: String,
|
|
14 |
previous_author: String,
|
|
15 |
}
|
|
16 |
|
|
17 |
fn inform(state: &mut StateInfo, title: &str, author: &str) {
|
|
18 |
if state.previous_title == title && state.previous_author == author {
|
|
19 |
return;
|
|
20 |
}
|
|
21 |
|
|
22 |
state.previous_title = title.to_string();
|
|
23 |
state.previous_author = author.to_string();
|
|
24 |
|
0
|
25 |
let c = Connection::get_private(BusType::Session).unwrap();
|
|
26 |
let mut m = Message::new_method_call(
|
|
27 |
"org.kde.konversation",
|
|
28 |
"/irc",
|
|
29 |
"org.kde.konversation",
|
|
30 |
"say",
|
|
31 |
)
|
|
32 |
.unwrap();
|
1
|
33 |
let msg_str = &format!("[{}] {}: {}", state.url, author, title)[..];
|
0
|
34 |
|
|
35 |
info!("Notification: {}", msg_str);
|
|
36 |
|
|
37 |
m.append_items(&["freenode".into(), "#hedgewars".into(), msg_str.into()]);
|
1
|
38 |
let _r = c.send(m).unwrap();
|
0
|
39 |
}
|
|
40 |
|
1
|
41 |
fn handle_page(state: &mut StateInfo, data: &[u8]) -> Result<usize, WriteError> {
|
0
|
42 |
let page = str::from_utf8(data).unwrap();
|
|
43 |
let document = Html::parse_document(page);
|
|
44 |
|
|
45 |
let title_selector = Selector::parse("title").unwrap();
|
|
46 |
let mut titles = document.select(&title_selector);
|
|
47 |
|
|
48 |
if let Some(title_str) = titles.next() {
|
|
49 |
let t = &title_str.inner_html();
|
|
50 |
let title = &t[..t.len() - 12];
|
|
51 |
//info!("Topic is '{}'", title);
|
|
52 |
|
|
53 |
let author_selector = Selector::parse("div.forum-post-wrapper div.forum-post-panel-sub div.author-pane div.author-pane-inner div.author-pane-name-status.author-pane-section div.author-pane-line.author-name").unwrap();
|
|
54 |
let mut authors = document.select(&author_selector);
|
|
55 |
|
|
56 |
if let Some(author_name) = authors.next() {
|
|
57 |
let author = &author_name.inner_html();
|
|
58 |
//info!("Author is '{}'", author);
|
|
59 |
|
1
|
60 |
inform(state, title, &author[1..author.len() - 1]);
|
0
|
61 |
} else {
|
|
62 |
warn!("Author not found");
|
|
63 |
}
|
|
64 |
} else {
|
|
65 |
warn!("No title found");
|
|
66 |
}
|
|
67 |
|
|
68 |
Ok(data.len())
|
|
69 |
}
|
|
70 |
|
1
|
71 |
fn query(state: &mut StateInfo) -> Result<(), Error> {
|
0
|
72 |
let mut easy = Easy::new();
|
|
73 |
|
1
|
74 |
easy.url(&state.url)?;
|
|
75 |
easy.fail_on_error(true)?;
|
0
|
76 |
|
|
77 |
let mut transfer = easy.transfer();
|
1
|
78 |
transfer.write_function(|data| handle_page(state, data))?;
|
0
|
79 |
transfer.perform()
|
|
80 |
}
|
|
81 |
|
|
82 |
fn main() {
|
|
83 |
stderrlog::new()
|
|
84 |
.verbosity(3)
|
|
85 |
.timestamp(stderrlog::Timestamp::Second)
|
|
86 |
.module(module_path!())
|
|
87 |
.init()
|
|
88 |
.unwrap();
|
|
89 |
|
|
90 |
let short_delay = time::Duration::from_secs(3);
|
1
|
91 |
let long_delay = time::Duration::from_secs(100);
|
0
|
92 |
let file_name = "/usr/home/unC0Rr/.local/unC0Rr/forumchecker/node.txt";
|
|
93 |
let mut node_id: u32 = fs::read_to_string(file_name).unwrap().parse().unwrap();
|
1
|
94 |
let mut known_good_id: Option<u32> = None;
|
|
95 |
let mut shift: u32 = 0;
|
|
96 |
|
|
97 |
let mut state = StateInfo {
|
|
98 |
url: String::new(),
|
|
99 |
previous_title: String::new(),
|
|
100 |
previous_author: String::new()
|
|
101 |
};
|
0
|
102 |
|
|
103 |
info!("Starting from node id {}", node_id);
|
|
104 |
|
|
105 |
loop {
|
1
|
106 |
let try_id = node_id + shift;
|
|
107 |
|
|
108 |
debug!("Quering node id {}", try_id);
|
0
|
109 |
|
1
|
110 |
thread::sleep(short_delay);
|
|
111 |
|
|
112 |
state.url = format!("https://www.hedgewars.org/node/{}", try_id);
|
|
113 |
|
|
114 |
let res = query(&mut state);
|
0
|
115 |
|
|
116 |
if let Ok(_) = res {
|
1
|
117 |
if shift > 0 && known_good_id == None {
|
|
118 |
known_good_id = Some(try_id);
|
|
119 |
shift = 0;
|
|
120 |
} else {
|
|
121 |
node_id = try_id + 1;
|
|
122 |
if Some(node_id) == known_good_id {
|
|
123 |
node_id += 1;
|
|
124 |
known_good_id = None;
|
|
125 |
}
|
|
126 |
shift = 0;
|
|
127 |
fs::write(file_name, node_id.to_string()).unwrap();
|
|
128 |
}
|
0
|
129 |
} else {
|
1
|
130 |
if known_good_id == None {
|
|
131 |
shift = (shift + 1) % 11;
|
|
132 |
thread::sleep(long_delay);
|
|
133 |
} else if Some(try_id + 1) == known_good_id {
|
|
134 |
node_id = known_good_id.unwrap() + 1;
|
|
135 |
known_good_id = None;
|
|
136 |
shift = 0;
|
|
137 |
fs::write(file_name, node_id.to_string()).unwrap();
|
|
138 |
} else {
|
|
139 |
shift += 1;
|
|
140 |
}
|
0
|
141 |
}
|
|
142 |
}
|
|
143 |
}
|