forum_notifier/src/main.rs
author unC0Rr
Sun, 07 Apr 2019 23:20:41 +0200
changeset 0 5696442fee1c
child 1 52bbcfcbd850
permissions -rw-r--r--
Add new forum topics notificator
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
     1
use curl::easy::{Easy, WriteError};
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
     2
use curl::Error;
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
     3
use dbus::{BusType, Connection, Message};
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
     4
use log::{debug, info, warn};
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
     5
use scraper::{Html, Selector};
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
     6
use std::fs;
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
     7
use std::str;
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
     8
use std::thread;
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
     9
use std::time;
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    10
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    11
fn inform(url: &str, title: &str, author: &str) {
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    12
    let c = Connection::get_private(BusType::Session).unwrap();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    13
    let mut m = Message::new_method_call(
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    14
        "org.kde.konversation",
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    15
        "/irc",
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    16
        "org.kde.konversation",
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    17
        "say",
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    18
    )
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    19
    .unwrap();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    20
    let msg_str = &format!("[{}] {}: {}", url, author, title)[..];
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    21
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    22
    info!("Notification: {}", msg_str);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    23
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    24
    m.append_items(&["freenode".into(), "#hedgewars".into(), msg_str.into()]);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    25
    let r = c.send(m).unwrap();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    26
}
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    27
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    28
fn handle_page(url: &str, data: &[u8]) -> Result<usize, WriteError> {
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    29
    let page = str::from_utf8(data).unwrap();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    30
    let document = Html::parse_document(page);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    31
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    32
    let title_selector = Selector::parse("title").unwrap();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    33
    let mut titles = document.select(&title_selector);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    34
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    35
    if let Some(title_str) = titles.next() {
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    36
        let t = &title_str.inner_html();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    37
        let title = &t[..t.len() - 12];
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    38
        //info!("Topic is '{}'", title);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    39
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    40
        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();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    41
        let mut authors = document.select(&author_selector);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    42
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    43
        if let Some(author_name) = authors.next() {
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    44
            let author = &author_name.inner_html();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    45
            //info!("Author is '{}'", author);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    46
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    47
            inform(url, title, &author[1..author.len() - 1]);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    48
        } else {
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    49
            warn!("Author not found");
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    50
        }
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    51
    } else {
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    52
        warn!("No title found");
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    53
    }
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    54
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    55
    Ok(data.len())
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    56
}
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    57
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    58
fn query(url: &str) -> Result<(), Error> {
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    59
    let mut easy = Easy::new();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    60
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    61
    easy.url(url)?;
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    62
    easy.fail_on_error(true);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    63
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    64
    let mut transfer = easy.transfer();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    65
    transfer.write_function(|data| handle_page(url, data))?;
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    66
    transfer.perform()
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    67
}
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    68
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    69
fn main() {
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    70
    stderrlog::new()
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    71
        .verbosity(3)
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    72
        .timestamp(stderrlog::Timestamp::Second)
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    73
        .module(module_path!())
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    74
        .init()
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    75
        .unwrap();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    76
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    77
    let short_delay = time::Duration::from_secs(3);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    78
    let long_delay = time::Duration::from_secs(7 * 60);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    79
    let file_name = "/usr/home/unC0Rr/.local/unC0Rr/forumchecker/node.txt";
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    80
    let mut node_id: u32 = fs::read_to_string(file_name).unwrap().parse().unwrap();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    81
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    82
    info!("Starting from node id {}", node_id);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    83
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    84
    loop {
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    85
        debug!("Quering node id {}", node_id);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    86
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    87
        let res = query(&format!("https://www.hedgewars.org/node/{}", node_id));
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    88
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    89
        if let Ok(_) = res {
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    90
            node_id += 1;
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    91
            fs::write(file_name, node_id.to_string()).unwrap();
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    92
            thread::sleep(short_delay);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    93
        } else {
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    94
            thread::sleep(long_delay);
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    95
        }
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    96
    }
5696442fee1c Add new forum topics notificator
unC0Rr
parents:
diff changeset
    97
}