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 ( ) ;

for ( int i = 0 ; i < titles . Count ; )

{

//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 episodes which are nammed 01v1 01v2 02v1 02v2 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 ( "http://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 ;

}