It starts to look like something…

This commit is contained in:
VC
2020-03-01 09:35:19 +01:00
parent 52f98690a5
commit b6748975c3
4 changed files with 70 additions and 51 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
target
.config.toml
.last_tweet

60
Cargo.lock generated
View File

@@ -128,12 +128,6 @@ dependencies = [
"iovec",
]
[[package]]
name = "bytes"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1"
[[package]]
name = "c2-chacha"
version = "0.2.3"
@@ -343,7 +337,7 @@ dependencies = [
"serde_derive",
"serde_json",
"sha-1",
"tokio 0.1.22",
"tokio",
"url 1.7.2",
]
@@ -497,7 +491,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5b34c246847f938a410a03c5458c7fee2274436675e76d8b903c08efc29c462"
dependencies = [
"byteorder",
"bytes 0.4.12",
"bytes",
"fnv",
"futures",
"http",
@@ -533,7 +527,7 @@ version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6ccf5ede3a895d8856620237b2f02972c1bbc78d2965ad7fe8838d4a0ed41f0"
dependencies = [
"bytes 0.4.12",
"bytes",
"fnv",
"itoa",
]
@@ -544,7 +538,7 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6741c859c1b2463a423a1dbce98d418e6c3c3fc720fb0d45528657320920292d"
dependencies = [
"bytes 0.4.12",
"bytes",
"futures",
"http",
"tokio-buf",
@@ -562,7 +556,7 @@ version = "0.12.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dbe6ed1438e1f8ad955a4701e9a944938e9519f6888d12d8558b645e247d5f6"
dependencies = [
"bytes 0.4.12",
"bytes",
"futures",
"futures-cpupool",
"h2",
@@ -575,7 +569,7 @@ dependencies = [
"net2",
"rustc_version",
"time",
"tokio 0.1.22",
"tokio",
"tokio-buf",
"tokio-executor",
"tokio-io",
@@ -592,7 +586,7 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a800d6aa50af4b5850b2b0f659625ce9504df908e9733b635720483be26174f"
dependencies = [
"bytes 0.4.12",
"bytes",
"futures",
"hyper",
"native-tls",
@@ -606,7 +600,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad5125567a8328ced412a29a52a3ca0a2520c617d40a8bee62d12d877eec236c"
dependencies = [
"base64",
"bytes 0.4.12",
"bytes",
"cfg-if",
"http",
"httparse",
@@ -956,12 +950,6 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
[[package]]
name = "pin-project-lite"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "237844750cfbb86f67afe27eee600dfbbcb6188d734139b534cbfbf4f96792ae"
[[package]]
name = "pkg-config"
version = "0.3.17"
@@ -1201,7 +1189,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f88643aea3c1343c804950d7bf983bd2067f5ab59db6d613a08e05572f2714ab"
dependencies = [
"base64",
"bytes 0.4.12",
"bytes",
"cookie",
"cookie_store",
"encoding_rs",
@@ -1218,7 +1206,7 @@ dependencies = [
"serde_json",
"serde_urlencoded",
"time",
"tokio 0.1.22",
"tokio",
"tokio-executor",
"tokio-io",
"tokio-threadpool",
@@ -1267,7 +1255,7 @@ dependencies = [
"egg-mode",
"mammut",
"serde",
"tokio 0.2.13",
"tokio",
"toml",
]
@@ -1395,7 +1383,7 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d24114bfcceb867ca7f71a0d3fe45d45619ec47a6fbfa98cb14e14250bfa5d6d"
dependencies = [
"bytes 0.4.12",
"bytes",
]
[[package]]
@@ -1482,7 +1470,7 @@ version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a09c0b5bb588872ab2f09afa13ee6e9dac11e10a0ec9e8e3ba39a5a5d530af6"
dependencies = [
"bytes 0.4.12",
"bytes",
"futures",
"mio",
"num_cpus",
@@ -1500,23 +1488,13 @@ dependencies = [
"tokio-uds",
]
[[package]]
name = "tokio"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa5e81d6bc4e67fe889d5783bd2a128ab2e0cfa487e0be16b6a8d177b101616"
dependencies = [
"bytes 0.5.4",
"pin-project-lite",
]
[[package]]
name = "tokio-buf"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fb220f46c53859a4b7ec083e41dec9778ff0b1851c0942b211edb89e0ccdc46"
dependencies = [
"bytes 0.4.12",
"bytes",
"either",
"futures",
]
@@ -1527,7 +1505,7 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25b2998660ba0e70d18684de5d06b70b70a3a747469af9dea7618cc59e75976b"
dependencies = [
"bytes 0.4.12",
"bytes",
"futures",
"tokio-io",
]
@@ -1569,7 +1547,7 @@ version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57fc868aae093479e3131e3d165c93b1c7474109d13c90ec0dda2a1bbfff0674"
dependencies = [
"bytes 0.4.12",
"bytes",
"futures",
"log",
]
@@ -1609,7 +1587,7 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98df18ed66e3b72e742f185882a9e201892407957e45fbff8da17ae7a7c51f72"
dependencies = [
"bytes 0.4.12",
"bytes",
"futures",
"iovec",
"mio",
@@ -1652,7 +1630,7 @@ version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2a0b10e610b39c38b031a2fcab08e4b82f16ece36504988dcbd81dbba650d82"
dependencies = [
"bytes 0.4.12",
"bytes",
"futures",
"log",
"mio",
@@ -1667,7 +1645,7 @@ version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5076db410d6fdc6523df7595447629099a1fdc47b3d9f896220780fa48faf798"
dependencies = [
"bytes 0.4.12",
"bytes",
"futures",
"iovec",
"libc",

View File

@@ -10,6 +10,8 @@ edition = "2018"
serde = { version = "1.0", features = ["derive"] }
toml = "^0.5"
clap = "^2.33"
tokio = "^0.2"
tokio = "^0.1"
egg-mode = "^0.13"
mammut = "^0.13"

View File

@@ -1,5 +1,8 @@
// std
use std::fs::read_to_string;
use std::{
fs::{read_to_string, write},
error::Error,
};
// clap
use clap::{App, Arg};
@@ -13,13 +16,33 @@ use egg_mode::{
KeyPair,
tweet::{
user_timeline,
Tweet,
},
};
// tokio
use tokio::runtime::current_thread::block_on_all;
/**********
* Generic usage functions
***********/
fn read_state(s: &str) -> Option<u64> {
let state = read_to_string(s);
if let Ok(s) = state {
return s.parse::<u64>().ok();
}
None
}
fn write_state(f: &str, s: u64) -> Result<(), std::io::Error> {
write(f, format!("{}", s))
}
/**********
* Config structure
***********/
/// General configuration Struct
#[derive(Debug, Deserialize)]
pub struct Config {
twitter: TwitterConfig,
@@ -32,9 +55,11 @@ struct TwitterConfig {
consumer_secret: String,
access_key: String,
access_secret: String,
last_tweet_path: String,
}
impl Config {
/// parses configuration from command line and TOML config file
pub fn new() -> Config {
let matches = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
@@ -61,19 +86,32 @@ impl Config {
}
}
/*********
* Main function
*********/
/// This is where the magic happens
pub fn run(config: Config) {
let con_token = KeyPair::new(config.twitter.consumer_key, config.twitter.consumer_secret);
let access_token = KeyPair::new(config.twitter.access_key, config.twitter.access_secret);
let last_tweet_id = read_state(&config.twitter.last_tweet_path);
let con_token = KeyPair::new(String::from(&config.twitter.consumer_key), String::from(&config.twitter.consumer_secret));
let access_token = KeyPair::new(String::from(&config.twitter.access_key), String::from(&config.twitter.access_secret));
let token = Token::Access {
consumer: con_token,
access: access_token,
};
let timeline = user_timeline(&config.twitter.username, false, false, &token)
let (_timeline, mut feed) = block_on_all(user_timeline(&config.twitter.username, false, false, &token)
.with_page_size(200)
.older(None);
.older(last_tweet_id)).unwrap();
timeline.types();
for tweet in &feed {
println!("{}: <@{}> {}", tweet.id, tweet.user.as_ref().unwrap().screen_name, tweet.text);
}
write_state(&config.twitter.last_tweet_path, feed[0].id).unwrap_or_else(|e|
panic!("Cant write the last tweet retrieved: {}", e)
);
}