Post: What's the best function you have made for a tool
04-11-2016, 06:08 PM #1
CrEaTiiOn_420
Can’t trickshot me!
(adsbygoogle = window.adsbygoogle || []).push({}); I would like to see some of the best snippets of code that users of ngu have created and what they are for (games, system, etc.) I can't wait to see what you guys got ✌👌
07-05-2016, 06:04 PM #11
jagex
Gym leader
Part of my app that downloads new shows, this function checks if a new episode has released.

    
class Tracker
{

char[] blackListedCharacters = new char[] { '!', '?' };

public void CheckForNewEpisodes(Dictionary<string, int> anime, TorrentQuality quality)
{
var titles = anime.Keys.ToList();
var episodes = anime.Values.ToList();

var episodeDlLinks = new List<string>();
try
{
using (var driver = new PhantomJSDriver(Dependencies.PhantomJS))
{

driver.Manage().Timeouts().SetPageLoadTimeout(new TimeSpan(0, 0, 2, 0));

Console.Clear();

//i only increments when no episode for the current anime is found
//This allows to go through each anime, find all the available episodes and download them at once which then the program can sleep for an hour before checking again
//This is to reduce network/cpu usage to a minimum
for (int i = 0; i < titles.CountWinky Winky
{


//Navigate to the show page
string animePage = Path.Combine(Dependencies.HSCurrentSeason, titles[i]).ToLower() + "/";
driver.Url = animePage;
if (!driver.Url.Contains(animePage) || driver.Url.Contains("about:blank")) { driver.Navigate().GoToUrl(animePage); Console.WriteLine(driver.Url); }


//Save the html page
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(driver.PageSource);

//Search for the episode with the corresponding quality
int videoQuality = (int)quality;

//Horriblesubs episode number listing less than 10 have a 0 before the number. This will check and add the 0 if the episode is less than 10
string episode = episodes[i] < 10 ? "0" + episodes[i].ToString() : episodes[i].ToString();

//dashes take place of exclamation marks in the HTML document
//So titles with ! need to be replaced with - to be able to find them
string title = titles[i].ToLower();

//Clean up the anime title for querying in the HTML page
if (blackListedCharacters.Any(titles[i].Contains))
{

while (blackListedCharacters.Any(title.Contains))
{
var charToReplace = titles[i].ElementAt(titles[i].IndexOfAny(blackListedCharacters));
title = titles[i].Replace(charToReplace, '-'Winky Winky.ToLower();
}


}
//string title = titles[i].ToLower().Replace("!", "-");

//Create the xPath based on the episode title and episode number
string xPath = String.Concat("//div[@class= " + "'" + "release-links " + title + "-" + episode + "-" + videoQuality + "p" + "'" + "]");
HtmlNode node = doc.DocumentNode.SelectSingleNode(xPath);

//Some animes have multiple revisions for episode 1 which are nammed 01v1 01v2 etc...
if (node == null)
{

for (int j = 1; j <= 3; j++)
{
string episodeVersion = episode + "v" + j.ToString();
xPath = String.Concat("//div[@class= " + "'" + "release-links " + title + "-" + episodeVersion + "-" + videoQuality + "p" + "'" + "]");
node = doc.DocumentNode.SelectSingleNode(xPath);

if (node != null)
{
break;
}
}

if (node == null)
{
int e = Convert.ToInt32(episode) + 1;
Console.WriteLine("Anime: " + title + "episode: " + e + " is not yet released or not found");
i++;
continue;
}


}




HtmlNodeCollection children = node.ChildNodes;

//Retrieve the torrent link
var downloadLinks = children[0].InnerHtml;
var trimBefore = downloadLinks.Substring(downloadLinks.IndexOf("https://www.nyaa.se/?page=download", 0));
var index = trimBefore.IndexOf("\">Torrent</a>");
if (index < 0) { Console.WriteLine("Could not retrieve torrent link"); continue; }
var torrentLink = trimBefore.Substring(0, index).Replace("amp;", string.Empty);
//episodeDlLinks.Add(torrentLink);

//move onto the next episode for the anime
if (DownloadTorrent(torrentLink))
{
Console.WriteLine(title.Replace("-", " ") + " " + episode.ToString() + " downloaded torrent.");
Thread.Sleep(850);
episodes[i] += 1;
FileHandler.UpdateEpisode(titles, episodes);
continue;

}

}
}
}

catch (Selenium.SeleniumException) { Program.RestartApplication(); }
catch (OpenQA.Selenium.WebDriverTimeoutException) { Program.RestartApplication(); }
catch (OpenQA.Selenium.DriverServiceNotFoundException)
{
Console.WriteLine("PhantomJS must be placed in: " + FileHandler.directoryPath);
Console.WriteLine("You can download PhantJS from: " + "https://phantomjs.org/download.html");
Console.ReadLine();
}

}
Last edited by jagex ; 08-08-2016 at 11:42 PM.

The following user thanked jagex for this useful post:

CrEaTiiOn_420
07-06-2016, 11:15 PM #12
Sloth
Banned
Originally posted by 420 View Post
I would like to see some of the best snippets of code that users of ngu have created and what they are for (games, system, etc.) I can't wait to see what you guys got ✌******************



You must login or register to view this content.

I maek d3nk codez
Last edited by Sloth ; 07-08-2016 at 12:57 PM.
07-08-2016, 12:22 AM #13
Jim Halpert
Bounty hunter
Originally posted by Sloth View Post
You must login or register to view this content.

I maek d3nk codez


Bruh your return is stronger than my break.
07-08-2016, 12:39 AM #14
Sloth
Banned
Originally posted by Jim
Bruh your return is stronger than my break.


git fekin rekt sun
07-08-2016, 09:21 AM #15
Toxic
former staff
Originally posted by Sloth View Post
You must login or register to view this content.

I maek d3nk codez


so fuckin dank
08-03-2016, 07:22 PM #16
CrEaTiiOn_420
Can’t trickshot me!
Originally posted by jagex View Post
Part of my app that downloads new shows, this function checks if a new episode has released. Those double if(node == null) wtf was I thinking haha xD

    
public void CheckForNewEpisodes(Dictionary<string, int> anime, TorrentQuality quality)
{
var titles = anime.Keys.ToList();
var episodes = anime.Values.ToList();

var episodeDlLinks = new List<string>();
try
{
using (var driver = new PhantomJSDriver(Dependencies.PhantomJS))
{
driver.Manage().Timeouts().SetPageLoadTimeout(new TimeSpan(0, 0, 2, 0));
Console.Clear();


//i only increments when no episode for the current anime is found
//This allows to go through each anime, find all the available episodes and download them at once which then the program can sleep for an hour before checking again
//This is to reduce network/cpu usage to a minimum
for (int i = 0; i < titles.CountWinky Winky
{


//Navigate to the show page
string animePage = Path.Combine(Dependencies.HSCurrentSeason, titles[i]).ToLower() + "/";

if (!driver.Url.Contains(animePage) || driver.Url.Contains("about:blank")) { driver.Navigate().GoToUrl(animePage); }


//Save the html page
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(driver.PageSource);

//Search for the episode with the corresponding quality
int videoQuality = (int)quality;

//Horriblesubs episode number listing less than 10 have a 0 before the number. This will check and add the 0 if the episode is less than 10
string episode = episodes[i] < 10 ? "0" + episodes[i].ToString() : episodes[i].ToString();

//dashes take place of exclamation marks in the HTML document
//So titles with ! need to be replaced with - to be able to find them
string title = titles[i].ToLower().Replace("!", "-");

//Create the xPath based on the episode title and episode number
string xPath = String.Concat("//div[@class= " + "'" + "release-links " + title + "-" + episode + "-" + videoQuality + "p" + "'" + "]");
HtmlNode node = doc.DocumentNode.SelectSingleNode(xPath);

//Some animes have multiple revisions for episode 1 which are nammed 01v1 01v2 etc...
if (node == null)
{

for (int j = 1; j <= 3; j++)
{
string episodeVersion = episode + "v" + j.ToString();
xPath = String.Concat("//div[@class= " + "'" + "release-links " + title + "-" + episodeVersion + "-" + videoQuality + "p" + "'" + "]");
node = doc.DocumentNode.SelectSingleNode(xPath);

if (node != null)
{
break;
}
}

if (node == null)
{
int e = Convert.ToInt32(episode) + 1;
Console.WriteLine("Anime: " + title + "episode: " + e + " is not yet released or not found" );
i++;
continue;
}


}




HtmlNodeCollection children = node.ChildNodes;

//Retrieve the torrent link
var downloadLinks = children[0].InnerHtml;
var trimBefore = downloadLinks.Substring(downloadLinks.IndexOf("https://www.nyaa.se/?page=download", 0));
var index = trimBefore.IndexOf("\">Torrent</a>");
if (index < 0) { Console.WriteLine("Could not retrieve torrent link"); continue; }
var torrentLink = trimBefore.Substring(0, index).Replace("amp;", string.Empty);
//episodeDlLinks.Add(torrentLink);

//move onto the next episode for the anime
if (DownloadTorrent(torrentLink))
{
Console.WriteLine(title.Replace("-", " ") + " " + episode.ToString() + " downloaded torrent.");
Thread.Sleep(850);
episodes[i] += 1;
FileHandler.UpdateEpisode(titles, episodes);
continue;

}

}
}
}
catch (Selenium.SeleniumException) { Program.RestartApplication(); }
catch (OpenQA.Selenium.WebDriverTimeoutException) { Program.RestartApplication(); }
catch (OpenQA.Selenium.DriverServiceNotFoundException)
{
Console.WriteLine("PhantomJS must be placed in: " + FileHandler.directoryPath);
Console.WriteLine("You can download PhantJS from: " + "https://phantomjs.org/download.html");
Console.ReadLine();
}

}



nice work man looks great and could be very useful for other tv shows

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo