-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebParser.cs
187 lines (168 loc) · 4.73 KB
/
WebParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
using Microsoft.Edge.SeleniumTools;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace StreamLabs_Helper
{
class WebParser
{
private static IWebDriver webDriver;
Actions mouseActions;
private MoveDirection moveDirection;
private const string backSlash = "\\";
private string driverDirectory;
private string[] edgeArguments = new string[] {
"headless", "disable-gpu", "window-size=1600,1200", "disable-extensions", "mute-audio", "enable-logging=false" };
public enum ParsingStatus
{
Success = 1,
Failure = 0,
FatalError = -1
}
private enum Direction
{
Right = 1,
Left = -1,
None = 0
}
private struct MoveDirection
{
private Direction realValue;
public Direction Direction
{
get //toggle the direction on every get
{
var beforeInvert = realValue;
realValue = (Direction)(-1 * (int)realValue);
return beforeInvert;
}
set { realValue = value; }
}
}
public WebParser()
{
driverDirectory = GetApplicationExecutableDirectoryName() + backSlash + "Drivers";
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.UseChromium = true;
edgeOptions.AddArguments(edgeArguments);
webDriver = new Microsoft.Edge.SeleniumTools.EdgeDriver(driverDirectory, edgeOptions);
moveDirection.Direction = Direction.Right;
}
public async Task<ParsingStatus> FindIFrameOnPage(string url)
{
WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(5));
try {
webDriver.Navigate().GoToUrl(url);
}
catch {
ProgramManager.Error("URL does not exist", true);
return ParsingStatus.FatalError;
}
try
{
var button = webDriver.FindElement(By.CssSelector(".ui.green.button"));
if (button is null)
return ParsingStatus.FatalError;
button.Click(); //find and click the button to join the watch2gether room
}
catch {
ProgramManager.Error("not an accessible Watch2Gether url");
return ParsingStatus.Failure;
}
try {
var collection = await FindElements(By.ClassName("w2g-player-video"));
var iframes = await FindElementsFromElement(By.CssSelector("iframe"), collection.First());
webDriver.SwitchTo().Frame(iframes.First());
mouseActions = new Actions(webDriver);
var mainVideo = await FindElements(By.CssSelector("body"));
mouseActions.MoveToElement(mainVideo.First());
mouseActions.Build().Perform();
}
catch {
ProgramManager.Error("failed to parse");
return ParsingStatus.Failure;
}
return ParsingStatus.Success;
}
public async Task<string> GetIFrameTitle()
{
try
{
if (webDriver is not null)
{
//moves the mouse back and forth to keep the youtube title visible
mouseActions = new Actions(webDriver);
mouseActions.MoveByOffset(10 * ((int)moveDirection.Direction), 0);
mouseActions.Build().Perform();
var title = await FindElements(By.CssSelector(".ytp-chrome-top"));
string titleText = title.First().Text;
//returns what is before the newline to remove "Watch Later" and "Share"
var strArray = titleText.Split('\n');
return strArray[0];
}
else
return null;
}
catch
{
ProgramManager.Error("failed to get iframe");
return null;
}
}
//loop until the elements load
private Task<IReadOnlyCollection<IWebElement>> FindElements(By by)
{
int callCounter = 0;
Task<IReadOnlyCollection<IWebElement>> task = Task<IReadOnlyCollection<IWebElement>>.Factory.StartNew(() =>
{
while (webDriver is not null && callCounter < 8000) //should time out after 8 sec
{
var elements = webDriver.FindElements(by);
if (elements.Count > 0)
return elements;
Thread.Sleep(10);
callCounter++;
}
return null;
});
return task;
}
//same as above, but search from specific node
private Task<IReadOnlyCollection<IWebElement>> FindElementsFromElement(By by, IWebElement element)
{
int callCounter = 0;
Task<IReadOnlyCollection<IWebElement>> task = Task<IReadOnlyCollection<IWebElement>>.Factory.StartNew(() =>
{
while (webDriver is not null && callCounter < 8000) //should time out after 8 sec
{
var elements = element.FindElements(by);
if (elements.Count > 0)
return elements;
Thread.Sleep(10);
callCounter++;
}
return null;
});
return task;
}
private string GetApplicationExecutableDirectoryName()
{
return Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
}
public void Destroy()
{
webDriver.Quit();
}
~WebParser()
{
webDriver.Quit();
}
}
}