Skip to content

Commit

Permalink
Adding cronjob file for syncing all data
Browse files Browse the repository at this point in the history
  • Loading branch information
alve89 committed Oct 5, 2021
1 parent 16caed4 commit 3158fae
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 187 deletions.
119 changes: 119 additions & 0 deletions admin/cron.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
define('_JEXEC', 1);
define('JPATH_BASE', '../../../');
require_once JPATH_BASE . 'includes/defines.php';
require_once JPATH_BASE . 'includes/framework.php';
require_once 'helper.php';


// Check if all necessary tables exist in database
$db = JFactory::getDbo();
$prefix = $db->getPrefix();
$availableTables = $db->setQuery('SHOW TABLES')->loadColumn();
$tablesNotFound = false;

if(!array_search($prefix.'tvo_teams', $availableTables) ) {
$tablesNotFound = true;
}
if(!array_search($prefix.'tvo_games', $availableTables) ) {
$tablesNotFound = true;
}
if(!array_search($prefix.'tvo_tables', $availableTables) ) {
$tablesNotFound = true;
}

// Load all relevant (= published) teams from database
if( !$tablesNotFound ) {
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__tvo_teams');
$query->where('published = 1');
$db->setQuery((string) $query);
$alleTeams = $db->loadObjectList();
}
else {
die('ERROR: TABLES NOT FOUND');
}



/*
*
*
* UPDATE #__tvo_games
*
*
*/

echo '<p>Games Data</p>';
foreach($alleTeams as $team) {
$updateNulls = true;

// Retrieve current data from BHV server
$team->gamesData = ComTvoHelper::getCurrentGamesData($team->teamGamesId);

// Define new object to be stored in the database
$object = new stdClass;
$object->teamGamesId = $team->teamGamesId;
$object->gamesData = $team->gamesData;
$object->lastUpdated = date('Y-m-d H:i:s', time());

// Check if team exists in tvo_games table
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->quoteName('teamGamesId'));
$query->from($db->quoteName('#__tvo_games'));
$query->where($db->quoteName('teamGamesId') . '=' . $db->quote($team->teamGamesId) );
$db->setQuery((string) $query);
$db->query();

// Check if team already exists as record in the database. If yes => update, if no => insert
if( $db->getNumRows() > 0 ) {
$result = JFactory::getDbo()->updateObject('#__tvo_games', $object, 'teamGamesId', $updateNulls);
print_r($team->teamGamesId.': UPDATE<br />');
}
else {
$result = JFactory::getDbo()->insertObject('#__tvo_games', $object);
print_r($team->teamGamesId.': INSERT<br />');
}

}

/*
*
*
* UPDATE #__tvo_tables
*
*
*/
echo '<p>Tables Data</p>';
foreach($alleTeams as $team) {
// Define new object to be stored in the database
$team->tablesData = ComTvoHelper::getCurrentTableData($team->teamTableId);
$object = new stdClass;
$object->teamTableId = $team->teamTableId;
$object->tablesData = $team->tablesData;
$object->lastUpdated = date('Y-m-d H:i:s', time());

// Check if team exists in tvo_games table
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->quoteName('teamTableId'));
$query->from($db->quoteName('#__tvo_tables'));
$query->where($db->quoteName('teamTableId') . '=' . $db->quote($team->teamTableId) );
$db->setQuery((string) $query);
$db->query();

// Check if team already exists as record in the database. If yes => update, if no => insert
if( $db->getNumRows() > 0 ) {
$result = JFactory::getDbo()->updateObject('#__tvo_tables', $object, 'teamTableId', $updateNulls);
print_r($team->teamTableId.': UPDATE<br />');
}
else {
$result = JFactory::getDbo()->insertObject('#__tvo_tables', $object);
print_r($team->teamTableId.': INSERT<br />');
}


}
156 changes: 44 additions & 112 deletions admin/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,134 +2,72 @@

class ComTvoHelper {

// Retrieve the current API URL
public static function getCurrentUrl()
{
return "https://api.h4a.mobi/spo/spo-proxy_public.php";
}

public static function getPathToDataFile()
{
return __DIR__ . '/data.json';
}

public static function getSeasonDataFromFile($file)
{
return json_decode(file_get_contents($file));
}


public static function getSeasonDataForTeam($id)
{
$teams = self::getSeasonDataFromFile(self::getPathToDataFile());
foreach($teams as $team)
{
if($team->lvIDPathStr == $id)
{
return $team;
}
}
}




/**
* Retrieves the hello message
*
* @param array $params An object containing the module parameters
*
* @access public
*/
public static function getCurrentIDs($id)
{
// Retrieve all team data for the given ID
public static function getCurrentGamesData($id) {
// create curl ressource
$ch = curl_init();
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, self::getCurrentUrl()."?cmd=data&lvTypeNext=club&lvIDNext=" . $id);
// set url
// club (!) id = 986
curl_setopt($ch, CURLOPT_URL, self::getCurrentUrl()."?cmd=data&lvTypeNext=team&lvIDNext=" . $id);

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');

// $output contains the output string
$output = curl_exec($ch);
// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);
// close curl resource to free up system resources
curl_close($ch);
// Return the result
return $output;
}

public static function getCurrentGames($id)
{
// TVO: 986
// HSG: 1005

// create curl ressource
$ch = curl_init();

// set url
// club (!) id = 986
curl_setopt($ch, CURLOPT_URL, self::getCurrentUrl()."?cmd=data&lvTypeNext=club&lvIDNext=" . $id);

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);
// Return the result
return json_decode($output)[0];
}

}

public static function getLatestScorings($id)
{
// Retrieve all table data for the given ID
public static function getCurrentTableData($id) {
// create curl ressource
$ch = curl_init();

// set url
// club (!) id = 986
curl_setopt($ch, CURLOPT_URL, self::getCurrentUrl()."?cmd=data&lvTypeNext=team&lvIDNext=" . $id);
$ch = curl_init();

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// set url
curl_setopt($ch, CURLOPT_URL, self::getCurrentUrl()."?cmd=data&lvTypeNext=class&subType=table&lvIDNext=" . $id);

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');

// $output contains the output string
$output = curl_exec($ch);
// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);
// Return the result
return json_decode($output)[0];
}

public static function getCurrentUrl()
{
// create curl ressource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "http://www.handball4all.de/api/url/spo_vereine-01.php");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);
// close curl resource to free up system resources
curl_close($ch);
// Return the result
return $output;
}
}


// User defined comparison and sort function
public static function cmp($a, $b)
{
return strcmp($a->gDateTS, $b->gDateTS);
}


public static function varDump($var)
{
echo '<pre>';
var_dump($var);
echo '</pre>';
return;
}


public static function getTimestamp($game)
{

Expand All @@ -143,7 +81,7 @@ public static function getTimestamp($game)
}


/*
/*
*
* Create current score
*
Expand All @@ -169,11 +107,5 @@ public static function score($homegoals, $guestgoals, $homegoals1, $guestgoals1)
return $return;
}

public static function varDump($var)
{
echo '<pre>';
var_dump($var);
echo '</pre>';
return;
}

}
71 changes: 0 additions & 71 deletions admin/tvo.xml

This file was deleted.

Loading

0 comments on commit 3158fae

Please sign in to comment.