twitchdrops_miner 0.3.6

Automatically watches Twitch streams and claims Time-Based Drops for selected games
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::{collections::{BinaryHeap, HashSet}, error::Error, sync::Arc, time::Duration};

use tokio::sync::{Mutex, Notify, broadcast::{self, Sender}, watch::Receiver};

use futures_util::{SinkExt, StreamExt};
use serde_json::{Value, json};
use tokio::time::sleep;
use tokio_tungstenite::{connect_async, tungstenite::Message};
use tracing::debug;
use twitch_gql_rs::{TwitchClient, structs::{Channels, DropCampaigns, GameDirectory}};

use crate::{retry, r#static::{ALLOW_CHANNELS, CHANNEL_IDS, Channel, DEFAULT_CHANNELS, retry_backup}};

const UPDATE_TIME: u64 = 15;
const MAX_TOPICS: usize = 40;
const WS_URL: &'static str = "wss://pubsub-edge.twitch.tv/v1";

pub async fn filter_streams (client: Arc<TwitchClient>, campaigns: Arc<Vec<DropCampaigns>>) {
    let mut count = 0;
    let mut video_vec = HashSet::new();
    for campaign in campaigns.iter() {
        let campaign_details = retry!(client.get_campaign_details(&campaign.id));
        if let Some(allow) = campaign_details.allow.channels {
            let mut allow_channels = ALLOW_CHANNELS.lock().await;
            let allow: HashSet<Channels> = allow.into_iter().collect();
            allow_channels.insert(campaign.id.to_string(), allow.clone());
            drop(allow_channels);
            for channel in allow {
                let stream_info = if let Ok(stream) = client.get_stream_info(&channel.name).await {
                    stream
                } else {
                    continue;
                };

                if stream_info.stream.is_some() {
                    if count >= MAX_TOPICS {
                        break;
                    }
                    let avaiable_drops = retry!(client.get_available_drops_for_channel(&channel.id));
                    if avaiable_drops.viewerDropCampaigns.is_some() {
                        video_vec.insert(Channel { channel_id: channel.id, channel_login: channel.name });
                        count += 1
                    }
                }
            }
        } else {
            let game_directory = retry!(client.get_game_directory(&campaign_details.game.slug, 30, true));
            let mut all_default = DEFAULT_CHANNELS.lock().await;
            let game_directory: HashSet<GameDirectory> = game_directory.into_iter().collect();
            all_default.insert(campaign.id.to_string(), game_directory.clone());
            drop(all_default);
            for channel in game_directory {
                let stream_info = if let Ok(stream) = client.get_stream_info(&channel.broadcaster.login).await {
                    stream
                } else {
                    continue;
                };
                if count >= MAX_TOPICS {
                    break;
                }
                if stream_info.stream.is_some() {
                    let available_drops = retry!(client.get_available_drops_for_channel(&channel.broadcaster.id));
                    if available_drops.viewerDropCampaigns.is_some() {
                        video_vec.insert(Channel { channel_id: channel.broadcaster.id, channel_login: channel.broadcaster.login });
                        count += 1
                    }
                }
            }
        }
    }
    let mut lock = CHANNEL_IDS.lock().await;
    *lock = video_vec;
    drop(lock);
    debug!("Drop lock video");
    spawn_ws(client.access_token.clone().unwrap()).await;

    tokio::spawn(async move {
        loop {
            let lock = CHANNEL_IDS.lock().await;
            let count = lock.len();
            drop(lock);
            if count < MAX_TOPICS {
                let mut to_add = HashSet::new();
                for campaign in campaigns.iter() {
                    let allow_channels = ALLOW_CHANNELS.lock().await;
                    if let Some(channels) = allow_channels.get(&campaign.id) {
                        for channel in channels {
                            if to_add.len() + count  >= MAX_TOPICS {
                                break;
                            }
                            let stream_info = if let Ok(channel) = client.get_stream_info(&channel.name).await {
                                channel
                            } else {
                                continue;
                            };

                            if stream_info.stream.is_some() {
                                let available_drops = retry!(client.get_available_drops_for_channel(&channel.id));
                                if available_drops.viewerDropCampaigns.is_some() {
                                    to_add.insert(Channel { channel_id: channel.id.clone(), channel_login: channel.name.clone() });
                                }
                            }
                        }
                    } else {
                        let mut default_channels = DEFAULT_CHANNELS.lock().await;
                        let slug = retry!(client.get_slug(&campaign.game.displayName));
                        let game_directory = retry!(client.get_game_directory(&slug, 30, true));
                        let game_directory: HashSet<GameDirectory> = game_directory.into_iter().collect();
                        default_channels.insert(campaign.id.clone(), game_directory.clone());
                        
                        for channel in &game_directory {
                            if to_add.len() + count >= MAX_TOPICS {
                                break;
                            }

                            let stream_info = if let Ok(stream) = client.get_stream_info(&channel.broadcaster.login).await {
                                stream
                            } else {
                                continue;
                            };

                            if stream_info.stream.is_some() {
                                let available_drops = retry!(client.get_available_drops_for_channel(&channel.broadcaster.id));
                                if available_drops.viewerDropCampaigns.is_some() {
                                    to_add.insert(Channel { channel_id: channel.broadcaster.id.clone(), channel_login: channel.broadcaster.login.clone() });
                                }
                            }
                            
                        }
                        drop(default_channels);
                    }

                    if to_add.len() + count >= MAX_TOPICS {
                        break;
                    }

                }

                let mut lock = CHANNEL_IDS.lock().await;
                let mut cur = lock.len();
                for channel in to_add {
                    if cur >= MAX_TOPICS { 
                        break 
                    };
                    lock.insert(channel);
                    cur += 1;
                }
                drop(lock);
            }
            debug!("Drop ids");
            sleep(Duration::from_secs(UPDATE_TIME)).await
        }
    });
}

//ws_logick
async fn spawn_ws (auth_token: String) {
    tokio::spawn(async move {
        loop {
            let (ws_stream, _) = retry!(connect_async(WS_URL));
            let (mut write, mut read) = ws_stream.split();
            let mut send_channels: HashSet<Channel> = HashSet::new();
            loop {
                let channel_ids = CHANNEL_IDS.lock().await;
                let new_channels: Vec<Channel> = channel_ids.iter().filter(|id| !send_channels.contains(*id)).cloned().collect();
                let delete_channels: Vec<Channel> = send_channels.iter().filter(|id| !channel_ids.contains(&id)).cloned().collect();

                if !new_channels.is_empty() {
                    let topics: Vec<String> = new_channels.iter().map(|channel| format!("video-playback-by-id.{}", channel.channel_id)).collect();
                    let payload = json!({
                        "type": "LISTEN",
                        "data": {
                            "topics": topics,
                            "auth_token": auth_token
                        }
                    });
                    let payload = serde_json::to_string(&payload).unwrap();
                    let payload = tokio_tungstenite::tungstenite::Message::Text(payload.into());
                    write.send(payload).await.unwrap_or_else(|e| tracing::error!("Failed to send payload to WebSocket: {e}"));
                    send_channels.extend(new_channels);
                }

                if !delete_channels.is_empty() {
                    let delete_topics: Vec<String> = delete_channels.iter().map(|channel| format!("video-playback-by-id.{}", channel.channel_id)).collect();
                    let payload = json!({
                        "type": "UNLISTEN",
                        "data": {
                            "topics": delete_topics,
                            "auth_token": auth_token
                        }
                    });
                    let payload = serde_json::to_string(&payload).unwrap();
                    let payload = tokio_tungstenite::tungstenite::Message::Text(payload.into());
                    write.send(payload).await.unwrap_or_else(|e| tracing::error!("Failed to send payload to WebSocket: {e}"));
                    for delete in delete_channels {
                        send_channels.remove(&delete);
                    }
                };

                if let Some(msg) = read.next().await {
                    match msg {
                        Ok(Message::Text(text)) => {
                            if text.contains("\"type\":\"PING\"") {
                                let pong = Message::Text("{\"type\":\"PONG\"}".into());
                                write.send(pong).await.unwrap();
                            }
                            let json: Value = serde_json::from_str(&text).unwrap();
                            if let Some(err) = json.get("error").and_then(|e| e.as_str()) {
                                if !err.is_empty() {
                                    tracing::error!("{err}")
                                }
                                continue;
                            } else {
                                let data = check_json(&json, "data").unwrap_or_else(|e| {tracing::error!("{e}"); &Value::Null});
                                let message = check_json(&data, "message").unwrap_or_else(|e| {tracing::error!("{e}"); &Value::Null}).as_str().unwrap_or_default();
                                let topic = check_json(data, "topic").unwrap_or_else(|e| { tracing::error!("{e}"); &Value::Null }).as_str().unwrap_or_default();
                                let message_json: Value = serde_json::from_str(&message).unwrap();
                                if let Some(viewers) = message_json.get("viewers").and_then(|s| s.as_u64()) {
                                    if viewers == 0 {
                                        if let Some(id_str) = topic.split('.').last() {
                                            let mut channel_ids = CHANNEL_IDS.lock().await;
                                            if let Some(to_remove) = channel_ids.iter().find(|channel| channel.channel_id == id_str).cloned() {
                                                channel_ids.remove(&to_remove);
                                            }
                                            send_channels.retain(|channel| channel.channel_id != id_str );
                                        }
                                    }
                                } else {
                                    if let Some(id_str) = topic.split('.').last() {
                                        let mut channel_ids = CHANNEL_IDS.lock().await;
                                        if let Some(to_remove) = channel_ids.iter().find(|channel| channel.channel_id == id_str).cloned() {
                                            channel_ids.remove(&to_remove);
                                        };
                                        send_channels.retain(|channel| channel.channel_id != id_str );
                                    }
                                }
                            }

                        },
                        Ok(Message::Ping(ping)) => write.send(Message::Pong(ping)).await.unwrap_or_else(|e| tracing::error!("Failed to send PONG to WebSocket: {e}")),
                        Ok(_) => {},
                        Err(_) => {
                            sleep(Duration::from_secs(UPDATE_TIME)).await;
                            break ;
                        } 
                    }
                }
            }
        }
        
        
    });
}

fn check_json<'a>(v: &'a Value, data: &str) -> Result<&'a Value, Box<dyn Error>> {
    if let Some(key) = v.get(&data) {
        return Ok(key);
    } else {
        return Err(format!("Failed to find '{}' in JSON", data))?;
    }
}

