Last active
August 16, 2017 03:51
-
-
Save Freelix/0eb643a753902026982a to your computer and use it in GitHub Desktop.
Parse Steamgifts.com to automatically have a chance to win Steam games based on a games list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
while true | |
do | |
/usr/bin/php SteamGiftsScript.php & | |
printf "\n" | |
date +'%m/%d/%Y %H:%M:%S' | |
printf "\n" | |
sleep 7200 | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// To start script --> nohup ./sg.sh >> log.txt & | |
// To stop script --> ps -ef | |
// then kill -9 pid | |
// Some variables | |
$GLOBALS['user_agent'] = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0'; | |
$GLOBALS['main_url'] = "http://www.steamgifts.com/"; | |
$GLOBALS['numberOfPages'] = 12; | |
// These variables needs to be change when cookie expires | |
$GLOBALS['cookie'] = "PHPSESSID=af3715b65f9ff0ac5c72dc76bdf967afecf75a9965f93e4646f3e62f375b9591f0508b4a9770eb207eda9d0699adc876c0a9030441e871e1e6d1841c947fc287; path:/"; | |
$GLOBALS['xsrf'] = "a75f3fd636826b14507faf5055becb5f"; | |
main(); | |
function main() | |
{ | |
// Variables | |
$file_name = "TheGamesList.txt"; | |
//$GLOBALS['games_list'] = explode("\n", file_get_contents($file_name)); | |
$GLOBALS['games_list'] = file($file_name, FILE_IGNORE_NEW_LINES); | |
// Proceed to the first page | |
doRequests($GLOBALS['main_url'], 1); | |
} | |
function doRequests($current_url, $current_page) | |
{ | |
$html = getRequest($current_url); | |
// Create a new DOM Document to handle scraping | |
$dom = new DOMDocument(); | |
// In HTML5, there is no DTD to check, which will make DOM use the HTML4 Transitional DTD | |
// and that doesnt contain those elements, hence the Warnings | |
libxml_use_internal_errors(true); | |
$res = $dom->loadHTML($html); | |
libxml_use_internal_errors(false); | |
$xpath = new DomXPath($dom); | |
$class = 'giveaway__summary'; | |
$divs = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]"); | |
foreach ($divs as $div) { | |
$node = $div->getElementsByTagName('h2')->item(0)->getElementsByTagName('a')->item(0); | |
$line = $node->nodeValue; | |
if (match($line, $GLOBALS['games_list'])) { | |
// Get the url | |
$game_url = $node->attributes->getNamedItem('href')->nodeValue; | |
$game_url = $GLOBALS['main_url'] . $game_url; | |
echo "Entering " . $game_url . PHP_EOL; | |
// Launch a new request | |
postRequest($game_url); | |
} | |
} | |
// Proceed to the next page | |
if ($current_page < $GLOBALS['numberOfPages']) { | |
++$current_page; | |
$next_url = "http://www.steamgifts.com/giveaways/search?page=" . $current_page; | |
doRequests($next_url, $current_page); | |
} | |
} | |
function getRequest($url) | |
{ | |
// Initialize curl and following options | |
$ch = curl_init(); | |
curl_setopt_array($ch, array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => $url, | |
CURLOPT_USERAGENT => $GLOBALS['user_agent'], | |
CURLOPT_COOKIE => $GLOBALS['cookie'] | |
)); | |
// Grab the html from the page | |
$html = curl_exec($ch); | |
if (!$html) { | |
echo "An error append with the request" . PHP_EOL; | |
exit(); | |
} | |
curl_close($ch); | |
return $html; | |
} | |
function postRequest($url) | |
{ | |
$post_url = 'http://www.steamgifts.com/ajax.php'; | |
$str_start = "/giveaway/"; | |
$str_end = "/"; | |
$do = "entry_insert"; | |
$code = getStringBetween($url, $str_start, $str_end); | |
$fields = array( | |
'xsrf_token' => urlencode($GLOBALS['xsrf']), | |
'do' => urlencode($do), | |
'code' => urlencode($code) | |
); | |
$fields_string = createFieldsString($fields); | |
// Execute post request | |
$ch = curl_init(); | |
curl_setopt_array($ch, array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => $post_url, | |
CURLOPT_POST => count($fields), | |
CURLOPT_POSTFIELDS => $fields_string, | |
CURLOPT_USERAGENT => $GLOBALS['user_agent'], | |
CURLOPT_COOKIE => $GLOBALS['cookie'] | |
)); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
} | |
function createFieldsString($fields) | |
{ | |
$fields_string = ""; | |
foreach ($fields as $key => $value) { | |
$fields_string .= $key . '=' . $value . '&'; | |
} | |
$fields_string = rtrim($fields_string, '&'); | |
return $fields_string; | |
} | |
function contains($str, array $arr) | |
{ | |
foreach ($arr as $a) { | |
if (stripos($str, $a) !== false) | |
return true; | |
} | |
return false; | |
} | |
function match($str, array $arr) | |
{ | |
foreach ($arr as $a) { | |
if ($str === $a) | |
return true; | |
} | |
return false; | |
} | |
function getStringBetween($string, $start, $end) | |
{ | |
$string = " " . $string; | |
$ini = strpos($string, $start); | |
if ($ini == 0) | |
return ""; | |
$ini += strlen($start); | |
$len = strpos($string, $end, $ini) - $ini; | |
return substr($string, $ini, $len); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Far Cry 3 | |
Far Cry 4 | |
Fallout 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment