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).

!7

!include once #1025225 // Shared IntegralImage

cmodule SegmentScreenshotStream > DynPrintLogAndEnabled {
  ISegmenter segMainVideo, segThumbnails, segSearchBar, segVideoControls;
  Rect mainVideo, searchBar;
  L<Rect> thumbnails, videoControls;
  S searchQuery;
  transient BufferedImage screenshot;
  transient ReliableSingleThread rstAnalyzeScreen = dm_rst(this, r analyzeScreen);

  visual
    centerAndSouthWithMargin(
      withCenteredButtons(super, 
        "Play/Pause", rThread playOrPause,
        jPopDownButton_noText(
        "Move mouse to main video", rThread moveMouseToMainVideo,
          "Click on a thumbnail", rThread clickOnRandomThumbnail,
          "Click in search bar", rThread clickInSearchBar)),
      withMargin(dm_textFieldAndButton searchQuery('youTubeSearch, "Search")));
  
  start-thread {
    dm_watchField enabled(r { if (enabled) dm_enableStreamOfScreenshots() });
    
    segMainVideo ifNull = mandatorySegmenterFromAGIBlue("Find main YT video #1");
    segThumbnails ifNull = mandatorySegmenterFromAGIBlue("Find YT thumbnails #1");
    segSearchBar ifNull = mandatorySegmenterFromAGIBlue("Find YT search bar #1");
    segVideoControls ifNull = mandatorySegmenterFromAGIBlue("YT video controls #1");

    dm_onScreenshot_q(ss -> {
      if (!enabled) ret;
      screenshot = (BufferedImage) get image(ss);
        rstAnalyzeScreen.trigger();
      });
  }

  void analyzeScreen {
    BufferedImage img = screenshot;
    if (img == null) ret;
    
    dm_setFieldAndPrintIfChanged(mainVideo := first_warnIfMultiple(segMainVideo.get(img)));
    if (mainVideo == null) ret with print("No main video found");
    dm_setFieldAndPrintIfChanged(thumbnails := segThumbnails.get(img));
    dm_setFieldAndPrintIfChanged(searchBar := first_warnIfMultiple(segSearchBar.get(img)));
    dm_setFieldAndPrintIfChanged(videoControls:= sortRectsByMiddleX(segVideoControls.get(img)));
  }

  // API

  void moveMouseToMainVideo enter {
    if (mainVideo == null) ret with infoBox("No main video found");
    dm_unblockAutomation();
    dm_moveMouse(+mainVideo); // move to the middle
  }

  void clickOnRandomThumbnail enter {
    Rect thumbnail = random(thumbnails);
    if (thumbnail == null) ret with infoBox("No thumbnails found");
    dm_unblockAutomation();
    dm_moveMouseAndClick(+thumbnail);
  }

  void clickInSearchBar enter {
    dm_unblockAutomation();
    dm_moveMouseAndClick(+searchBar);
  }

  void youTubeSearch(S query) enter {
    clickInSearchBar();
    if (dm_hasAutomationInterference()) ret with print("interference");
    automate_ctrlA();
    automate_typeString(query);
    automate_enter();
  }

  /*void youTubeSearch enter {
    inputText("What to search for", voidfunc(S query) { youTubeSearch(query) });
  }*/

  void youTubeSearch enter { youTubeSearch(searchQuery); }

  void playOrPause enter {
    dm_unblockAutomation();
    int safety = 0;
    while (l(videoControls) < 5 && safety++ < 5) {
      moveMouseToMainVideo();
      sleepSeconds(1);
    }
    dm_moveMouseAndClick("Play/pause button", first(videoControls));
  }
}

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: 246 / 1181
Version history: 33 change(s)
Referenced in: [show references]