#[derive(PartialEq, Eq, Clone)]
struct Priority {
    priority: u32,
    name: Channel
}

impl Ord for Priority {
    fn cmp (&self, other: &Self) -> std::cmp::Ordering {
        self.priority.cmp(&other.priority).then_with(|| self.name.channel_id.cmp(&other.name.channel_id))
    }
}

impl PartialOrd for Priority {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

async fn send_now_watched (mut rx: Receiver<BinaryHeap<Priority>>, tx_now_watch: broadcast::Sender<Channel>, notify: Arc<Notify>, tx_for_delete: tokio::sync::watch::Sender<Channel>) {
    tokio::spawn(async move {
        loop {
            if let Ok(_) = rx.changed().await {
                let watch = rx.borrow().clone();
                    if let Some(max) = watch.peek() {
                        debug!("Send: {}", max.name.channel_login);
                        if let Err(e) = tx_now_watch.send(Channel { channel_id: max.name.channel_id.to_string(), channel_login: max.name.channel_login.to_string() }) {
                            tracing::error!("{e}")
                        };
                        notify.notified().await;

                        loop {
                            if let Err(e) = tx_for_delete.send(max.name.clone()) {
                                tracing::error!("{e}");
                                sleep(Duration::from_secs(5)).await;
                                continue;
                            } else {
                                break;
                            }
                        }

                        sleep(Duration::from_secs(5)).await;
                        continue;
                    } else {
                        sleep(Duration::from_secs(5)).await;
                        continue;
                    }  
                
            }
            

        }

    });
}

pub async fn update_stream (tx_now_watch: Sender<Channel>, notify: Arc<Notify>) {
    tokio::spawn(async move {
        let mut old_channel_ids: HashSet<Channel> = HashSet::new();
        let mut watched: HashSet<Channel> = HashSet::new();

        let (tx, rx) = tokio::sync::watch::channel(BinaryHeap::new());
        let (tx_for_delete, mut rx_for_delete) = tokio::sync::watch::channel(Channel::default());

        send_now_watched(rx, tx_now_watch, notify, tx_for_delete).await;

        let channel_to_delete = Arc::new(Mutex::new(Channel::default()));
        let channel_to_delete_clone = Arc::clone(&channel_to_delete);

        tokio::spawn(async move {
            loop {
                if let Ok(_) = rx_for_delete.changed().await {
                    let channel = rx_for_delete.borrow().clone();
                    let mut lock = channel_to_delete.lock().await;
                    *lock = channel
                }
            }
        });

        loop {
            let channel_ids = CHANNEL_IDS.lock().await.clone();
            let allow_channels = ALLOW_CHANNELS.lock().await.clone();
            let default_channels = DEFAULT_CHANNELS.lock().await.clone();

            if channel_ids.is_empty() || (allow_channels.is_empty() && default_channels.is_empty()) {
                sleep(Duration::from_secs(UPDATE_TIME)).await;
                continue;
            }

            let offline: Vec<Channel> = old_channel_ids.difference(&channel_ids).cloned().collect();
            for ch in offline {
                watched.remove(&ch);
            }

            let just_watched = {
                let guard = channel_to_delete_clone.lock().await;
                if *guard != Channel::default() {
                    guard.clone()
                } else {
                    Channel::default()
                }
            };
            if just_watched != Channel::default() {
                watched.insert(just_watched.clone());
            }

            let mut new_heap: BinaryHeap<Priority> = BinaryHeap::new();
            for channel in &channel_ids {
                if watched.contains(channel) {
                    continue;
                }

                let mut prio = 0;

                let is_allow = allow_channels.iter().any(|(_, allow_set)| {
                    allow_set.iter().any(|s| s.id == channel.channel_id)
                });
                if is_allow {
                    prio = 3;
                } else {
                    let is_default = default_channels.iter().any(|(_, def_set)| {
                        def_set.iter().any(|s| s.broadcaster.id == channel.channel_id)
                    });
                    if is_default {
                        prio = 2;
                    }
                }

                if prio > 0 {
                    new_heap.push(Priority {
                        priority: prio,
                        name: channel.clone(),
                    });
                }
            }
            
            old_channel_ids = channel_ids;

            tx.send(new_heap).unwrap();

            sleep(Duration::from_secs(UPDATE_TIME)).await;
        }
    });
}