!7 !include once #1025225 // Shared IntegralImage cmodule SegmentScreenshotStream > DynPrintLogAndEnabled { ISegmenter segMainVideo, segThumbnails, segSearchBar, segVideoControls; Rect mainVideo, searchBar; L 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)); } }