Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

66
LINES

< > BotCompany Repo | #1029985 // Gazelle_GrabPostsLoop [OK]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5835L/36K).

1  
persistable sclass Gazelle_GrabPostsLoop {
2  
  S server = gazelle_server();
3  
  S _user, _botToken;
4  
  Set<S> fields = litset("_modified", "text");
5  
  long changedAfter;
6  
  double delay = 0.5;
7  
  double errorDelay = 10;
8  
  bool verbose, enabled = true;
9  
  int pollFor = 600; // seconds for each poll
10  
  bool firstGrab = true;
11  
  
12  
  !include #1028121 // setField + change()
13  
  
14  
  run {
15  
    while licensed {
16  
      pcall { if (enabled) grab(); }
17  
      sleepSeconds(delay);
18  
    }
19  
  }
20  
  
21  
  void grab {
22  
    O[] params = litobjectarray(+_user, +_botToken, fields := joinWithSpace(fields),
23  
      +changedAfter, +pollFor);
24  
    print(params := litorderedmap(params));
25  
    Map _result = cast postJSONPage(server + "bot/listPosts",
26  
      params);
27  
    if (verbose) print(+_result);
28  
    
29  
    S error = cast _result.get("error");
30  
    if (error != null) {
31  
      print("Server ERROR: " + error);
32  
      sleepSeconds(errorDelay);
33  
      ret;
34  
    }
35  
    
36  
    L<Map> posts = cast _result.get("result");
37  
    print("Found " + n2(posts, "new post"));
38  
  
39  
      
40  
    L<GazellePost> posts2 = map(posts, p -> GazellePost(p));
41  
    for (GazellePost p : posts2)
42  
      setField(changedAfter := max(changedAfter, p.modified));
43  
      
44  
    if (nempty(posts2))
45  
      handlePosts(posts2);
46  
    firstGrab = false;
47  
  }
48  
  
49  
  swappable void handlePosts(Cl<GazellePost> posts) {
50  
    for (GazellePost p : posts) pcall {
51  
      handlePost(p);
52  
    }
53  
  }
54  
  
55  
  transient swappable void handlePost(GazellePost post) {
56  
  }
57  
  
58  
  void reset { setField(changedAfter := 0L); }
59  
  
60  
  Cl<Long> allPostIDs() {
61  
    Map _result = cast postJSONPage(server + "bot/listPosts",
62  
      +_user, +_botToken);
63  
    L<Map> posts = cast _result.get("result");
64  
    ret allToLong(collect id(posts));
65  
  }
66  
}

Author comment

Began life as a copy of #1029982

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1029985
Snippet name: Gazelle_GrabPostsLoop [OK]
Eternal ID of this version: #1029985/23
Text MD5: 84a4d8d9d36f4e353ceb17d8ced2b741
Transpilation MD5: 258fdf526af74d8665a90fd91b08a6a3
Author: stefan
Category: javax / gazelle.rocks
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-28 22:06:03
Source code size: 1780 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 282 / 620
Version history: 22 change(s)
Referenced in: [show references]