|
@@ -0,0 +1,53 @@
|
|
1
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
2
|
+
|
|
3
|
+import java.io.IOException;
|
|
4
|
+import java.util.ArrayList;
|
|
5
|
+
|
|
6
|
+public class MessageScraper implements Runnable{
|
|
7
|
+ private ArrayList<Message> messageList;
|
|
8
|
+ private User user;
|
|
9
|
+ private YouAreEll webber;
|
|
10
|
+
|
|
11
|
+ public MessageScraper(User user, YouAreEll webber) {
|
|
12
|
+ this.user = user;
|
|
13
|
+ this.webber = webber;
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ public void run() {
|
|
17
|
+ this.messageList = new ArrayList<Message>();
|
|
18
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
19
|
+ Message[] incoming = new Message[0];
|
|
20
|
+ String scrape;
|
|
21
|
+ while (true){
|
|
22
|
+ waitN(4000);
|
|
23
|
+ scrape = webber.get_messages(user);
|
|
24
|
+ waitN(100);
|
|
25
|
+ try {
|
|
26
|
+ incoming = objectMapper.readValue(scrape, Message[].class);
|
|
27
|
+ } catch (IOException e) {
|
|
28
|
+ e.printStackTrace();
|
|
29
|
+ }
|
|
30
|
+ for (Message message:incoming) {
|
|
31
|
+ if (newMessageCheck(message)){
|
|
32
|
+ System.out.println("New Message!\n" + message.getFromid() + " says: " + message.getMessage());
|
|
33
|
+ this.messageList.add(message);
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+ }
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ private void waitN(int i) {
|
|
40
|
+ try {
|
|
41
|
+ Thread.sleep(i);
|
|
42
|
+ } catch (InterruptedException e) {
|
|
43
|
+ e.printStackTrace();
|
|
44
|
+ }
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ private boolean newMessageCheck(Message newMessage){
|
|
48
|
+ for (Message message:this.messageList) {
|
|
49
|
+ if (message.getSequence().equals(newMessage.getSequence())) return false;
|
|
50
|
+ }
|
|
51
|
+ return true;
|
|
52
|
+ }
|
|
53
|
+}
|