Best Puppeteer-sharp code snippet using PuppeteerSharp.Mobile.Program.Get
HomeController.cs
Source:HomeController.cs
...3244 public IActionResult Privacy()3245 {3246 return View();3247 }3248 [HttpGet("search")]3249 public IActionResult Search()3250 {3251 ;3252 return View("Search");3253 }3254 [HttpPost("addsearch")]3255 public async Task<IActionResult> AddSearchAsync()3256 {3257 var d = new SortedList<System.Numerics.BigInteger, string>();3258 var d2 = new List<string>();3259 string numbSearches = Request.Form["numberOfSearches"];3260 if (numbSearches == "")3261 {3262 var nextd = new SortedList<System.Numerics.BigInteger, string>();...
Chromium.cs
Source:Chromium.cs
...11 using ICSharpCode.SharpZipLib.Zip;12 using PuppeteerSharp;13 public static class Chromium14 {15 public static string[] Args => GetArguments();16 private static string[] GetArguments()17 {18 var arguments = new List<string>19 {20 "--autoplay-policy=user-gesture-required",21 "--disable-background-networking",22 "--disable-background-timer-throttling",23 "--disable-backgrounding-occluded-windows",24 "--disable-breakpad",25 "--disable-client-side-phishing-detection",26 "--disable-component-update",27 "--disable-default-apps",28 "--disable-dev-shm-usage",29 "--disable-domain-reliability",30 "--disable-extensions",31 "--disable-features=AudioServiceOutOfProcess",32 "--disable-hang-monitor",33 "--disable-ipc-flooding-protection",34 "--disable-notifications",35 "--disable-offer-store-unmasked-wallet-cards",36 "--disable-popup-blocking",37 "--disable-print-preview",38 "--disable-prompt-on-repost",39 "--disable-renderer-backgrounding",40 "--disable-setuid-sandbox",41 "--disable-speech-api",42 "--disable-sync",43 "--disk-cache-size=33554432",44 "--hide-scrollbars",45 "--ignore-gpu-blocklist",46 "--metrics-recording-only",47 "--mute-audio",48 "--no-default-browser-check",49 "--no-first-run",50 "--no-pings",51 "--no-sandbox",52 "--no-zygote",53 "--password-store=basic",54 "--use-gl=swiftshader",55 "--use-mock-keychain"56 };57 if (Chromium.GetHeadless())58 {59 arguments.Add("--single-process");60 }61 else62 {63 arguments.Add("--start-maximized");64 }65 return arguments.ToArray();66 }67 static bool GetHeadless()68 {69 string isLocalEnvVar = Environment.GetEnvironmentVariable("IS_LOCAL", target: EnvironmentVariableTarget.Process);70 string offLineEnvVar = Environment.GetEnvironmentVariable("IS_OFFLINE", target: EnvironmentVariableTarget.Process);71 if (isLocalEnvVar != null || offLineEnvVar != null)72 {73 return false;74 }75 IReadOnlyList<string> cloudEnvVars = new string[]76 {77 "AWS_LAMBDA_FUNCTION_NAME",78 "FUNCTION_NAME",79 "FUNCTION_TARGET",80 "FUNCTIONS_EMULATOR"81 };82 return cloudEnvVars.Any(key => Environment.GetEnvironmentVariable(key, target: EnvironmentVariableTarget.Process) != null);83 }84 public static ViewPortOptions GetDefaultViewport()85 {86 var browserOptions = new ViewPortOptions87 {88 DeviceScaleFactor = 1,89 HasTouch = false,90 Height = Chromium.GetHeadless() == true ? 1080 : 0,91 IsLandscape = true,92 IsMobile = true,93 Width = Chromium.GetHeadless() == true ? 1920 : 0,94 };95 return browserOptions;96 }97 public static string GetExecutablePath()98 {99 if (Chromium.GetHeadless() != true)100 {101 return null;102 }103 if (File.Exists("/tmp/chromium"))104 {105 foreach (var file in Directory.GetFiles("/tmp"))106 {107 var onlyFileName = Path.GetFileName(file);108 if (onlyFileName.StartsWith("core.chromium").Equals(true))109 {110 File.Delete($"/tmp/{onlyFileName}");111 }112 }113 return "/tmp/chromium";114 }115 string binFolder = "/opt/nodejs/node_modules/chrome-aws-lambda/bin";116 // === /opt/nodejs/node_modules/chrome-aws-lambda/bin/chromium.br === //117 var chromiumArchive = "chromium.br";118 var chromiumFile = new FileInfo($"{Path.Combine(binFolder, chromiumArchive)}");119 var chromiumExecPath = DecompressBrotli(chromiumFile.FullName, "/tmp"); // inflate(`${input}/chromium.br`)120 RunBashCommands("/bin/bash", "-c \"chmod 777 /tmp/chromium\"");121 string awsLambdaNodejsEnvVar = Environment.GetEnvironmentVariable("AWS_EXECUTION_ENV", target: EnvironmentVariableTarget.Process);122 //if (awsLambdaNodejsEnvVar != null && awsLambdaNodejsEnvVar.Contains("AWS_Lambda_nodejs"))123 //{124 // === /opt/nodejs/node_modules/chrome-aws-lambda/bin/aws.tar.br === //125 var libAwsArchive = "aws.tar.br";126 var libAwsArchivePath = new FileInfo($"{Path.Combine(binFolder, libAwsArchive)}");127 Chromium.DecompressBrotli(libAwsArchivePath.FullName, "/tmp");128 Chromium.DecompressTar("/tmp/aws.tar", "/tmp/aws");129 RunBashCommands("/bin/bash", "-c \"chmod -R 777 /tmp/aws\"");130 //}131 // === /opt/nodejs/node_modules/chrome-aws-lambda/bin/swiftshader.tar.br === //132 var swiftArchive = "swiftshader.tar.br";133 var swiftArchivePath = new FileInfo($"{Path.Combine(binFolder, swiftArchive)}");134 Chromium.DecompressBrotli(swiftArchivePath.FullName, "/tmp");135 Chromium.DecompressTar("/tmp/swiftshader.tar", "/tmp/swiftshader");136 RunBashCommands("/bin/bash", "-c \"chmod -R 777 /tmp/swiftshader\"");137 return chromiumExecPath;138 }139 public static void CleanTmpOnLambda(string directoryPath)140 {141 DirectoryInfo di = new DirectoryInfo(directoryPath);142 foreach (FileInfo file in di.EnumerateFiles())143 {144 file.Delete();145 }146 foreach (DirectoryInfo dir in di.EnumerateDirectories())147 {148 dir.Delete(true);149 }150 }151 public static string DecompressBrotli(string archiveFilePath, string outputDirectoryName)152 {153 if (!Directory.Exists(outputDirectoryName))154 {155 throw new DirectoryNotFoundException($"The directory with the name {outputDirectoryName} was not found!");156 }157 FileInfo fileToDecompress = new FileInfo($"{archiveFilePath}");158 string fullPathTrimed = Path.GetFullPath(fileToDecompress.FullName).TrimEnd(Path.DirectorySeparatorChar);159 string onlyFileName = Path.GetFileName(fullPathTrimed);160 var onlyFileNameWithoutExtension = onlyFileName.Remove(onlyFileName.Length - fileToDecompress.Extension.Length);161 //var newFileName = Path.Combine("/tmp", onlyFileNameWithoutExtension);162 var newFileName = Path.Combine(outputDirectoryName, onlyFileNameWithoutExtension);163 using (FileStream originalFileStream = fileToDecompress.OpenRead())164 {165 using (FileStream outputFileStream = File.Create(newFileName))166 {167 using (var brotliStream = new BrotliStream(originalFileStream, CompressionMode.Decompress))168 {169 brotliStream.CopyTo(outputFileStream);170 }171 }172 }173 return newFileName;174 }175 public static void DecompressTar(string archiveFilePath, string outputDirectoryName, bool keepOldFiles = false)176 {177 FileInfo fileToDecompress = new FileInfo($"{archiveFilePath}");178 using (FileStream originalFileStream = fileToDecompress.OpenRead())179 {180 using (var tarInputStream = new TarInputStream(originalFileStream, Encoding.Default))181 {182 TarEntry entry;183 while ((entry = tarInputStream.GetNextEntry()) != null)184 {185 if (entry.TarHeader.TypeFlag == TarHeader.LF_LINK || entry.TarHeader.TypeFlag == TarHeader.LF_SYMLINK)186 {187 continue;188 }189 string name = entry.Name;190 if (Path.IsPathRooted(name))191 {192 // for UNC names... \\machine\share\zoom\beet.txt gives \zoom\beet.txt193 name = name.Substring(Path.GetPathRoot(name).Length);194 }195 name = name.Replace('/', Path.DirectorySeparatorChar);196 string destFile = Path.Combine(outputDirectoryName, name);197 if (entry.IsDirectory)198 {199 EnsureDirectoryExists(destFile);200 }201 else202 {203 string parentDirectory = Path.GetDirectoryName(destFile);204 EnsureDirectoryExists(parentDirectory);205 bool process = true;206 var fileInfo = new FileInfo(destFile);207 if (fileInfo.Exists)208 {209 if (keepOldFiles)210 {211 Console.Out.WriteLine($"Destination file already exists: {entry}");212 process = false;213 }214 else if ((fileInfo.Attributes & FileAttributes.ReadOnly) != 0)215 {216 Console.Out.WriteLine($"Destination file already exists, and is read-only: {entry}");217 process = false;218 }219 }220 if (process)221 {222 using (var outputStream = File.Create(destFile))223 {224 // If translation is disabled, just copy the entry across directly.225 tarInputStream.CopyEntryContents(outputStream);226 }227 }228 }229 }230 }231 }232 }233 public static string UnZip(string archiveFileFullPath, string destinationFolder)234 {235 string fileName = Path.GetFileName(archiveFileFullPath);236 if (fileName != null)237 {238 File.Copy(archiveFileFullPath, destinationFolder);239 return destinationFolder;240 }241 using (ZipArchive zipArchive = System.IO.Compression.ZipFile.Open(archiveFileFullPath, ZipArchiveMode.Read))242 {243 foreach (ZipArchiveEntry entry in zipArchive.Entries)244 {245 entry.ExtractToFile(destinationFolder, true);246 }247 }248 return destinationFolder;249 }250 public static string RunBashCommands(string fileName, string command)251 {252 string result = string.Empty;253 var process = new Process();254 var processStartInfo = new ProcessStartInfo255 {256 FileName = $"{fileName}",257 Arguments = $"{command}",258 WindowStyle = ProcessWindowStyle.Normal,259 RedirectStandardOutput = true,260 RedirectStandardError = true,261 UseShellExecute = false,262 CreateNoWindow = true,263 // Verb = "runas"264 };265 process.StartInfo = processStartInfo;266 process.Start();267 // process.WaitForExit();268 result = result + $"Test result: {process.StandardOutput.ReadToEnd()}{Environment.NewLine}Test errors: {process.StandardError.ReadToEnd()}";269 process.WaitForExit();270 process.Dispose();271 return result;272 }273 public static void RunCommands(string command)274 {275 string result = string.Empty;276 var process = new Process();277 var processStartInfo = new ProcessStartInfo278 {279 FileName = @"C:\Program Files\Git\git-bash.exe",280 WorkingDirectory = @"C:\Users\Dmytro_Stoliar",281 Arguments = $"{command}",282 WindowStyle = ProcessWindowStyle.Normal,283 RedirectStandardOutput = true,284 RedirectStandardError = true,285 UseShellExecute = false,286 CreateNoWindow = true,287 // Verb = "runas"288 };289 process.StartInfo = processStartInfo;290 process.Start();291 process.WaitForExit();292 process.Dispose();293 }294 public static void UnZipFile(string fileNameFullPath)295 {296 Console.Out.WriteLine($"Name of ZIP: {fileNameFullPath}");297 using (ZipInputStream zipInputStream = new ZipInputStream(File.OpenRead(fileNameFullPath)))298 {299 ZipEntry theEntry;300 // zipInputStream.Position = 0;301 while ((theEntry = zipInputStream.GetNextEntry()) != null)302 {303 Console.WriteLine(theEntry.Name);304 string directoryName = Path.GetDirectoryName(theEntry.Name);305 string fileName = Path.GetFileName(theEntry.Name);306 if (directoryName.Length > 0)307 {308 Directory.CreateDirectory(directoryName);309 }310 if (fileName != String.Empty)311 {312 using (FileStream streamWriter = File.Create(theEntry.Name))313 {314 int size = 2048;315 byte[] data = new byte[2048];316 while (true)317 {318 size = zipInputStream.Read(data, 0, data.Length);319 if (size > 0)320 {321 streamWriter.Write(data, 0, size);322 }323 else324 {325 break;326 }327 }328 }329 }330 }331 }332 }333 public static void ExtractTar(string filename, string outputDir)334 {335 using (var stream = File.OpenRead(filename))336 {337 var buffer = new byte[100];338 // store current position here339 long pos = 0;340 while (true)341 {342 pos += stream.Read(buffer, 0, 100);343 var name = Encoding.ASCII.GetString(buffer).Trim('\0');344 if (String.IsNullOrWhiteSpace(name))345 break;346 FakeSeekForward(stream, 24);347 pos += 24;348 pos += stream.Read(buffer, 0, 12);349 var size = Convert.ToInt64(Encoding.UTF8.GetString(buffer, 0, 12).Trim('\0').Trim(), 8);350 FakeSeekForward(stream, 376);351 pos += 376;352 var output = Path.Combine(outputDir, name);353 if (!Directory.Exists(Path.GetDirectoryName(output)))354 Directory.CreateDirectory(Path.GetDirectoryName(output));355 if (!name.Equals("./", StringComparison.InvariantCulture))356 {357 using (var str = File.Open(output, FileMode.OpenOrCreate, FileAccess.Write))358 {359 var buf = new byte[size];360 pos += stream.Read(buf, 0, buf.Length);361 str.Write(buf, 0, buf.Length);362 }363 }364 var offset = (int)(512 - (pos % 512));365 if (offset == 512)366 offset = 0;367 FakeSeekForward(stream, offset);368 pos += offset;369 }370 }371 }372 private static void FakeSeekForward(Stream stream, int offset)373 {374 if (stream.CanSeek)375 stream.Seek(offset, SeekOrigin.Current);376 else377 {378 int bytesRead = 0;379 var buffer = new byte[offset];380 while (bytesRead < offset)381 {382 int read = stream.Read(buffer, bytesRead, offset - bytesRead);383 if (read == 0)384 throw new EndOfStreamException();385 bytesRead += read;386 }387 }388 }389 private static void EnsureDirectoryExists(string directoryName)390 {391 if (!Directory.Exists(directoryName))392 {393 try394 {395 Directory.CreateDirectory(directoryName);396 }397 catch (Exception e)398 {399 throw new TarException("Exception creating directory '" + directoryName + "', " + e.Message, e);400 }401 }402 }403 public static IEnumerable<FileSystemInfo> AllFilesAndFolders(this DirectoryInfo dir)404 {405 foreach (var file in dir.GetFiles())406 {407 yield return file;408 }409 foreach (var directory in dir.GetDirectories())410 {411 yield return directory;412 foreach (var obj in AllFilesAndFolders(directory))413 {414 yield return obj;415 }416 }417 }418 }419}...
ScriptRunner.cs
Source:ScriptRunner.cs
...12 {13 Browser browser;14 Page page;15 public bool IsReady => page != null && !page.IsClosed;16 public IEnumerable<string> GetPotentialExecutablePathsForChrome()17 {18 var userUninstallLocation = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome", "InstallLocation", null) as string;19 if (userUninstallLocation != null) yield return userUninstallLocation;20 var userAppPath = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe", "", null) as string;21 if (userAppPath != null) yield return userAppPath;22 var machineUninstallLocation = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome", "InstallLocation", null) as string;23 if (machineUninstallLocation != null) yield return machineUninstallLocation;24 var machineAppPath = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe", "", null) as string;25 if (machineAppPath != null) yield return machineAppPath;26 yield return Environment.ExpandEnvironmentVariables(@"%ProgramFiles%\Google\Chrome\Application\chrome.exe");27 yield return Environment.ExpandEnvironmentVariables(@"%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe");28 }29 //public IEnumerable<string> GetPotentialExecutablePathForFirefox()30 //{31 // var userAppPath = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe", "", null) as string;32 // if (userAppPath != null) yield return userAppPath;33 // var userVersion = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mozilla\Mozilla Firefox", "CurrentVersion", null) as string;34 // if (userVersion != null)35 // {36 // var userLocation = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Mozilla\Mozilla Firefox\" + userVersion, "PathToExe", null) as string;37 // if (userLocation != null) yield return userLocation;38 // }39 // var machineVersion = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Mozilla\Mozilla Firefox", "CurrentVersion", null) as string;40 // if (machineVersion != null)41 // {42 // var machineLocation = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Mozilla\Mozilla Firefox\" + machineVersion, "PathToExe", null) as string;43 // if (machineLocation != null) yield return machineLocation;44 // }45 // yield return Environment.ExpandEnvironmentVariables(@"%ProgramFiles%\Mozilla Firefox\firefox.exe");46 // yield return Environment.ExpandEnvironmentVariables(@"%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe");47 //}48 public string FindExecutable(Product product)49 {50 switch (product)51 {52 case Product.Chrome:53 return GetPotentialExecutablePathsForChrome().FirstOrDefault(p => File.Exists(p));54 //case Product.Firefox:55 // return GetPotentialExecutablePathForFirefox().FirstOrDefault(p => File.Exists(p));56 default:57 throw new NotSupportedException();58 }59 }60 public async Task Initialize(AutomationProject project)61 {62 var screenWidth = (int)System.Windows.SystemParameters.WorkArea.Width;63 var screenHeight = (int)System.Windows.SystemParameters.WorkArea.Height;64 var defaultViewportWidth = Math.Min(screenWidth - 20, Math.Max(1200, screenWidth / 2));65 var defaultViewportHeight = screenHeight - 180;66 if (browser == null)67 {68 // select browser, prioritize Chrome69 var chromePath = FindExecutable(Product.Chrome);...
Scraping.cs
Source:Scraping.cs
...23 var dummy = await page.WaitForSelectorAsync("div.card a");24 var linkList = await page.QuerySelectorAllAsync("div.card a");25 foreach(var link in linkList)26 {27 var url = await (await link.GetPropertyAsync("href")).JsonValueAsync<string>();28 var title = await(await(await link.QuerySelectorAsync("h4")).GetPropertyAsync("innerText")).JsonValueAsync<string>();29 Console.WriteLine($"Found area: {title}");30 result.Add((url: url, title: title));31 }32 return result;33 }34 public static async Task<List<TaskInfo>> ScanTasks(Page page, IEnumerable<(string url, string title)> areas)35 {36 var result = new List<TaskInfo>();37 foreach (var area in areas)38 {39 if (area.title == "Databases") continue; // Does not support SQL queries.40 if (area.title == "Python") continue; // Does not support multiple choice.41 Console.WriteLine($"Opening area {area.title}");42 var response = await page.GoToAsync(area.url);43 var dummy = await page.WaitForSelectorAsync("div.arcade-map--topic");44 Console.WriteLine($"Scanning area {area.title}");45 var topicList = await page.QuerySelectorAllAsync("div.arcade-map--topic");46 foreach (var topic in topicList)47 {48 var span = await topic.QuerySelectorAsync("span");49 var topicTitle = await (await span.GetPropertyAsync("innerText")).JsonValueAsync<string>();50 var taskList = await topic.QuerySelectorAllAsync("div.arcade-map--topic-details a.arcade-map--task");51 foreach (var task in taskList)52 {53 var taskUrl = await (await task.GetPropertyAsync("href")).JsonValueAsync<string>();54 var solvedText = await (await task.GetPropertyAsync("className")).JsonValueAsync<string>();55 var solved = !solvedText.Contains("-current") && !solvedText.Contains("-not-solved");56 var taskTitle = await (await (await task.QuerySelectorAsync("h3")).GetPropertyAsync("innerText")).JsonValueAsync<string>();57 result.Add(new TaskInfo(area.url, area.title, topicTitle, taskUrl, taskTitle, solved));58 }59 }60 }61 return result;62 }63 public static async Task RetrieveTask(Page page, TaskInfo task)64 {65 Console.Write($"Reading {task.Task} of {task.Topic}.");66 await page.GoToAsync(task.TaskUrl);67 Console.Write(".");68 await page.SetViewportAsync(new ViewPortOptions() { Height = 100000, Width = 8000, HasTouch = false, IsMobile = false });69 Console.Write(".");70 var source = await page.WaitForSelectorAsync("div.view-lines");71 Console.Write(".");72 task.Source = await (await source.GetPropertyAsync("innerText")).JsonValueAsync<string>();73 Console.Write(".");74 await page.SetViewportAsync(new ViewPortOptions() { Height = 800, Width = 1600, HasTouch = false, IsMobile = false });75 Console.Write(".");76 var desc = await page.QuerySelectorAsync("div.markdown");77 Console.Write(".");78 task.Description = await (await desc.GetPropertyAsync("innerText")).JsonValueAsync<string>();79 Console.Write(".");80 var index = task.Description.IndexOf("[C#] Syntax Tips");81 if (index > 0) task.Description = task.Description.Substring(0, index);82 if (Source.TestExists(task, out var _)) return; // No need to read tests.83 task.Tests = new List<string>();84 var list = await page.QuerySelectorAllAsync("div.accordion");85 Console.Write(".");86 foreach (var item in list)87 {88 var btn = await item.QuerySelectorAsync("div.accordion--head");89 Console.Write(".");90 PuppeteerSharp.ElementHandle body;91 do92 {93 await btn.ClickAsync();94 Console.Write(".");95 body = await item.QuerySelectorAsync("div.accordion--body");96 Console.Write(".");97 if (body == null) await Task.Delay(100);98 } while (body == null);99 string test;100 var warning = await body.QuerySelectorAsync("div.task-tests--warning");101 Console.Write(".");102 if (warning != null)103 {104 // The test example is too big, download it.105 var name = await (await btn.GetPropertyAsync("innerText")).JsonValueAsync<string>();106 Console.Write(".");107 name = name.Replace(' ', '-') + ".json";108 name = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", name);109 if (File.Exists(name)) File.Delete(name);110 var download = await warning.QuerySelectorAsync("div.button");111 Console.Write(".");112 await download.ClickAsync();113 Console.Write(".");114 int limit = 80;115 while (limit-- > 0 && !File.Exists(name))116 {117 await Task.Delay(250);118 }119 if (File.Exists(name))120 {121 test = ReadJsonTest(name);122 }123 else124 {125 test = "Failed to download test " + name;126 task.Failed = true;127 }128 }129 else130 {131 var input = await body.QuerySelectorAsync("pre.task-tests--value");132 if (input == null) break;133 test = await (await input.GetPropertyAsync("innerText")).JsonValueAsync<string>();134 Console.Write(".");135 input = await body.QuerySelectorAsync("pre.-answer");136 Console.Write(".");137 test += "\nExpected Output: " + await (await input.GetPropertyAsync("innerText")).JsonValueAsync<string>();138 Console.Write(".");139 }140 task.Tests.Add(test);141 }142 Console.WriteLine("");143 }144 private static string ReadJsonTest(string filename)145 {146 var jsonText = File.ReadAllText(filename);147 var json = JsonSerializer.Deserialize(jsonText, typeof(object)) as JsonElement?;148 StringBuilder result = new StringBuilder();149 json.Value.TryGetProperty("input", out var input);150 foreach (var value in input.EnumerateObject())151 {152 Write(result, value.Name, value.Value);153 }154 json.Value.TryGetProperty("output", out var output);155 Write(result, "Expected Output", output);156 return result.ToString();157 }158 static void Write(StringBuilder result, string name, JsonElement value)159 {160 result.Append(name);161 result.Append(": ");162 var json = value.ToString();163 if (value.ValueKind == JsonValueKind.Array)164 {165 json = Regex.Replace(json, @"(?<=""[^""]*""|-?\w+)\s*,\s*(?=""[^""]*""|-?\w+)", ",");166 json = Regex.Replace(json, @"(?<=\[|-?\w+)\s*(?=""[^""]*""|-?\w+)", "");167 json = Regex.Replace(json, @"(?<=""[^""]*""|-?\w+)\s*(?=\])", "");168 json = Regex.Replace(json, @"(?<=\])\s*,\s*(?=\[)", "," + Environment.NewLine);...
MatchTask.cs
Source:MatchTask.cs
...48 public override async Task<object> ParseAsync(CancellationToken cancellationToken)49 {50 using (var scope = serviceScopeFactory.CreateScope())51 {52 var raceDB = scope.ServiceProvider.GetService<RaceDB.Models.RaceDBContext>();53 var leagues = raceDB.League.Where(x => x.Status == 1).ToList();54 foreach (var league in leagues)55 {56 var totalMatchHtml = await GetMatchListAsync(league.LeagueKey);//await _engine.LoadHtml(string.Format(totalMatchUrl, league.LeagueKey.Trim()), JobTimeout);57 var rawDate = HtmlHandler.GetImplement("Date", totalMatchHtml).Get(_DateFilter) + " 2018";58 var attrs = HtmlHandler.GetImplement("TotalMatchAttrs", totalMatchHtml).GetsAttributes(_totalMatchValueFilter);59 var values = HtmlHandler.GetImplement("TotalLeagueValues", totalMatchHtml).Gets(_totalMatchValueFilter);60 if (values == null)61 {62 continue;63 }64 var matchDataList = (from a in attrs65 from b in values66 where a.Key == b.Key67 where a.Value.Any(x => x.Key == "data-fixtureid")68 select new69 {70 MatchKey = a.Value.Where(x => x.Key == "data-fixtureid").FirstOrDefault().Value,71 MatchValue = b.Value72 });73 var redis = new RedisVoteService<int>(this._fact);74 var date = Convert.ToDateTime(rawDate).ToString("yyyyMMdd");75 76 foreach (var match in matchDataList)77 {78 var matchData = HtmlHandler.GetImplement("matchData", match.MatchValue).Gets(_matchCompetitor);79 var matchDate = HtmlHandler.GetImplement("matchDate", match.MatchValue).Get(_matchDate);80 var gameStartDate = new DateTimeOffset(Convert.ToDateTime(matchDate + " " + rawDate), new TimeSpan(1, 0, 0));81 RaceDB.Models.Match matchModel = new RaceDB.Models.Match();82 var existMatch = raceDB.Match.Where(x => x.MatchKey == match.MatchKey && x.StartDateTime == gameStartDate).FirstOrDefault();83 if (existMatch != null)84 {85 matchModel = existMatch;86 }87 var homeCompetitor = matchData[0];88 var awayCompetitor = matchData[1];89 matchModel.MatchKey = match.MatchKey;90 matchModel.LeagueId = league.LeagueId;91 matchModel.CategoryId = league.CategoryId;92 matchModel.HomeCompetitorName = homeCompetitor;93 matchModel.AwayCompetitorName = awayCompetitor;94 matchModel.Status = 2;95 matchModel.InPlay = false;96 matchModel.SportId = 0;97 matchModel.StartDateTime = gameStartDate;98 matchModel.CreateDate = DateTimeOffset.Now.ToOffset(new TimeSpan(-4, 0, 0));99 matchModel.UpdateDate = DateTimeOffset.Now.ToOffset(new TimeSpan(-4, 0, 0));100 matchModel.ResultStatus = 0;101 if (existMatch == null)102 {103 raceDB.Add(matchModel);104 }105 raceDB.SaveChanges();106 }107 if(redis.GetList($"{date}:matches") == null)108 {109 var matchList = raceDB.Match.Where(x => x.StartDateTime.ToString("yyyyMMdd") == date).Select(x => x.MatchId).ToList();110 redis.SaveList($"{date}:matches", matchList);111 }112 }113 }114 return null;115 }116 protected override int JobTimeout117 {118 get119 {120 return 300000;121 }122 }123 public async Task<string> GetMatchListAsync(string leagueKey){124 var browser = await Puppeteer.LaunchAsync(new LaunchOptions125 {126 ExecutablePath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",127 Headless = false128 });129 var page = await browser.NewPageAsync();130 DeviceDescriptor IPhone = DeviceDescriptors.Get(DeviceDescriptorName.IPhone6);131 var dic = new Dictionary<string, string>();132 dic.Add("Referer", _settings.Bet365.Url.MainPage.ToString());133 dic.Add("Accept-Encoding", "gzip, deflate, br");134 dic.Add("Accept-Language", "zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6");135 dic.Add("Connection", "keep-alive");136 await page.EmulateAsync(IPhone);137 await page.SetRequestInterceptionAsync(true);138 await page.SetExtraHttpHeadersAsync(dic);139 page.Request += async (sender, e) =>140 {141 if (e.Request.ResourceType == ResourceType.Image)142 await e.Request.AbortAsync();143 else144 await e.Request.ContinueAsync();145 };146 var waitUntil = new NavigationOptions();147 waitUntil.WaitUntil = new WaitUntilNavigation[1];148 waitUntil.WaitUntil.Append(WaitUntilNavigation.Networkidle2);149 await page.GoToAsync(_settings.Bet365.Url.MainPage.ToString(), waitUntil);150 var waitOption = new WaitForSelectorOptions151 {152 Timeout = 20000,153 Hidden = true154 };155 var preLoadOuter = await page.WaitForXPathAsync(_settings.Bet365.ElementXpath.PreLoader, waitOption);156 waitOption.Hidden = false;157 var selectSport = await page.WaitForXPathAsync(_settings.Bet365.ElementXpath.Soccer, waitOption);158 Thread.Sleep(5000);159 await selectSport.ClickAsync();160 var selectLeague = await page.WaitForXPathAsync("//*[@data-sportskey='"+leagueKey.Trim()+"']", waitOption);161 Thread.Sleep(1000);162 await selectLeague.ClickAsync();163 //Thread.Sleep(2000);164 var selectMatch = await page.WaitForXPathAsync("//*[@data-fixtureid]", waitOption);165 // await selectMatch.ClickAsync();166 return await page.GetContentAsync();167 }168 }169}...
Program.cs
Source:Program.cs
...26 var fetcher = new BrowserFetcher();27 await fetcher.DownloadAsync(BrowserFetcher.DefaultRevision);28 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()))29 {30 chromeVersion = (await browser.GetVersionAsync()).Split('/').Last();31 }32 Console.WriteLine($"GET {url}");33 var text = await HttpGET(url);34 RootObject json = null;35 try36 {37 json = JsonConvert.DeserializeObject<RootObject>(text);38 }39 catch (Exception ex)40 {41 Console.WriteLine($"FAILED: error parsing response - {ex.Message}");42 }43 var devicePayloads = json.Extensions44 .Where(extension => extension.Type == "emulated-device")45 .Select(extension => extension.Device)46 .ToArray();47 var devices = new List<OutputDevice>();48 foreach (var payload in devicePayloads)49 {50 string[] names;51 if (payload.Title == "iPhone 6/7/8")52 {53 names = new[] { "iPhone 6", "iPhone 7", "iPhone 8" };54 }55 else if (payload.Title == "iPhone 6/7/8 Plus")56 {57 names = new[] { "iPhone 6 Plus", "iPhone 7 Plus", "iPhone 8 Plus" };58 }59 else if (payload.Title == "iPhone 5/SE")60 {61 names = new[] { "iPhone 5", "iPhone SE" };62 }63 else64 {65 names = new[] { payload.Title };66 }67 foreach (var name in names)68 {69 var device = CreateDevice(chromeVersion, name, payload, false);70 var landscape = CreateDevice(chromeVersion, name, payload, true);71 devices.Add(device);72 if (landscape.Viewport.Width != device.Viewport.Width || landscape.Viewport.Height != device.Viewport.Height)73 {74 devices.Add(landscape);75 }76 }77 }78 devices.RemoveAll(device => !device.Viewport.IsMobile);79 devices.Sort((a, b) => a.Name.CompareTo(b.Name));80 WriteDeviceDescriptors(devices);81 WriteDeviceDescriptorName(devices);82 }83 static void WriteDeviceDescriptors(IEnumerable<OutputDevice> devices)84 {85 var builder = new StringBuilder();86 var begin = @"using System.Collections.Generic;87namespace PuppeteerSharp.Mobile88{89 /// <summary>90 /// Device descriptors.91 /// </summary>92 public class DeviceDescriptors93 {94 private static readonly Dictionary<DeviceDescriptorName, DeviceDescriptor> Devices = new Dictionary<DeviceDescriptorName, DeviceDescriptor>95 {96";97 var end = @"98 };99 100 /// <summary>101 /// Get the specified device description.102 /// </summary>103 /// <returns>The device descriptor.</returns>104 /// <param name=""name"">Device Name.</param>105 public static DeviceDescriptor Get(DeviceDescriptorName name) => Devices[name];106 }107}";108 builder.Append(begin);109 builder.AppendJoin(",\n", devices.Select(GenerateCsharpFromDevice));110 builder.Append(end);111 File.WriteAllText(deviceDescriptorsOutput, builder.ToString());112 }113 static void WriteDeviceDescriptorName(IEnumerable<OutputDevice> devices)114 {115 var builder = new StringBuilder();116 var begin = @"namespace PuppeteerSharp.Mobile117{118 /// <summary>119 /// Device descriptor name.120 /// </summary>121 public enum DeviceDescriptorName122 {";123 var end = @"124 }125}";126 builder.Append(begin);127 builder.AppendJoin(",", devices.Select(device =>128 {129 return $@"130 /// <summary>131 /// {device.Name}132 /// </summary>133 {DeviceNameToEnumValue(device)}";134 }));135 builder.Append(end);136 File.WriteAllText(deviceDescriptorNameOutput, builder.ToString());137 }138 static string GenerateCsharpFromDevice(OutputDevice device)139 {140 var w = string.Empty;141 return $@" [DeviceDescriptorName.{DeviceNameToEnumValue(device)}] = new DeviceDescriptor142 {{143 Name = ""{device.Name}"",144 UserAgent = ""{device.UserAgent}"",145 ViewPort = new ViewPortOptions146 {{147 Width = {device.Viewport.Width},148 Height = {device.Viewport.Height},149 DeviceScaleFactor = {device.Viewport.DeviceScaleFactor},150 IsMobile = {device.Viewport.IsMobile.ToString().ToLower()},151 HasTouch = {device.Viewport.HasTouch.ToString().ToLower()},152 IsLandscape = {device.Viewport.IsLandscape.ToString().ToLower()}153 }}154 }}";155 }156 static string DeviceNameToEnumValue(OutputDevice device)157 {158 var output = new StringBuilder();159 output.Append(char.ToUpper(device.Name[0]));160 for (var i = 1; i < device.Name.Length; i++)161 {162 if (char.IsWhiteSpace(device.Name[i]))163 {164 output.Append(char.ToUpper(device.Name[i + 1]));165 i++;166 }167 else168 {169 output.Append(device.Name[i]);170 }171 }172 return output.ToString();173 }174 static OutputDevice CreateDevice(string chromeVersion, string deviceName, RootObject.Device descriptor, bool landscape)175 {176 var devicePayload = LoadFromJSONV1(descriptor);177 var viewportPayload = landscape ? devicePayload.Horizontal : devicePayload.Vertical;178 return new OutputDevice179 {180 Name = deviceName + (landscape ? " landscape" : string.Empty),181 UserAgent = devicePayload.UserAgent.Replace("%s", chromeVersion),182 Viewport = new OutputDevice.OutputDeviceViewport183 {184 Width = viewportPayload.Width,185 Height = viewportPayload.Height,186 DeviceScaleFactor = devicePayload.DeviceScaleFactor,187 IsMobile = devicePayload.Capabilities.Contains("mobile"),188 HasTouch = devicePayload.Capabilities.Contains("touch"),189 IsLandscape = landscape190 }191 };192 }193 static DevicePayload LoadFromJSONV1(RootObject.Device json) => new DevicePayload194 {195 Type = json.Type,196 UserAgent = json.UserAgent,197 Capabilities = json.Capabilities.ToHashSet(),198 DeviceScaleFactor = json.Screen.DevicePixelRatio,199 Horizontal = new ViewportPayload200 {201 Height = json.Screen.Horizontal.Height,202 Width = json.Screen.Horizontal.Width203 },204 Vertical = new ViewportPayload205 {206 Height = json.Screen.Vertical.Height,207 Width = json.Screen.Vertical.Width208 }209 };210 static Task<string> HttpGET(string url) => new HttpClient().GetStringAsync(url);211 }212}...
Get
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp.Mobile;4{5 {6 public static async Task Main(string[] args)7 {8 var program = new PuppeteerSharp.Mobile.Program();9 Console.WriteLine(result);10 }11 public async Task<string> Get(string url)12 {13 var browser = await Puppeteer.LaunchAsync(new LaunchOptions14 {15 Args = new string[] { "--no-sandbox" }16 });17 var page = await browser.NewPageAsync();18 await page.GoToAsync(url);19 var content = await page.GetContentAsync();20 await browser.CloseAsync();21 return content;22 }23 }24}
Get
Using AI Code Generation
1using PuppeteerSharp.Mobile;2using System;3using System.Threading.Tasks;4{5 {6 static void Main(string[] args)7 {8 var task = MainAsync();9 task.Wait();10 Console.WriteLine("Press any key to exit");11 Console.ReadKey();12 }13 static async Task MainAsync()14 {15 var program = new Program();16 await program.Get();17 }18 public async Task Get()19 {20 {21 };22 var browser = await Puppeteer.LaunchAsync(options);23 var page = await browser.NewPageAsync();24 await page.ScreenshotAsync("Google.png");25 await browser.CloseAsync();26 }27 }28}29using PuppeteerSharp.Mobile;30using System;31using System.Threading.Tasks;32{33 {34 static void Main(string[] args)35 {36 var task = MainAsync();37 task.Wait();38 Console.WriteLine("Press any key to exit");39 Console.ReadKey();40 }41 static async Task MainAsync()42 {43 var program = new Program();44 await program.Get();45 }46 public async Task Get()47 {48 {
Get
Using AI Code Generation
1using PuppeteerSharp.Mobile;2using System.Threading.Tasks;3{4 {5 static async Task Main(string[] args)6 {7 await new Program().Get();8 }9 public async Task Get()10 {11 var browser = await Puppeteer.LaunchAsync(new LaunchOptions12 {13 Args = new string[] {14 }15 });16 var page = await browser.NewPageAsync();17 await Task.Delay(5000);18 await browser.CloseAsync();19 }20 }21}22Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "
Get
Using AI Code Generation
1using PuppeteerSharp.Mobile;2{3 static void Main(string[] args)4 {5 }6}7using PuppeteerSharp.Mobile;8{9 static void Main(string[] args)10 {11 }12}13using PuppeteerSharp.Mobile;14{15 static void Main(string[] args)16 {17 }18}19using PuppeteerSharp.Mobile;20{21 static void Main(string[] args)22 {23 }24}25using PuppeteerSharp.Mobile;26{27 static void Main(string[] args)28 {29 }30}31using PuppeteerSharp.Mobile;32{33 static void Main(string[] args)34 {35 }36}37using PuppeteerSharp.Mobile;38{39 static void Main(string[] args)40 {41 }42}43using PuppeteerSharp.Mobile;44{45 static void Main(string[] args)46 {47 }48}49using PuppeteerSharp.Mobile;50{51 static void Main(string[] args)52 {53 }54}
Get
Using AI Code Generation
1var program = new PuppeteerSharp.Mobile.Program();2Console.WriteLine(result);3var program = new PuppeteerSharp.Mobile.Program();4Console.WriteLine(result);5var program = new PuppeteerSharp.Mobile.Program();6Console.WriteLine(result);7var program = new PuppeteerSharp.Mobile.Program();8Console.WriteLine(result);9var program = new PuppeteerSharp.Mobile.Program();10Console.WriteLine(result);11var program = new PuppeteerSharp.Mobile.Program();12Console.WriteLine(result);13var program = new PuppeteerSharp.Mobile.Program();14Console.WriteLine(result);15var program = new PuppeteerSharp.Mobile.Program();16Console.WriteLine(result);17var program = new PuppeteerSharp.Mobile.Program();18Console.WriteLine(result);19var program = new PuppeteerSharp.Mobile.Program();20Console.WriteLine(result);21var program = new PuppeteerSharp.Mobile.Program();22var result = await program.Get("https
Get
Using AI Code Generation
1using PuppeteerSharp.Mobile;2{3 {4 static void Main(string[] args)5 {6 var browser = Puppeteer.Mobile.Program.Get();7 }8 }9}10PuppeteerSharp.Mobile.Program.Get(string executablePath);
Get
Using AI Code Generation
1using PuppeteerSharp.Mobile;2var options = new LaunchOptions { Headless = true };3var program = new Program();4var browser = await program.Get(url, options);5var page = await browser.NewPageAsync();6await page.GoToAsync(url);7var title = await page.GetTitleAsync();8Console.WriteLine(title);9await browser.CloseAsync();
Get
Using AI Code Generation
1var myMobileProgram = new PuppeteerSharp.Mobile.Program();2var myDevice = await myMobileProgram.Get("iPhone 8");3Console.WriteLine(myDevice.Name);4Console.WriteLine(myDevice.UserAgent);5Console.WriteLine(myDevice.ViewPort.Width);6Console.WriteLine(myDevice.ViewPort.Height);7Console.WriteLine(myDevice.ViewPort.DeviceScaleFactor);8Console.WriteLine(myDevice.ViewPort.IsMobile);9Console.WriteLine(myDevice.ViewPort.HasTouch);10Console.WriteLine(myDevice.ViewPort.IsLandscape);11var mobileDevice = await PuppeteerSharp.Mobile.Program.Get("iPhone 8");12{13 Args = new[] { $"--user-agent={mobileDevice.UserAgent}" }14};15var mobileBrowser = await Puppeteer.LaunchAsync(mobileOptions);16var mobilePage = await mobileBrowser.NewPageAsync();17 at PuppeteerSharp.Browser.<>c__DisplayClass25_0.<WaitForTargetAsync>b__0(TargetInfo target)18 at PuppeteerSharp.BrowserContext.<>c__DisplayClass24_0.<WaitForTargetAsync>b__0(TargetInfo target)19 at PuppeteerSharp.Helper.WaitForEventAsync[T](TaskCompletionSource`1 waitForEventTask, Func`2 predicate, Int32 timeout, String taskName)20 at PuppeteerSharp.BrowserContext.WaitForTargetAsync(Func`2 predicate, Int32 timeout)21 at PuppeteerSharp.BrowserContext.NewPageAsync()22 at PuppeteerSharp.Mobile.Program.Main(String[] args) in C:\Users\miguel\source\repos\2\2\Program.cs:line 1223 at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)24 at PuppeteerSharp.BrowserContext.<>c__DisplayClass24_0.<WaitForTargetAsync>b__0(TargetInfo target)25 at PuppeteerSharp.Helper.WaitForEventAsync[T](TaskCompletionSource`1 waitForEventTask,
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!