1
+ package com .felipeflohr .w2gbuilder .seleniumbuilder ;
2
+
3
+ import io .github .bonigarcia .wdm .WebDriverManager ;
4
+ import org .openqa .selenium .By ;
5
+ import org .openqa .selenium .TimeoutException ;
6
+ import org .openqa .selenium .WebDriver ;
7
+ import org .openqa .selenium .WebElement ;
8
+ import org .openqa .selenium .chrome .ChromeDriver ;
9
+ import org .openqa .selenium .chrome .ChromeOptions ;
10
+ import org .openqa .selenium .support .ui .ExpectedConditions ;
11
+ import org .openqa .selenium .support .ui .WebDriverWait ;
12
+
13
+ import java .io .File ;
14
+ import java .io .FileNotFoundException ;
15
+ import java .io .FileWriter ;
16
+ import java .io .IOException ;
17
+ import java .time .Duration ;
18
+ import java .util .ArrayList ;
19
+ import java .util .Scanner ;
20
+
21
+ public class Builder {
22
+
23
+ private final File txtLinkFiles ;
24
+ private final String username ;
25
+ private final boolean notWorkingVideosEnabled ;
26
+ private final File notWorkingVideosFile ;
27
+ private final WebDriver driver ;
28
+
29
+ public Builder (File txtLinkFiles , String username , boolean notWorkingVideosEnabled , File notWorkingVideosFile ) throws FileNotFoundException , InterruptedException {
30
+ this .txtLinkFiles = txtLinkFiles ;
31
+ this .username = username ;
32
+ this .notWorkingVideosEnabled = notWorkingVideosEnabled ;
33
+ this .notWorkingVideosFile = notWorkingVideosFile ;
34
+
35
+ ChromeOptions chromeOptions = new ChromeOptions ();
36
+ chromeOptions .addArguments ("--mute-audio" );
37
+
38
+ WebDriverManager .chromedriver ().setup ();
39
+ driver = new ChromeDriver (chromeOptions );
40
+
41
+ buildW2G ();
42
+ }
43
+
44
+ private void buildW2G () throws InterruptedException , FileNotFoundException {
45
+ generateW2GRoom ();
46
+ getVideoURLs ().forEach (video -> {
47
+ try {
48
+ addVideo (video );
49
+ } catch (IOException e ) {
50
+ e .printStackTrace ();
51
+ }
52
+ });
53
+ }
54
+
55
+ private void generateW2GRoom () throws InterruptedException {
56
+ driver .get ("https://w2g.tv/?lang=pt" );
57
+ driver .manage ().window ().maximize ();
58
+
59
+ // Will click to create a room
60
+ WebElement createRoomBtn = driver .findElement (new By .ByXPath ("//*[@id=\" create_room_button\" ]" ));
61
+ createRoomBtn .click ();
62
+
63
+ // Will enter the username
64
+ WebElement usernameInput = driver .findElement (new By .ByXPath ("//*[@id=\" intro-nickname\" ]" ));
65
+ usernameInput .clear ();
66
+ usernameInput .sendKeys (this .username ); // Sets the username to the chosen one
67
+
68
+ // Will click the "Enter room" button
69
+ WebElement enterRoomBtn = driver .findElement (new By .ByXPath ("//*[@id=\" intro-modal\" ]/div[2]/div" ));
70
+ enterRoomBtn .click ();
71
+ Thread .sleep (3000 );
72
+ }
73
+
74
+ private void addVideo (String url ) throws IOException {
75
+ // Selects the video search bar and inserts the video URL
76
+ WebElement videoSearchBar = new WebDriverWait (this .driver , Duration .ofSeconds (5 ))
77
+ .until (ExpectedConditions .elementToBeClickable (By .xpath ("//*[@id=\" search-bar-input\" ]" )));
78
+ videoSearchBar .clear ();
79
+ videoSearchBar .sendKeys (url );
80
+
81
+ // Clicks the button "search"
82
+ WebElement searchBtn = this .driver .findElement (By .xpath ("//*[@id=\" search-bar-form\" ]/div/button" ));
83
+ searchBtn .click ();
84
+
85
+ // Clicks to add the video
86
+ try {
87
+ WebElement videoAddBtn = new WebDriverWait (this .driver , Duration .ofSeconds (5 ))
88
+ .until (ExpectedConditions .elementToBeClickable (
89
+ By .xpath ("//*[@id=\" w2g-search-results\" ]/div[4]/div/div[3]/div[2]" )));
90
+ videoAddBtn .click ();
91
+ }
92
+ catch (TimeoutException e ){
93
+ if (notWorkingVideosEnabled ) {
94
+ FileWriter writer = new FileWriter (notWorkingVideosFile .getAbsolutePath (), true );
95
+ writer .write ("\n " + url );
96
+ writer .close ();
97
+ }
98
+ }
99
+ }
100
+
101
+ private ArrayList <String > getVideoURLs () throws FileNotFoundException {
102
+ final ArrayList <String > links = new ArrayList <>();
103
+
104
+ Scanner scanner = new Scanner (txtLinkFiles );
105
+
106
+ while (scanner .hasNext ()) {
107
+ links .add (scanner .nextLine ());
108
+ }
109
+ scanner .close ();
110
+ return links ;
111
+ }
112
+ }
0 commit comments