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

92
LINES

< > BotCompany Repo | #1025311 // YouTube Operator v0.1 [works with a certain screen setup]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1382K of libraries. Click here for Pure Java version (12752L/68K).

1  
!7
2  
3  
!include once #1025225 // Shared IntegralImage
4  
5  
cmodule SegmentScreenshotStream > DynPrintLogAndEnabled {
6  
  ISegmenter segMainVideo, segThumbnails, segSearchBar, segVideoControls;
7  
  Rect mainVideo, searchBar;
8  
  L<Rect> thumbnails, videoControls;
9  
  S searchQuery;
10  
  transient BufferedImage screenshot;
11  
  transient ReliableSingleThread rstAnalyzeScreen = dm_rst(this, r analyzeScreen);
12  
13  
  visual
14  
    centerAndSouthWithMargin(
15  
      withCenteredButtons(super, 
16  
        "Play/Pause", rThread playOrPause,
17  
        jPopDownButton_noText(
18  
        "Move mouse to main video", rThread moveMouseToMainVideo,
19  
          "Click on a thumbnail", rThread clickOnRandomThumbnail,
20  
          "Click in search bar", rThread clickInSearchBar)),
21  
      withMargin(dm_textFieldAndButton searchQuery('youTubeSearch, "Search")));
22  
  
23  
  start-thread {
24  
    dm_watchField enabled(r { if (enabled) dm_enableStreamOfScreenshots() });
25  
    
26  
    segMainVideo ifNull = mandatorySegmenterFromAGIBlue("Find main YT video #1");
27  
    segThumbnails ifNull = mandatorySegmenterFromAGIBlue("Find YT thumbnails #1");
28  
    segSearchBar ifNull = mandatorySegmenterFromAGIBlue("Find YT search bar #1");
29  
    segVideoControls ifNull = mandatorySegmenterFromAGIBlue("YT video controls #1");
30  
31  
    dm_onScreenshot_q(ss -> {
32  
      if (!enabled) ret;
33  
      screenshot = (BufferedImage) get image(ss);
34  
        rstAnalyzeScreen.trigger();
35  
      });
36  
  }
37  
38  
  void analyzeScreen {
39  
    BufferedImage img = screenshot;
40  
    if (img == null) ret;
41  
    
42  
    dm_setFieldAndPrintIfChanged(mainVideo := first_warnIfMultiple(segMainVideo.get(img)));
43  
    if (mainVideo == null) ret with print("No main video found");
44  
    dm_setFieldAndPrintIfChanged(thumbnails := segThumbnails.get(img));
45  
    dm_setFieldAndPrintIfChanged(searchBar := first_warnIfMultiple(segSearchBar.get(img)));
46  
    dm_setFieldAndPrintIfChanged(videoControls:= sortRectsByMiddleX(segVideoControls.get(img)));
47  
  }
48  
49  
  // API
50  
51  
  void moveMouseToMainVideo enter {
52  
    if (mainVideo == null) ret with infoBox("No main video found");
53  
    dm_unblockAutomation();
54  
    dm_moveMouse(+mainVideo); // move to the middle
55  
  }
56  
57  
  void clickOnRandomThumbnail enter {
58  
    Rect thumbnail = random(thumbnails);
59  
    if (thumbnail == null) ret with infoBox("No thumbnails found");
60  
    dm_unblockAutomation();
61  
    dm_moveMouseAndClick(+thumbnail);
62  
  }
63  
64  
  void clickInSearchBar enter {
65  
    dm_unblockAutomation();
66  
    dm_moveMouseAndClick(+searchBar);
67  
  }
68  
69  
  void youTubeSearch(S query) enter {
70  
    clickInSearchBar();
71  
    if (dm_hasAutomationInterference()) ret with print("interference");
72  
    automate_ctrlA();
73  
    automate_typeString(query);
74  
    automate_enter();
75  
  }
76  
77  
  /*void youTubeSearch enter {
78  
    inputText("What to search for", voidfunc(S query) { youTubeSearch(query) });
79  
  }*/
80  
81  
  void youTubeSearch enter { youTubeSearch(searchQuery); }
82  
83  
  void playOrPause enter {
84  
    dm_unblockAutomation();
85  
    int safety = 0;
86  
    while (l(videoControls) < 5 && safety++ < 5) {
87  
      moveMouseToMainVideo();
88  
      sleepSeconds(1);
89  
    }
90  
    dm_moveMouseAndClick("Play/pause button", first(videoControls));
91  
  }
92  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025311
Snippet name: YouTube Operator v0.1 [works with a certain screen setup]
Eternal ID of this version: #1025311/34
Text MD5: 34e71179716fa28a85487817f6854abc
Transpilation MD5: 8d18a50e9f2fd0980c0d254d75e11194
Author: stefan
Category:
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-23 22:32:16
Source code size: 3179 bytes / 92 lines
Pitched / IR pitched: No / No
Views / Downloads: 184 / 1096
Version history: 33 change(s)
Referenced in: [show references]