Skip to content

Commit 4f2894e

Browse files
committed
Better OO Implementation. Added an option to write down the URL of non-working videos to a .txt file
1 parent e869ed1 commit 4f2894e

File tree

15 files changed

+323
-282
lines changed

15 files changed

+323
-282
lines changed

Watch2Gether-builder/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<version>1.0</version>
1010

1111
<properties>
12-
<maven.compiler.source>16</maven.compiler.source>
13-
<maven.compiler.target>16</maven.compiler.target>
12+
<maven.compiler.source>17</maven.compiler.source>
13+
<maven.compiler.target>17</maven.compiler.target>
1414
</properties>
1515
<dependencies>
1616
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.felipeflohr.w2gbuilder;
22

3-
import com.felipeflohr.w2gbuilder.guirelated.Frame;
3+
import com.felipeflohr.w2gbuilder.swing.MainFrame;
44

55
public class Main {
66

77
public static void main(String[] args){
8-
new Frame();
8+
new MainFrame();
99
}
10-
1110
}

Watch2Gether-builder/src/main/java/com/felipeflohr/w2gbuilder/guirelated/BuildW2GBtn.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

Watch2Gether-builder/src/main/java/com/felipeflohr/w2gbuilder/guirelated/Frame.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

Watch2Gether-builder/src/main/java/com/felipeflohr/w2gbuilder/guirelated/InsertUsername.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

Watch2Gether-builder/src/main/java/com/felipeflohr/w2gbuilder/guirelated/SelectTxtFile.java

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}

Watch2Gether-builder/src/main/java/com/felipeflohr/w2gbuilder/seleniumrelated/BuildW2G.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

Watch2Gether-builder/src/main/java/com/felipeflohr/w2gbuilder/seleniumrelated/ReadTxtFile.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)