<commit_before><?php
require_once('db.inc');

# mysql connect
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbdatabase);
if (mysqli_connect_errno($mysqli)) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error();
}


# Create curl resource 
$ch = curl_init(); 

# Set url 
curl_setopt($ch, CURLOPT_URL, "http://www.instructables.com/contest"); 

# Return the transfer as a string 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

#contains the output string 
$output = curl_exec($ch); 
#var_dump($output);


$conteststart = '/<div id="opened-contests" class="contest-landing-section">(.*?)<div id="previous-contests" class="contest-landing-section">/s';
#$contestjudge = '<div id="judged-contests" class="contest-landing-section">';
#$contestend   = '<div id="recently-closed-contests" class="contest-landing-section">';
#$end =          '<div id="previous-contests" class="contest-landing-section">';
#<div class="contest-preview"><a ><img /></a><div class="cover"><div class="cover-inner"><h1>This contest is open for entries. Closes midnight Jun 22, 2015</h1></div>



preg_match($conteststart, $output, $matches);
$block = $matches[1];
#print "Matches: ";
#print_r($matches[1]);

#Get All:
$contestblock = '/<div class="contest-preview"><a.*?title="(.*?)".*?href="(.*?)".*?<h1>(.*?)<\/h1>.*?(?:<\/div>){4}/s';
preg_match_all($contestblock, $block, $contests);
#print_R($contests);


$contestlookup = array();
$res = $mysqli->query("SELECT cid, contesturl FROM contests ORDER BY cid DESC");
for ($row_no = 0; $row_no < $res->num_rows; $row_no++){
    $row = mysqli_fetch_assoc($res);
    $contestlookup["'".$row['contesturl']."'"] = $row['cid'];
}
print_R($contestlookup);


for ($i=0; $i < sizeof($contests[0]); $i++){
    #For Each:
    $contests[1][$i] = $mysqli->real_escape_string($contests[1][$i]);
    $contests[2][$i] = $mysqli->real_escape_string($contests[2][$i]);


    #Iterate 
    # 1 = title 2 = url 3= date
    print $contests[1][$i] ." - " . $contests[2][$i] . " = ". $contests[3][$i] . "\n";

    $datepattern = '/(\w+ \d{1,2}, \d{4})/';
    preg_match($datepattern, $contests[3][$i], $m);
    $dpat= date('Y-m-d',strtotime($m[1]));
    $dpat = $mysqli->real_escape_string($dpat);
    $cid = 0;
    if (array_key_exists("'".$contests[2][$i]."'", $contestlookup)) {
       $cid = $contestlookup["'".$contests[2][$i]."'"];
       printf("Found: %d\n", $cid);
    } else {
       #insert = new Contest
       if (!$mysqli->query("Insert into contests (contestname, contesturl, contestends, creation_time) values ('" . $contests[1][$i]."', '".$contests[2][$i]."', '". $dpat. "', NOW())")){ 
      	  printf("Error: %s\n", $mysqli->sqlstate);
	  printf("Errormessage: %s\n", $mysqli->error);
       }
       
       if ($notify == true){
         mail ( $email , "New Contest" , "New Contest: $contests[1][$i], Ends: $dpat");
       }
       printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows);	   
       $contestlookup[$contests[2][$i]] = $mysqli->insert_id;
       $cid = $mysqli->insert_id;
    }   

    #open Contest, get Entries:
    # set url
    curl_setopt($ch, CURLOPT_URL, "http://www.instructables.com" . $contests[2][$i]);

    #return the transfer as a string<commit_msg>Fixed Email to have title, not 'Array[x]' item<commit_after><?php
require_once('db.inc');

# mysql connect
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbdatabase);
if (mysqli_connect_errno($mysqli)) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error();
}


# Create curl resource 
$ch = curl_init(); 

# Set url 
curl_setopt($ch, CURLOPT_URL, "http://www.instructables.com/contest"); 

# Return the transfer as a string 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

#contains the output string 
$output = curl_exec($ch); 
#var_dump($output);


$conteststart = '/<div id="opened-contests" class="contest-landing-section">(.*?)<div id="previous-contests" class="contest-landing-section">/s';
#$contestjudge = '<div id="judged-contests" class="contest-landing-section">';
#$contestend   = '<div id="recently-closed-contests" class="contest-landing-section">';
#$end =          '<div id="previous-contests" class="contest-landing-section">';
#<div class="contest-preview"><a ><img /></a><div class="cover"><div class="cover-inner"><h1>This contest is open for entries. Closes midnight Jun 22, 2015</h1></div>



preg_match($conteststart, $output, $matches);
$block = $matches[1];
#print "Matches: ";
#print_r($matches[1]);

#Get All:
$contestblock = '/<div class="contest-preview"><a.*?title="(.*?)".*?href="(.*?)".*?<h1>(.*?)<\/h1>.*?(?:<\/div>){4}/s';
preg_match_all($contestblock, $block, $contests);
#print_R($contests);


$contestlookup = array();
$res = $mysqli->query("SELECT cid, contesturl FROM contests ORDER BY cid DESC");
for ($row_no = 0; $row_no < $res->num_rows; $row_no++){
    $row = mysqli_fetch_assoc($res);
    $contestlookup["'".$row['contesturl']."'"] = $row['cid'];
}
print_R($contestlookup);


for ($i=0; $i < sizeof($contests[0]); $i++){
    #For Each:
    $contests[1][$i] = $mysqli->real_escape_string($contests[1][$i]);
    $contests[2][$i] = $mysqli->real_escape_string($contests[2][$i]);


    #Iterate 
    # 1 = title 2 = url 3= date
    print $contests[1][$i] ." - " . $contests[2][$i] . " = ". $contests[3][$i] . "\n";

    $datepattern = '/(\w+ \d{1,2}, \d{4})/';
    preg_match($datepattern, $contests[3][$i], $m);
    $dpat= date('Y-m-d',strtotime($m[1]));
    $dpat = $mysqli->real_escape_string($dpat);
    $cid = 0;
    if (array_key_exists("'".$contests[2][$i]."'", $contestlookup)) {
       $cid = $contestlookup["'".$contests[2][$i]."'"];
       printf("Found: %d\n", $cid);
    } else {
       #insert = new Contest
       if (!$mysqli->query("Insert into contests (contestname, contesturl, contestends, creation_time) values ('" . $contests[1][$i]."', '".$contests[2][$i]."', '". $dpat. "', NOW())")){ 
      	  printf("Error: %s\n", $mysqli->sqlstate);
	  printf("Errormessage: %s\n", $mysqli->error);
       }
       
       if ($notify == true){
         mail ( $email , "New Contest" , "New Contest: ".$contests[1][$i].", Ends: $dpat");
       }
       printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows);	   
       $contestlookup[$contests[2][$i]] = $mysqli->insert_id;
       $cid = $mysqli->insert_id;
    }   

    #open Contest, get Entries:
    # set url
    curl_setopt($ch, CURLOPT_URL, "http://www.instructables.com" . $contests[2][$i]);

    #return the transfer as a string
{'fraction_non_alphanumeric': 0.23013823484733403, 'fraction_numerical': 0.01276013975391159, 'mean_word_length': 4.318255250403877, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3548780', 'n_tokens_mistral': 2634, 'n_tokens_neox': 2532, 'n_words': 458}