magicrebirth/Django and JSON ( Django)
def json_response(something):
from django.utils import simplejson
return HttpResponse(simplejson.dumps(something),
content_type='application/json; charset=UTF-8')
This function takes anything which can be dumped into JSON and returns an HTTP response of it, with the right Content-type header.
jonniespratley/JSON To SQL Generator (JQUERY) ( PHP)
<?php
/**
* I am a JSON Query builder
*
* Here is a inline example of how to create your json when sending it.
*
* <code>
* $flexJSON = '[{"table":"TABLENAME","objectName":"FIELDNAME","objectValue":"FIELDVALUE","database":"DATABASE","tableKey":"PRI KEY","objectKey":"RECORD KEY"}]';
* $j = new JSONQuery( 'host', 'user', 'pass' );
* $j->dump( $j->buildQuery( $flexJSON, 'INSERT' ), 'INSERT JSON ' );
* $j->dump( $j->buildQuery( $flexJSON, 'UPDATE' ), 'UPDATE JSON' );
* $j->dump( $j->buildQuery( $flexJSON, 'DELETE' ), 'DELETE JSON' );
* $j->setJSONString( $flexJSON );
* $j->dump( $j->getJSONString(), 'JSON STRING' );
* </code>
*
* @author Jonnie Spratley
* @copyright 2009 http://jonniespratley.com
* @version 0.7
*
*/
class JSONQuery
{
private $mysqli;
private $jsonArray;
private $jsonString;
private $jsonQuery;
/**
* Database Link
*
* @param [database] $link
*/
public function __construct( $link )
{
$this->mysqli = $link;
}
/**
* I build a INSERT/UPDATE/DELETE from a json string.
* I can either auto update the database, or return the sql.
* <code>
* [{"table":"TABLENAME","objectName":"FIELDNAME","objectValue":"FIELDVALUE","database":"DATABASE","tableKey":"PRI KEY","objectKey":"RECORD KEY"}]
* </code>
*
* @param [json] $json JSON string like [{"table":"TABLENAME","objectName":"FIELDNAME","objectValue":"FIELDVALUE","database":"DATABASE","tableKey":"PRI KEY","objectKey":"RECORD KEY"}]
* @param [string] $type INSERT/UPDATE/DELETE
* @param [boolean] $autoInsert = false Auto update database
* @return unknown
*/
public function buildQuery( $json, $type, $autoInsert = false )
{
$jsonString = str_replace ( "\\", "", $json );
$jsonArray = json_decode ( $jsonString, true );
$query = '';
$queryArray = array ();
switch ( $type )
{
case 'INSERT' :
//do insert statement
foreach ( $jsonArray as $record )
{
$query = sprintf ( 'INSERT INTO %s.%s SET %s = "%s"', $record [ 'database' ], $record [ 'table' ], $record [ 'objectName' ], $record [ 'objectValue' ] );
//echo $query;
$queryArray [] = $query;
}
break;
case 'UPDATE' :
foreach ( $jsonArray as $record )
{
$query = sprintf ( 'UPDATE %s.%s SET %s = "%s" WHERE %s = "%s"', $record [ 'database' ], $record [ 'table' ], $record [ 'objectName' ], $record [ 'objectValue' ], $record [ 'tableKey' ], $record [ 'objectKey' ] );
//echo $query;
$queryArray [] = $query;
}
break;
case 'DELETE' :
foreach ( $jsonArray as $record )
{
$query = sprintf ( 'DELETE FROM %s.%s WHERE %s = "%s"', $record [ 'database' ], $record [ 'table' ], $record [ 'tableKey' ], $record [ 'objectKey' ] );
//echo $query;
$queryArray [] = $query;
}
break;
default :
echo 'Specify a type of statement';
exit ();
break;
}
if ( $autoInsert )
{
return $this->_batchQuery ( $queryArray );
}
return $queryArray;
}
/**
* I take an array of sql statements and execute them in order to the database.
*
* @param [array] $sql array of sql statements
*/
private function _batchQuery( $sql )
{
foreach ( $sql as $q )
{
$result = $this->mysqli->query ( $q );
if ( $result )
{
printf ( "%d row affected.", $this->mysqli->affected_rows );
}
}
}
private function _filterTable( $s )
{
$tablename = stripos ( $s, 'table' );
return $tablename;
}
/**
* Escape SQL
*
* @param [string] $str
* @return escaped string
*/
private function _sqlQuote( $str )
{
if ( ! isset ( $str ) )
return ( "NULL" );
$func = function_exists ( "mysqli_escape_string" ) ? "mysqli_escape_string" : "addslashes";
return ( "'" . $func ( $str ) . "'" );
}
/**
* @return associative array
*/
public function getJSONArray()
{
return $this->jsonArray;
}
/**
* @return json string
*/
public function getJSONString()
{
return $this->jsonString;
}
/**
* @return sql query
*/
public function getQuery()
{
return $this->query;
}
/**
* @param associative $jsonArray
*/
public function setJSONArray( $json )
{
$jsonArray = json_decode ( $json, true );
$this->jsonArray = $jsonArray;
}
/**
* @param json $jsonString
*/
public function setJSONString( $jsonString )
{
$this->jsonString = $jsonString;
}
/**
* @param string $query
*/
public function setJSONQuery( $query )
{
$this->jsonQuery = $query;
}
/**
* I dump vars
*
* @param [var] $var
* @param [string] $name
*/
public function dump( $var, $name = 'Variable Dump' )
{
echo "<b>$name</b><br/>";
echo '<pre>';
print_r ( $var );
echo '</pre>';
}
}
?>
jonniespratley/Full REST JSON MySQL Management Class ( PHP)
<?php
require_once 'FileSystemService.php';
require_once 'JSONQuery.php';
require_once 'MySQLDump.php';
/**
* I am a REST MySQL Database Manager Service the structure for the url is as follows:
*
* @eample
* <code>
* url string:
*
* http://localhost/service.php?
* h=HOST
* &u=USERNAME
* &p=PASSWORD
* &m=MODE
* &d=DATABASE
* &t=TABLE
* &q=QUERY
* </code>
*
* <code>
* result:
*
* [
* {
* "Database":"information_schema"
* },
* {
* "Database":"mysql"
* },
* {
* "Database":"test"
* }
* ]
* </code>
*
* TABLE OF CONTENTS
*
* 1. MYSQL SHOW METHODS
* 2. PRIVATE DATABASE/TABLE METHODS
* 3. QUERY BUILDER/CREATE/UPDATE/DELETE METHODS
* 4. ANALYZE/CHECK/OPTIMIZE/REPAIR METHODS
* 5. BACKUP/IMPORT/EXPORT METHODS
* 6. SERVER VARIABLES
* 7. DATA METHODS
* 8. UTILITY METHODS
* 9. CLASS TESTING
*
*
* @name MySQLService
* @author Jonnie Spratley
* @version 1.0
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class MySQLService
{
/**
* I am the database link
*
* @var private
*/
private $mysqli;
private $query_pieces = array ();
private $fileSvc;
private $jquery;
/**
* I hold alot of access to monitor and manager mysql tables
*
* @param [string] $host Host name
* @param [string] $username User name
* @param [string] $password User password
*
* @return MySQLService
*/
public function MySQLService( $host, $username, $password )
{
//temporary for the bs warning signs on live
// Report simple running errors
error_reporting ( E_ERROR | E_USER_ERROR | E_PARSE );
//$this->mysqli = new mysqli ( $host, $username, $password );
/* create a connection object which is not connected */
$this->mysqli = mysqli_init ();
/* set connection options */
$this->mysqli->options ( MYSQLI_CLIENT_COMPRESS );
$this->mysqli->options ( MYSQLI_OPT_CONNECT_TIMEOUT, 5 );
/* connect to server */
$this->mysqli->real_connect ( $host, $username, $password );
/* check connection */
if ( mysqli_connect_errno () )
{
trigger_error ( 'Database connection failure: Username/Password was incorrect.', E_USER_ERROR );
exit ();
}
$this->fileSvc = new FileSystemService ( );
$this->jquery = new JSONQuery ( $this->mysqli );
}
/* ********************************************************************
* ********************************************************************
*
* 1. MYSQL SHOW METHODS
*
* Below is all the methods for getting tables, columns, databases,
* indexs, statusus from the database.
*
* SHOW DATABASES;
* SHOW TABLES FROM test;
* SHOW TABLE STATUS LIKE 'users';
* SHOW INDEX FROM 'contacts';
* SHOW INDEX FROM contacts;
* SHOW COLUMNS FROM contacts;
* SHOW STATUS FROM test;
* SHOW TABLE STATUS FROM test;
*
* ********************************************************************
* *********************************************************************/
/**
* I show all of the columns for a specified table.
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [json] Json of all the columns
*/
public function getTableColumns( $whatDatabase, $whatTable )
{
$sql = "SHOW COLUMNS FROM $whatDatabase.$whatTable";
return $this->_queryToJSON ( $sql );
}
/**
* I get all tables in database
*
* @param [string] $database the database
* @return [json]
*/
public function showTableStatus( $whatDatabase )
{
$sql = mysqli_query ( $this->mysqli, "SHOW TABLE STATUS FROM $whatDatabase" );
$tables = array ();
while ( $row = mysqli_fetch_assoc ( $sql ) )
{
$tables [] = $row;
}
return json_encode ( $tables );
}
/**
* I get the primary key for the table.
*
* @param [string] $database the database
* @param [string] $table the table
* @return [json]
*/
public function getTableIndex( $whatDatabase, $whatTable )
{
$sql = "SHOW INDEX FROM $whatDatabase.$whatTable";
return $this->_queryToARRAY ( $sql );
}
/**
* I get all databases, tables, columns, and fields in the database.
* Formatted specially for Flex's Tree control.
*
* @return [json]
*/
public function getDatabasesAndTables()
{
//Database query
$databaseSQL = mysqli_query ( $this->mysqli, "SHOW DATABASES" );
//New database array
$databases = array ();
//Loop the query
while ( $database = mysqli_fetch_assoc ( $databaseSQL ) )
{
//Create a new array of tables for each database
$tables = array ();
foreach ( $database as $key => $value )
{
//Set the table array to get the tbles from the database
$tables = $this->_getTables ( $value );
}
//Add the tables to the database array
$databases [] = array (
"aDatabase" => $value, "aType" => "database", "aData" => $key, "aIcon" => "databaseIcon", "aTables" => $tables
);
}
sort ( $databases );
//Encode in json
return json_encode ( $databases );
}
/**
* I get all the databases
*
* @return [json]
*/
public function getDatabases()
{
//Database query
$databaseSQL = mysqli_query ( $this->mysqli, "SHOW DATABASES" );
//New database array
$databases = array ();
//Loop the query
while ( $database = mysqli_fetch_assoc ( $databaseSQL ) )
{
//Create a new array of tables for each database
$tables = array ();
$status = array ();
$size = array ();
foreach ( $database as $key => $value )
{
//Set the table array to get the tbles from the database
$tables = $this->_getTables ( $value );
$status = $this->_getTableStatus ( $value );
$size = $this->_getDatabaseSize ( $value );
}
//Add the tables to the database array
$databases [] = array (
"aDatabase" => $value, "aType" => "database", "aData" => $key, "aIcon" => "databaseIcon", "aTables" => $tables, "aStatus" => $status, "aSize" => $size
);
}
sort ( $databases );
//Encode in json
return json_encode ( $databases );
//return $databases;
}
/**
* I get all tables in the database
*
* @param [string] $whatDatabase the name of the database
* @return [json]
*/
public function getTables( $whatDatabase )
{
//table query
$tableSQL = mysqli_query ( $this->mysqli, "SHOW TABLES FROM $whatDatabase" );
//create a new array of tables
$tables = array ();
//loop all the results
while ( $table = mysqli_fetch_assoc ( $tableSQL ) )
{
$fields = array ();
$indexes = array ();
$statuss = array ();
//for each table in the result make an array
foreach ( $table as $key => $value )
{
//now descibe each table
$fields = $this->_describeTable ( $whatDatabase, $value );
//now get the indexes
$indexes = $this->_getTableIndexes ( $whatDatabase, $value );
//now get the status for that table
$statuss = $this->_getSingleTableStatus ( $whatDatabase, $value );
}
//build a tree
$tables [] = array (
"tableName" => $value, "aFields" => $fields, "aIndexes" => $indexes, 'aStatus' => $statuss
);
}
return json_encode ( $tables );
}
/**
* I describe a table
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [json]
*/
public function describeTable( $whatDatabase, $whatTable )
{
$sql = mysqli_query ( $this->mysqli, "DESCRIBE $whatDatabase.$whatTable" );
$tables = array ();
while ( $row = mysqli_fetch_assoc ( $sql ) )
{
$tables [] = array (
'Field' => $row [ 'Field' ], 'Type' => $row [ 'Type' ], 'Null' => $row [ 'Null' ], 'Default' => $row [ 'Default' ], 'Extra' => $row [ 'Extra' ], 'Key' => $row [ 'Key' ]
);
}
sort ( $tables );
return json_encode ( $tables );
}
/**
* I get user information
*
* @param [string] $username the users name
* @return [json]
*/
public function getUserInfo( $username )
{
$sql = "SELECT * FROM mysql.user_info WHERE User = '$username'";
$result = mysqli_query ( $this->mysqli, $sql );
//return $this->_queryToJSON($sql);
$array = array ();
while ( $row = mysqli_fetch_assoc ( $result ) )
{
$array [] = array (
"User" => $row [ 'User' ],
/*"Fullname" => $row[ 'Fullname' ],*/
"Description" => $row [ 'Description' ], "Icon" => base64_encode ( $row [ 'Icon' ] ), "Email" => $row [ 'Email' ], "Info" => $row [ 'Contact_information' ]
);
}
return json_encode ( $array );
}
/**
* I get all open tables for a database
*
* @param [string] $whatDatabase the database name
* @return [json]
*/
public function getOpenTables( $whatDatabase )
{
$sql = "SHOW OPEN TABLES FROM $whatDatabase";
return $this->_queryToJSON ( $sql );
}
/**
* I get a count of rows from the table
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [json]
*/
public function getTableRows( $whatDatabase, $whatTable )
{
return $this->_queryToJSON ( "SELECT COUNT(*) FROM $whatDatabase.$whatTable" );
}
/* ********************************************************************
* ********************************************************************
*
* 2. PRIVATE DATABASE/TABLE METHODS
*
* Below is all the private methods that build up the database
* and table tree with information about each item.
*
* Example:
*
* DatabaseName/
* TableName/
* FieldName/
*
* ********************************************************************
* ********************************************************************
/**
* I get all tables for a database
*
* @param [string] $whatDatabase the database
* @return [array]
*/
public function _getTables( $whatDatabase )
{
//table query
$tableSQL = mysqli_query ( $this->mysqli, "SHOW TABLES FROM $whatDatabase" );
//create a new array of tables
$tables = array ();
//loop all the results
while ( $table = mysqli_fetch_assoc ( $tableSQL ) )
{
$fields = array ();
$statuss = array ();
$indexes = array ();
//for each table in the result make an array
foreach ( $table as $t_key => $t_value )
{
//get the tables fields for each table
$fields = $this->_describeTable ( $whatDatabase, $t_value );
//now get the primary key for each table
$primaryKey = $this->_getTableKey ( $whatDatabase, $t_value );
//now get the status for that table
$statuss = $this->_getSingleTableStatus ( $whatDatabase, $t_value );
//now get the indexes for each table
$indexes = $this->_getTableIndexes ( $whatDatabase, $t_value );
}
$tables [] = array (
"aTable" => $t_value, "aKey" => $primaryKey, "aType" => "table", "aIcon" => "tableIcon", "aData" => $t_key, "aFields" => $fields, "aStatus" => $statuss, "aIndexes" => $indexes
);
}
//sort ( $tables );
return $tables;
}
/**
* I describe a table for the getDatabasesAndTables() method
*
* @param [string] $database the database
* @param [string] $table the table
* @return [array]
*/
public function _describeTable( $whatDatabase, $whatTable )
{
return $this->_queryToARRAY ( "SHOW FIELDS FROM $whatDatabase.$whatTable" );
}
public function _getTableIndexes( $whatDatabase, $whatTable )
{
return $this->_queryToARRAY ( "SHOW INDEX FROM $whatDatabase.$whatTable" );
}
/**
* I get the table status for a table only when called from getDatabases()
*
* @param [string] $whatDatabase
* @return [array]
*/
public function _getTableStatus( $whatDatabase )
{
return $this->_queryToARRAY ( "SHOW TABLE STATUS FROM $whatDatabase" );
}
public function _getSingleTableStatus( $whatDatabase, $whatTable )
{
return $this->_queryToARRAY ( "SHOW TABLE STATUS FROM $whatDatabase LIKE '$whatTable'" );
}
/**
* I get tables and fields
*
* @param [string] $whatDatabase the database
* @return [array]
*/
private function _getTableAndFields( $whatDatabase )
{
$tableInfoSql = mysqli_query ( $this->mysqli, "SHOW TABLE STATUS FROM $whatDatabase" );
$tables = array (); //array of all table info
$fields = array (); //array of all field info
//get the table name from the result
while ( $tableInfo = mysqli_fetch_row ( $tableInfoSql ) )
{
$tables [] = $tableInfo [ 0 ];
//loop threw every table inside of the tables array
foreach ( $tables as $table )
{
//for each table, get the fields info for that table
$fields = $this->_showFieldInfo ( $whatDatabase, $table );
}
$tableInfoAndFields [] = array (
'aTable' => $table, 'aType' => 'table', 'aFields' => $fields
);
}
$databaseInfoAndTables [] = array (
'aDatabase' => $whatDatabase, 'aType' => 'database', 'aTables' => $tableInfoAndFields
);
//return $fields;
//return $databaseInfoAndTables;
return json_encode ( $databaseInfoAndTables );
}
private function _getDatabasesTablesAndFields()
{
$databaseInfoSql = mysqli_query ( $this->mysqli, "SHOW DATABASES" );
$databases = array (); //array of all databases info
$tables = array (); //array of all table info
$fields = array (); //array of all field info
//get the table name from the result
while ( $databaseInfo = mysqli_fetch_row ( $databaseInfoSql ) )
{
$databases [] = $databaseInfo [ 0 ];
//loop threw every table inside of the tables array
foreach ( $databases as $database )
{
$tables = $this->_showTableInfo ( $database );
//for each table, get the fields info for that table
foreach ( $tables as $table )
{
$fields = $this->_showFieldInfo ( $database, $table );
}
}
$tableInfoAndFields [] = array (
'aTable' => $table, 'aFields' => $fields
);
}
//return $fields;
//return $tables;
return json_encode ( $tableInfoAndFields );
}
/**
* I get information about the table.
*
* @param [string] $whatDatabase the database
* @return [array]
*/
private function _showTableInfo( $whatDatabase )
{
$tableInfoSql = "SHOW TABLE STATUS FROM $whatDatabase";
$result = mysqli_query ( $this->mysqli, $tableInfoSql );
$tableInfo = array ();
while ( $tables = mysqli_fetch_assoc ( $result ) )
{
$tableInfo [] = $tables;
}
return $tableInfo;
}
/**
* I get the fields for a table
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [array]
*/
private function _showFieldInfo( $whatDatabase, $whatTable )
{
$fieldInfoSql = "SHOW FIELDS FROM $whatDatabase.$whatTable";
$fieldInfo = array ();
$result = $this->mysqli->query ( $fieldInfoSql );
while ( $fields = mysqli_fetch_assoc ( $result ) )
{
$fieldInfo [] = $fields;
}
return $fieldInfo;
}
/**
* I get the key for the table.
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [string]
*/
public function _getTableKey( $whatDatabase, $whatTable )
{
$indexInfoSql = "SHOW INDEX FROM $whatDatabase.$whatTable";
$index = array ();
$result = $this->mysqli->query ( $indexInfoSql );
while ( $indexes = mysqli_fetch_assoc ( $result ) )
{
if ( $indexes [ 'Key_name' ] == 'PRIMARY' )
{
$index = $indexes [ 'Column_name' ];
}
}
return $index;
}
/**
* I get the size of all databases in the database
*
* @return [json]
*/
public function getDatabaseSpace()
{
$sql = 'SELECT table_schema "Database",
sum( data_length + index_length ) / 1024 / 1024 "TotalSize",
sum( data_length ) / 1024 / 1024 "DataSize",
sum( index_length ) / 1024 / 1024 "IndexSize",
sum( data_free ) / 1024 / 1024 "FreeSize"
FROM information_schema.TABLES
GROUP BY table_schema';
return $this->_queryToJSON ( $sql );
}
/**
* I get the database size for all tables
*
* @param [string] $whatDatabase the database name
*/
public function _getDatabaseSize( $whatDatabase )
{
$statusSQL = mysqli_query ( $this->mysqli, "SHOW TABLE STATUS FROM $whatDatabase" );
$sizeArray = array ();
$totalSize = 0;
$dataSize = 0;
$indexSize = 0;
//loop all the results
while ( $size = mysqli_fetch_assoc ( $statusSQL ) )
{
$dataSize += $size [ 'Data_length' ];
$indexSize += $size [ 'Index_length' ];
}
$totalSize = $dataSize + $indexSize;
$sizeArray [] = array (
'totalSize' => $totalSize, 'dataSize' => $dataSize, 'indexSize' => $indexSize
);
return $sizeArray;
}
/* ********************************************************************
* ********************************************************************
*
* 3. QUERY BUILDER/CREATE/UPDATE/DELETE METHODS
*
* Below is all the methods for building insert queries, creating
* databases, creating tables, creating users, removing data,
* inserting data, and updating data.
* Also there is methods for altering databases, and tables.
*
* ********************************************************************
* *********************************************************************/
/**
* I create a database
*
* @param [string] $whatDatabase the name of the database
* @return [string] the result outcome
*/
public function createDatabase( $whatDatabase )
{
//CREATE DATABASE `tutorial_library`
//DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
$sql = "CREATE SCHEMA IF NOT EXISTS $whatDatabase
CHARACTER SET utf8";
$result = mysqli_query ( $this->mysqli, $sql );
if ( ! $result )
{
return 'There was an error creating the database.';
}
return 'Database created!';
}
/**
* I create a table
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the name of the new table
* @return [string] the result
*/
public function createTable( $whatDatabase, $whatTable )
{
/**
* CREATE TABLE `books` ( id int ) DEFAULT CHARACTER SET latin1;
*
*/
$sql = "CREATE TABLE $whatDatabase.$whatTable ( id int ) DEFAULT CHARACTER SET latin1";
$result = mysqli_query ( $this->mysqli, $sql );
if ( ! $result )
{
return false;
}
return true;
}
/**
* I alter a database table
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the name of the new table
* @param [string] $whatQuery a query array of what to change
* @return [string] the result
*/
public function alterTable( $whatDatabase, $whatTable, $whatQuery )
{
/**
* ALTER TABLE `cars` ADD `engine` varchar(225) DEFAULT NULL ;
*/
$sql = "ALTER TABLE $whatDatabase.$whatTable ADD $whatQuery";
$result = mysqli_query ( $this->mysqli, $sql );
if ( ! $result )
{
return false;
}
return true;
}
/**
* I remove a table
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [string] the result
*/
public function removeTable( $whatDatabase, $whatTable )
{
/**
* DROP TABLE `library`;
*/
$sql = "DROP TABLE $whatDatabase.$whatTable";
$result = mysqli_query ( $this->mysqli, $sql );
if ( ! $result )
{
return false;
}
return true;
}
/**
* I rename a table
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @param [string] $newName the new name
* @return [string] the result
*/
public function renameTable( $whatDatabase, $whatTable, $newName )
{
/**
* RENAME TABLE test.books TO test.the_books
*/
$sql = "RENAME TABLE $whatDatabase.$whatTable TO $newName";
$result = mysqli_query ( $this->mysqli, $sql );
if ( ! $result )
{
return false;
}
return true;
}
/**
* I insert data into the database
*
*
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @param [array] $whatQuery Array of Key/Value pairs for inserting in the database
* @return [string] the result
*/
public function insertRecord( $jsonQuery )
{
return $this->jquery->buildQuery ( $jsonQuery, 'INSERT', true );
//$queryArray[] = $this->jquery->buildQuery ( $jsonQuery, 'INSERT', false );
}
/**
* I update data from the database
* UPDATE db.tbl SET name='value'
*
*
* @param [string] $jsonQuery Array of Key/Value pairs for updating the database
* @return [string] the result
*/
public function updateRecord( $jsonQuery )
{
return $this->jquery->buildQuery ( $jsonQuery, 'UPDATE', true );
}
/**
* I remove data from the database
*
* @param [array] $jsonQuery the value to which remove by
* @return [string] the result
*/
public function removeRecord( $jsonQuery )
{
return $this->jquery->buildQuery ( $jsonQuery, 'DELETE', true );
}
/* ********************************************************************
* ********************************************************************
*
* 4. ANALYZE/CHECK/OPTIMIZE/REPAIR METHODS
*
* Below is all the methods analyzing, checking, optimizing and repairing
* database tables.
*
* ********************************************************************
* *********************************************************************/
/**
* I analyze a table
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [json]
*/
public function analyzeTable( $whatDatabase, $whatTable )
{
$sql = "ANALYZE TABLE $whatDatabase.$whatTable";
return $this->_queryToJSON ( $sql );
}
/**
* I check a table
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [json]
*/
public function checkTable( $whatDatabase, $whatTable )
{
$sql = "CHECK TABLE $whatDatabase.$whatTable";
return $this->_queryToJSON ( $sql );
}
/**
* I optimize a table
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [json]
*/
public function optimizeTable( $whatDatabase, $whatTable )
{
$sql = "OPTIMIZE TABLE $whatDatabase.$whatTable";
return $this->_queryToJSON ( $sql );
}
/**
* I repair a table
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [json]
*/
public function repairTable( $whatDatabase, $whatTable )
{
$sql = "REPAIR TABLE $whatDatabase.$whatTable";
return $this->_queryToJSON ( $sql );
}
/**
* I analyze a query are return the statistics
*
* @param [string] $sql query string
* @return [json] json results
*/
public function analyzeQuery( $sql )
{
$setProfileSQL = $this->mysqli->query ( 'SET profiling = 1' );
$analyzeSQL = $this->mysqli->query ( $sql );
$showProfileSQL = $this->mysqli->query ( 'SHOW PROFILE' );
$showProfilesSQL = $this->mysqli->query ( 'SHOW PROFILES' );
$resultArray = array ();
$profileArray = array ();
$profilesArray = array ();
/* fetch associative array */
while ( $row1 = $analyzeSQL->fetch_assoc () )
{
$resultArray [] = $row1;
}
/* fetch associative array */
while ( $row2 = $showProfileSQL->fetch_assoc () )
{
$profileArray [] = $row2;
}
/* fetch associative array */
while ( $row3 = $showProfilesSQL->fetch_assoc () )
{
$profilesArray [] = $row3;
}
$analyzedQuery [] = array (
'aProfile' => $profileArray, 'aProfiles' => $profilesArray, 'aResults' => $resultArray
);
//$analyzedQuery[] = array( $profileArray, $profilesArray, $resultArray );
/* Free all of the results */
$analyzeSQL->close ();
$showProfileSQL->close ();
$showProfilesSQL->close ();
/* close connection */
//$this->mysqli->close ();
return json_encode ( $analyzedQuery );
//return $analyzedQuery;
}
/* ********************************************************************
* ********************************************************************
*
* 5. BACKUP/IMPORT/EXPORT METHODS
*
* Below is all the methods for backing up the database, importing data,
* exporting data.
*
* ********************************************************************
* *********************************************************************/
/**
* I execute a query and return XML
*
* @param [string] $query the query
* @return [xml]
*/
public function queryResultToXML( $query )
{
$xmlResult = '<?xml version="1.0"?>';
/* Set the content type for the browser */
//table query
$sql = mysqli_query ( $this->mysqli, "$query" );
$xmlResult .= "<results>";
//loop all the results
while ( $rows = mysqli_fetch_assoc ( $sql ) )
{
$xmlResult .= "<result>";
//for each table in the result make an array
foreach ( $rows as $key => $value )
{
$xmlResult .= "<$key>" . htmlspecialchars ( $value ) . "</$key>";
}
$xmlResult .= "</result>";
}
$xmlResult .= "</results>";
return $xmlResult;
}
/**
* I execute a query and return JSON
*
* @param [string] $query the query
* @return [json]
*/
private function queryResultToJSON( $query )
{
return $this->_queryToJSON ( $query );
}
/**
* I execute a query and return json
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [json]
*/
public function exportToJSON( $whatDatabase, $whatTable )
{
$sql = "SELECRT * FROM $whatDatabase.$whatTable";
return $this->_queryToJSON ( $sql );
}
/**
* I export data from the database
*
* @param [string] $whatDatabase
* @param [string] $whatTable
* @param [string all, db_structure, db_data, tbl_structure, tbl_data ] $whatMode
* @return [string] the filename of the file.
*/
public function createBackup( $whatDatabase, $whatTable, $whatMode )
{
$result = '';
$filename = $whatDatabase . '-' . $whatTable . '-' . $whatMode . '-' . $this->makeTimestamp () . '.sql';
//$dbDir = mkdir( "backups/".$whatDatabase );
//Set the database, filename, and we don't want to use compression.
$dumper = new MySQLDump ( $whatDatabase, "../backups/" . $filename, false );
$mode = $whatMode;
//Switch based on what mode is specified
switch ( $mode )
{
case 'all' :
$dumper->doDump ();
$result = 'Dumping all data';
return true;
break;
case 'db_structure' :
$dumper->getDatabaseStructure ();
$result = 'Database structure backed up successfully.';
$resultArray [] = array (
'mode' => $mode, 'result' => $result, 'filename' => $filename
);
return $resultArray;
break;
case 'db_data' :
$dumper->getDatabaseData ( false );
$result = 'Database data backed up successfully.';
$resultArray [] = array (
'mode' => $mode, 'result' => $result, 'filename' => $filename
);
return $resultArray;
break;
case 'tbl_structure' :
$dumper->getTableStructure ( $whatTable );
$result = 'Table structure backed up successfully.';
$resultArray [] = array (
'mode' => $mode, 'result' => $result, 'filename' => $filename
);
return $resultArray;
break;
case 'tbl_data' :
$dumper->getTableData ( $whatTable, false );
$result = 'Table data backed up successfully.';
$resultArray [] = array (
'mode' => $mode, 'result' => $result, 'filename' => $filename
);
return $resultArray;
break;
default :
$result = 'Please specify a mode.';
$resultArray [] = array (
'mode' => $mode, 'result' => $result, 'filename' => $filename
);
return $resultArray;
break;
}
return $result;
}
/**
* I get a list of all the backups in the backup folder
*
* @return [json]
*/
public function getDatabaseBackups()
{
return $this->fileSvc->browseDirectory ( './backups', 'json' );
}
public function removeBackup( $whatDatabase, $whatFile )
{
return $this->fileSvc->removeFile ( './backups', $whatFile );
}
/* ********************************************************************
* ********************************************************************
*
* 6. SERVER VARIABLES
*
* Below is all the methods that build up information about the server
* and system.
*
*
* ********************************************************************
* ********************************************************************/
/**
* I kill a thread that is connected or running
*
* @param [int] $whatThread the id of the thread
* @return [boolean] true or false
*/
public function killProcess( $whatThread )
{
$sql = "KILL $whatThread";
$message = '';
if ( mysqli_query ( $this->mysqli, $sql ) )
{
$message = array (
'message' => true, 'thread' => $whatThread
);
}
else
{
$message = array (
'message' => false, 'thread' => $whatThread
);
}
return json_encode ( $message );
}
/**
* I show all mysql system variables
*
* @return [json]
*/
public function showSystemVariables()
{
return $this->_queryStatusToJSON ( "SHOW GLOBAL VARIABLES" );
}
/**
* I show all system privileges
*
* @return [json]
*/
public function showSystemPrivileges()
{
return $this->_queryToJSON ( "SHOW PRIVILEGES" );
}
/**
* I show the system status
*
* @return [json]
*/
public function showSystemStatus()
{
return $this->_queryStatusToJSON ( "SHOW GLOBAL STATUS" );
}
/**
* I show system processes
*
* @return [json]
*/
public function showSystemProcess()
{
return $this->_queryStatusToJSON ( "SHOW FULL PROCESSLIST" );
}
/**
* I show all of the systems users
*
* @return [json]
*/
public function showSystemUsers()
{
return $this->_queryToJSON ( "SELECT * FROM mysql.user" );
}
/**
* I get server info
*
* @return [json]
*/
public function _getServerInfo()
{
$serverArray = array ();
$aPath = $_SERVER [ 'DOCUMENT_ROOT' ];
$serverArray [] = array (
'aDiskFreeSpace' => disk_free_space ( $aPath ),
'aDiskTotalSize' => disk_total_space ( $aPath ),
'aServerSoftware' => $_SERVER [ 'SERVER_SOFTWARE' ],
'aServerName' => $_SERVER [ 'SERVER_NAME' ],
'aPHPVersion' => PHP_VERSION,
'aPHPOs' => PHP_OS,
'aPHPExtensionDir' => PHP_EXTENSION_DIR,
'aMySQLClientV' => mysqli_get_client_info ( $this->mysqli ),
'aMySQLServerV' => mysqli_get_server_version ( $this->mysqli ),
'aMySQLHost' => mysqli_get_host_info ( $this->mysqli ),
'aMySQLProtocol' => mysqli_get_proto_info ( $this->mysqli ),
'aUptime' => $this->_getUptime ()
);
return json_encode ( $serverArray );
}
/**
* I get all of the threads
*
* @return [json]
*/
public function _getThreads()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Threads%'" );
}
/**
* I get the temp size
*
* @return [json]
*/
public function _getTemp()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%tmp%'" );
}
/**
* I get open tables
*
* @return [json]
*/
public function _getOpen()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Open%'" );
}
/**
* I get the handlers variables
*
* @return [json]
*/
public function _getHandlers()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Handler%'" );
}
/**
* I get the server uptime
*
* @return [array]
*/
public function _getUptime()
{
$result = mysqli_query ( $this->mysqli, "SHOW STATUS LIKE '%uptime%'" );
$row = mysqli_fetch_row ( $result );
$array = $this->_formatUptime ( $row [ 1 ] );
return $array;
}
private function _getUnixTimestamp( $unix )
{
return $this->_queryToARRAY ( "SELECT UNIX_TIMESTAMP() - $unix" );
}
/**
* I get the recent queries
*
* @return [json]
*/
public function _getQuestions()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE 'Questions%'" );
}
/**
* I get the query cache
*
* @return [json]
*/
public function _getQcache()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Qcache%'" );
}
/**
* I get InnoDB
*
* @return [json]
*/
public function _getInnoDb()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Innodb%'" );
}
/**
* I get the key cache
*
* @return [json]
*/
public function _getKeys()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Key%'" );
}
/**
* I get the performance of mysql.
*
* @return [json]
*/
public function _getPerformance()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Slow%'" );
}
/**
* I get all the sort
*
* @return [json]
*/
public function _getSort()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Sort%'" );
}
/**
* I get the connections
*
* @return [json]
*/
public function _getConnections()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Connections%'" );
}
/**
* I get the aborted clients and connections
*
* @return unknown
*/
public function _getClients()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Aborted%'" );
}
/**
* I get mysql bytes
*
* @return [json]
*/
public function _getBytes()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Bytes%'" );
}
/**
* I get all the slave hosts
*
* @return [json]
*/
public function _getReplication()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Slave%'" );
}
/**
* I get the commands
*
* @return [json]
*/
public function _getCommands()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Com%'" );
}
/**
* I show all of the SHOW commands
*
* @return [json]
*/
public function _getShowCommands()
{
return $this->_queryStatusToJSON ( "SHOW STATUS LIKE '%Com_show%'" );
}
/**
* I get the stats of the mysql connection
*
* @return [array]
*/
public function _getStat()
{
$stats = $this->mysqli->stat ();
$newStats = explode ( ' ', $stats );
return $newStats;
}
/* ********************************************************************
* ********************************************************************
*
* 7. POLLING METHODS
*
* Below is all the methods for executing a query on the database,
* and getting all records from the database.
*
* ********************************************************************
* ********************************************************************/
/**
* I get the health of a mysql server
*
* @return [array] of results
*/
public function _getHealth()
{
$query = $this->mysqli->query ( "SHOW GLOBAL STATUS LIKE '%Key_%'" );
$array = array ();
while ( $row = mysqli_fetch_assoc ( $query ) )
{
$array [ $row [ 'Variable_name' ] ] = array (
$row [ 'Variable_name' ] => $row [ 'Value' ]
);
}
return $array;
}
/**
* I am a polling method for checking the current select statements.
* @example Results
* <code>
* [
* {
* "Threads_cached":"0",
* "aTimestamp":"2009-02-20T21:52:34-08:00"
* },
* {
* "Threads_connected":"1",
* "aTimestamp":"2009-02-20T21:52:34-08:00"
* },
* {
* "Threads_created":"2070",
* "aTimestamp":"2009-02-20T21:52:34-08:00"
* },
* {
* "Threads_running":"1",
* "aTimestamp":"2009-02-20T21:52:34-08:00"
* }
* ]
*</code>
* @return [json] encoded results
*/
public function pollQueries()
{
$result = mysqli_query ( $this->mysqli, "SHOW GLOBAL STATUS LIKE '%Com_select%'" );
$timestamp = date ( DATE_W3C );
while ( $row = mysqli_fetch_row ( $result ) )
{
$array [] = array (
$row [ 0 ] => $row [ 1 ], 'aTimestamp' => $timestamp
);
}
return json_encode ( $array );
}
/**
* I am a polling method for checking the current bytes sent.
* @example Results
* <code>
* [
* {
* "Bytes_sent":"48438",
* "aTimestamp":"2009-02-20T21:52:34-08:00"
* }
* ]
*</code>
* @return [json] encoded results
*/
public function pollTraffic()
{
$result = mysqli_query ( $this->mysqli, "SHOW GLOBAL STATUS LIKE '%Bytes_sent%'" );
$timestamp = date ( DATE_W3C );
while ( $row = mysqli_fetch_row ( $result ) )
{
$array [] = array (
$row [ 0 ] => $row [ 1 ], 'aTimestamp' => $timestamp
);
}
return json_encode ( $array );
}
/**
* I am a polling method for checking the current connections.
* @example Results
* <code>
* [
* {
* "Com_select":"97",
* "aTimestamp":"2009-02-20T21:52:34-08:00"
* }
* ]
*</code>
*
* @return [json] encoded results
*/
public function pollConnections()
{
$result = mysqli_query ( $this->mysqli, "SHOW GLOBAL STATUS LIKE '%Threads_%'" );
$timestamp [] = array (
'aTimestamp' => date ( DATE_W3C )
);
while ( $row = mysqli_fetch_row ( $result ) )
{
$array [] = array (
$row [ 0 ] => $row [ 1 ]
);
}
//$a[] = array_merge( $timestamp, $array );
//return $a;
return json_encode ( $array );
}
/* ********************************************************************
* ********************************************************************
*
* 8. DATA METHODS
*
* Below is all the methods for executing a query on the database,
* and getting all records from the database.
*
* ********************************************************************
* ********************************************************************/
/**
* I get all the table data
*
* @param [string] $whatDatabase the database
* @param [string] $whatTable the table
* @return [json]
*/
public function getTableData( $whatDatabase, $whatTable )
{
$sql = "SELECT * FROM $whatDatabase.$whatTable";
return $this->_queryToJSON ( $sql );
}
/**
* I execute a query
*
* @param [string] $query the query to execute
* @return [json]
*/
public function executeQuery( $sql )
{
$query = mysqli_escape_string ( $this->mysqli, $sql );
return $this->_queryToJSON ( $query );
}
/* ********************************************************************
* ********************************************************************
*
* 9. RESULT HANDLERS
*
* ********************************************************************
* ********************************************************************/
/**
* I execute a query and return the results as json.
*
* @param [string] $sql the query to be executed
* @return [json] the result in json
*/
private function _queryToJSON( $sql )
{
$result = mysqli_query ( $this->mysqli, $sql );
while ( $row = mysqli_fetch_assoc ( $result ) )
{
$array [] = $row;
}
return json_encode ( $array );
}
/**
* I execute a query and return the result as an array.
*
* @param [string] $sql the query to be executed
* @return [array] the result array
*/
private function _queryToARRAY( $sql )
{
$query = mysqli_query ( $this->mysqli, $sql );
$array = array ();
while ( $row = mysqli_fetch_assoc ( $query ) )
{
$array [] = $row;
}
return $array;
}
/**
* I get the query status
*
* @param [string] $sql
* @return [json] mysql status with the ('_') striped out
*/
private function _queryStatusToJSON( $sql )
{
$result = mysqli_query ( $this->mysqli, $sql );
while ( $row = mysqli_fetch_assoc ( $result ) )
{
//replace some of the names
$row = str_replace ( 'Com_', '', $row );
//take out the _ of the rows
$row = str_replace ( '_', ' ', $row );
$array [] = $row;
}
sort ( $array );
return json_encode ( $array );
}
/* ********************************************************************
* ********************************************************************
*
* 10. UTILITY METHODS
*
* Below is all the utility methods for handling the results from a query
* and dumping variables or creating timestamps
*
*
* ********************************************************************
* ********************************************************************/
/**
* I ping mysql for a connection
*
* @return true or false
*/
public function ping()
{
$msg = '';
/* check if server is alive */
if ( $this->mysqli->ping () )
{
$msg = true;
}
else
{
$msg = false;
}
return $msg;
}
/**
* I get help from the mysql database
*
* @return [json]
*/
public function getHelp()
{
$sql = 'SELECT help_keyword.name,
help_topic.name,
help_topic.description,
help_category.name AS AVG_help_category_name,
help_category.url,
help_topic.example,
help_topic.url
FROM mysql.help_keyword
INNER JOIN mysql.help_relation
ON help_keyword.help_keyword_id = help_relation.help_keyword_id
INNER JOIN mysql.help_topic
ON help_topic.help_topic_id = help_relation.help_topic_id
INNER JOIN mysql.help_category
ON help_topic.help_category_id = help_category.help_category_id';
return $this->_queryToJSON ( $sql );
}
/**
* I format debug dumps
*
* @param [var] the variable you with to dump
*/
public function dumpIt( $var )
{
print "<pre>n";
print_r ( $var );
print "</pre>n";
}
/**
* I make a formatted timestamp.
* <code>
* 2008-12-30 22:40:00
* </code>
*
* @return [string] a timestamp
*/
private function makeTimestamp()
{
$time = time ();
return date ( 'm-d-Y-H-i', $time );
}
/**
* I format uptime from MySQL
*
* @param [int] $time the old time
* @return [string] the new time
*/
private function _formatUptime( $time = 0 )
{
$days = ( int ) floor ( $time / 86400 );
$hours = ( int ) floor ( $time / 3600 ) % 24;
$minutes = ( int ) floor ( $time / 60 ) % 60;
if ( $days == 1 )
{
$uptime = "$days day, ";
}
else if ( $days > 1 )
{
$uptime = "$days days, ";
}
if ( $hours == 1 )
{
$uptime .= "$hours hour";
}
else if ( $hours > 1 )
{
$uptime .= "$hours hours";
}
if ( $uptime && $minutes > 0 && $seconds > 0 )
{
$uptime .= ", ";
}
else if ( $uptime && $minutes > 0 & $seconds == 0 )
{
$uptime .= " and ";
}
( $minutes > 0 ) ? $uptime .= "$minutes minute" . ( ( $minutes > 1 ) ? "s" : NULL ) : NULL;
return $uptime;
}
/**
* I try and throw an error.
*
* @param [string] $msg the message of the mess
* @param [string] $type the type of error
* @return error
*/
private function _throwError( $msg, $type )
{
switch ( $type )
{
case 'user' :
throw ErrorException ();
break;
case 'error' :
return trigger_error ( $msg, E_ERROR );
break;
case 'other' :
return trigger_error ( $msg, E_USER_ERROR );
break;
}
return trigger_error ( $msg, E_USER_ERROR );
}
}
?>
I am currently still working on this, and will make changes.
saturngod/Plist to JSON ( Python)
plist = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aDict</key>
<dict>
<key>anotherString</key>
<string>&lt;hello hi there!&gt;</string>
</dict>
<key>aList</key>
<array>
<string>A</string>
<string>B</string>
<integer>12</integer>
<real>32.100000000000001</real>
<array>
<integer>1</integer>
<integer>2</integer>
<integer>3</integer>
</array>
</array>
<key>aString</key>
<string>Doodah</string>
</dict>
</plist>
"""
import json
from plistlib import readPlist
import StringIO
in_file = StringIO.StringIO(plist)
plist_dict = readPlist(in_file)
print json.dumps(plist_dict)
qrist0ph/JSON Parsing ( HTML)
<html>
<head>
<title>Titel der Webseite</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<!-- Parser from rom http://www.json.org/ -->
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript">
/*
* * Pyhton REQUEST Processing
* ------
* email = request.GET.get('name', False)
* passwort = request.GET.get('password', False)
*
* Pyhton RESPONSE
* -----
* return HttpResponse(simplejson.dumps({'sessionid':request.session.id}),0, mimetype='application/javascript')
*
*/
function parseJson(){
/*
* WITH eval() WORKS FINE
*/
var jsonString ='{ name: "Ein Name", liste: [1,"zwei",3] }';
var myObject = eval('(' + jsonString + ')');
alert("eval1 " + myObject.name + " " + myObject.liste[1]);
/*
* WITH eval() WORKS FINE
*/
var jsonString ='{ "name": "Ein Name", liste: [1,"zwei",3] }';
var myObject = eval('(' + jsonString + ')');
alert("eval2 " + myObject.name);
/*
* WITH JSON Parser WORKS FINE
*/
var jsonString ='{ "name": "Ein Name", "liste": [1,"zwei",3] }';
var myObject = JSON.parse(jsonString);
alert("JSON Parser " +myObject.name);
/*
* WITH JSON Parser DOESNT WORK
*/
var jsonString ='{ name: "Ein Name", liste: [1,"zwei",3] }';
var myObject = JSON.parse(jsonString);
alert("JSON Parser " +myObject.name);
}
/*
* Ajax XmlHttpRequest senden
*/
function sendData(){
$.getJSON('/login_ajax/', {
name: $("#username").val(),
password: "einPassword"
}, login_callback);
}
</script>
</head>
<body ">
<input type="button" value="Json Test" onclick="parseJson()">
</body>
</html>
ichnoweb/Process Tumblr JSON Feed ( PHP)
<?php
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://demo.tumblr.com/api/read/json");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($ch);
curl_close($ch);
$result = str_replace("var tumblr_api_read = ","",$result);
$result = str_replace(';','',$result);
$result = str_replace('\u00a0','&nbsp;',$result);
$jsondata = json_decode($result,true);
$posts = $jsondata['posts'];
var_dump($posts);
$blogroll = "";
foreach($posts as $post){
$blogroll .= "<p>";
$date = date('d.m.Y',$post['unix-timestamp']);
$blogroll .= $date."<br />";
switch ($post['type']) {
case 'photo':
$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a>";
break;
case 'link':
$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".$post['link-text']."</a><br />";
$blogroll .= substr(strip_tags($post['link-description']),0,100)."...";
break;
case 'regular':
$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".$post['regular-title']."</a><br />";
$blogroll .= substr(strip_tags($post['regular-body']),0,100)."...";
break;
case 'quote':
$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
$blogroll .= substr(strip_tags($post['quote-text']),0,100)."...";
break;
case 'conversation':
if(empty($post['conversation-title'])){
$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
}else{
$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".$post['conversation-title']."</a><br />";
}
$blogroll .= substr(nl2br($post['conversation-text']),0,100);
break;
case 'audio':
if(empty($post['audio-caption'])){
$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
}else{
$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".strip_tags($post['audio-caption'])."</a><br />";
}
break;
default:
$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a>";
break;
}
$blogroll .= "</p>";
}
echo $blogroll;
?>
Tumblr API: http://www.tumblr.com/docs/de/api
asimmittal/Google Docs Spreadsheet Rows to JSON ( JavaScript)
/*
* create a simple container that shows the string argument
* in a text box
*/
function dumpDataIntoUI(data){
//the white container is the app
var app = UiApp.createApplication();
app.setHeight(480);
app.setWidth(640);
//we want a nice label with the title of the script
var label = app.createLabel("All Rows to JSON");
label.setStyleAttribute("font-size","24");
label.setStyleAttribute("font-weight","bold");
label.setStyleAttribute("padding-bottom", "25px");
//next, we will add the text box and fill it with the string argument
var text = app.createTextArea();
text.setHeight(400);
text.setWidth(640);
text.setValue(data);
text.setSelectionRange(0, data.length);
//add all the widgets into the app and show it
app.add(label);
app.add(text);
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.show(app);
}
/*
* function to actually grab all the rows and put them
* into a JSON string.
*/
function rowsToJson() {
//grab the rows from the active spreadsheet
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
//the first row contains the titles or json keys
var firstRowTitles = values[0];
var allDataJSONString = "";
//this loop converts all the values starting from the second row to the last
//into one big JSON string
for(var i = 1; i < numRows; i++){
var row = values[i];
var rowData = "{\n";
for(var j = 0; j < row.length; j++){
var comma = (j == row.length - 1)?"":",";
rowData += '"' + firstRowTitles[j] + '"' + ":" + '"' + row[j] + '"' + comma + "\n";
}
var comma = (i == numRows-1)?"":","
rowData+="}" + comma + "\n";
allDataJSONString += rowData;
}
//add delimiters to that string and dump into the UI
dumpDataIntoUI("[\n" + allDataJSONString.toLowerCase() + "\n]");
};
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [
{
name : "All Rows to JSON",
functionName : "rowsToJson"
}
];
sheet.addMenu("My Scripts", entries);
};
This snippet takes all the rows from your google spreadsheet and turns it into JSON, which can be copied to the clipboard. You can run this as part of the Google Docs scripting environment.
jonniespratley/MySQL JSON Schema Tree ( PHP)
<?php
/**
* I hold mysql methods
*
* @name MySQLService
* @author Jonnie Spratley
* @version 1.0
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
class MySQLConnect
{
public $mysqli;
/**
* I hold methods to alter a mysql database
*
* @param [string] $host
* @param [string] $username
* @param [string] $password
*/
public function __construct( $host, $username, $password )
{
$link = new mysqli ( $host, $username, $password );
/* check connection */
if ( mysqli_connect_errno () )
{
trigger_error ( 'Database connection failure: Username/Password was incorrect.', E_USER_ERROR );
exit ();
}
else
{
$this->setMysqli ( $link );
}
}
/**
* I execute a query
*
* @param [string] $sql
* @return [array]
*/
public function execute( $sql )
{
return $this->queryToARRAY ( $sql );
}
/**
* I get the databases
*
* @return [array]
*/
public function getDatabases()
{
return $this->queryToARRAY ( "SHOW DATABASES" );
}
/**
* I execute a raw query
*
* @param [string] $query
* @return [link]
*/
public function realQuery( $query )
{
return $this->mysqli->query ( $query );
}
/**
* I start the tree
*
* @return [array]
*/
public function tree_getTree()
{
$mysqlVersion = mysqli_get_client_info ( $this->mysqli );
$host = $_SERVER [ 'HTTP_HOST' ] . " (MySQL v. $mysqlVersion )";
$hostArray = array (
'label' => $host, 'type' => 'server', 'children' => $this->tree_getSchemas ()
);
$treeArray [] = array (
'label' => 'SQL Databases', 'type' => 'servers', 'children' => $hostArray
);
return json_encode ( $treeArray );
}
/**
* I build the tree
*
* @return [array]
*/
private function tree_getSchemas()
{
//Database query
$databaseSQL = $this->realQuery ( "SHOW DATABASES" );
//New database array
$databases = array ();
//Loop the query
while ( $database = mysqli_fetch_assoc ( $databaseSQL ) )
{
//Create a new array of tables for each database
$tables = array ();
$status = array ();
$size = array ();
foreach ( $database as $key => $value )
{
//Set the table array to get the tbles from the database
$tables = $this->tree_db_getTables ( $value );
$status = $this->_db_getStatus ( $value );
$size = $this->_db_getSize ( $value );
}
//Add the tables to the database array
$databases [] = array (
"aDatabase" => $value, "aData" => $key, "aType" => "database", "aIcon" => "database", "aStatus" => $status, "aSize" => $size, "aTables" => $tables
);
}
$databaseFolder [] = array (
'label' => 'Schemas', 'children' => $databases
);
return $databaseFolder;
}
/**
* I get the users auth
*
* @return [array]
*/
private function tree_db_getAuthorizations()
{
$authorizationsArray = array (
'label' => 'Authorization IDs', 'children' => array (
'label' => 'rfd'
)
);
return $authorizationsArray;
}
//TODO:
private function tree_db_getDependcenies( $database )
{
$dependceniesArray = array (
'label' => 'Dependcencies', 'children' => array (
'label' => 'test'
)
);
return $dependceniesArray;
}
//TODO:
private function tree_db_getStoredProcs( $database )
{
$storedProcsArray = array (
'label' => 'Stored Procedures', 'children' => array (
'label' => 'test'
)
);
return $storedProcsArray;
}
/**
* I get the tables
*
* @param [string] $database the database
* @return [array]
*/
private function tree_db_getTables( $database )
{
//table query
$tableSQL = $this->realQuery ( "SHOW TABLES FROM $database" );
//create a new array of tables
$tables = array ();
//loop all the results
while ( $table = mysqli_fetch_assoc ( $tableSQL ) )
{
$columns = array ();
$statuss = array ();
$indexes = array ();
$constraints = array ();
$dependicies = array ();
$triggers = array ();
//for each table in the result make an array
foreach ( $table as $t_key => $t_value )
{
//get the tables fields for each table
$columns = $this->tree_tbl_getColumns ( $database, $t_value );
//now get the primary key for each table
$constraints = $this->tree_tbl_getConstraints ( $database, $t_value );
//now get the indexes for each table
$indexes = $this->tree_tbl_getIndexes ( $database, $t_value );
//now get the dependencys for each table
$dependicies = $this->tree_tbl_getDependcenies ( $database, $t_value );
//now get the triggers for each table
$triggers = $this->tree_tbl_getTriggers ( $database, $t_value );
//now get the status for each table
$statuss = $this->_tbl_getStatus ( $database, $t_value );
}
$columnArr = $columns;
$constraintArr = $constraints;
$indexArr = $indexes;
$dependencyArr = $dependicies;
$triggerArr = $triggers;
$statusArr = $statuss;
$tables [] = array (
"label" => $t_value, "type" => "table", "icon" => "table", "children" => array (
$columnArr, $constraintArr, $indexArr, $dependencyArr, $triggerArr, $statusArr
)
);
}
$tableFolder [] = array (
'label' => 'Tables', 'children' => $tables
);
return $tableFolder;
}
//TODO:
private function tree_db_getUserFunctions( $database )
{
}
//TODO:
private function tree_db_getViews( $database )
{
}
/**
* I get the columns
*
* @param [string] $database
* @param [string] $table
* @return [array]
*/
private function tree_tbl_getColumns( $database, $table )
{
$sql = "SHOW FIELDS FROM $database.$table";
$query = $this->realQuery ( $sql );
$columnsArray = array ();
while ( $row = mysqli_fetch_row ( $query ) )
{
$type = strtoupper ( $row [ 1 ] );
$null = '';
//Check if the column can be null
if ( $row [ 2 ] == 'YES' )
{
$null = 'Nullable';
}
$type = '[' . $type . ' ' . $null . ']';
$columnsArray [] = array (
'label' => $row [ 0 ] . ' ' . $type
);
}
//Create the folder
$columnsFolder = array (
'label' => 'Columns', 'children' => $columnsArray
);
return $columnsFolder;
}
/**
* I get the primary keys
*
* @param [string] $database
* @param [string] $table
* @return [array]
*/
private function tree_tbl_getConstraints( $database, $table )
{
$sql = "SHOW INDEX FROM $database.$table";
$result = $this->realQuery ( $sql );
$constraintArray = array ();
while ( $constraint = mysqli_fetch_assoc ( $result ) )
{
//check if the key is the primary key
if ( $constraint [ 'Key_name' ] == 'PRIMARY' )
{
$constraintArray = array (
'label' => $constraint [ 'Key_name' ]
);
}
}
$constraintFolder = array (
'label' => 'Constraints', 'children' => array (
$constraintArray
)
);
return $constraintFolder;
}
//TODO:
/**
* I get the dependcencies
*
* @param [string] $database
* @param [string] $table
* @return [array]
*/
private function tree_tbl_getDependcenies( $database, $table )
{
$dependArray = array (
'label' => 'admin table'
);
$dependFolder = array (
'label' => 'Dependencies', 'children' => array (
$dependArray
)
);
return $dependFolder;
}
/**
* I get the indexes
*
* @param [string] $database
* @param [string] $table
* @return [array]
*/
private function tree_tbl_getIndexes( $database, $table )
{
$sql = "SHOW INDEX FROM $database.$table";
$query = mysqli_query ( $this->mysqli, $sql );
$indexArray = array ();
while ( $row = mysqli_fetch_row ( $query ) )
{
if ( $row [ 2 ] !== 'PRIMARY' )
{
$indexArray [] = array (
'label' => $row [ 4 ] . "($row[2])"
);
}
}
$indexFolder = array (
'label' => 'Indexes', 'children' => $indexArray
);
return $indexFolder;
}
//TODO:
/**
* I get the triggers
*
* @param [string] $database
* @param [string] $table
* @return [array]
*/
private function tree_tbl_getTriggers( $database, $table )
{
$triggerArray = $this->queryToARRAY ( "SHOW INDEX FROM $database.$table" );
$triggerFolder = array (
'label' => 'Triggers', 'children' => array (
$triggerArray
)
);
return $triggerFolder;
}
/**
* I get the table status
*
* @param [string] $database
* @param [string] $table
* @return [array]
*/
private function _tbl_getStatus( $database, $table )
{
return $this->queryToARRAY ( "SHOW TABLE STATUS FROM $database LIKE '$table'" );
}
/**
* I get the size of all the databases
*
* @param [string] $database the database
* @return [array]
*/
private function _db_getSize( $database )
{
$statusSQL = $this->realQuery ( "SHOW TABLE STATUS FROM $database" );
$sizeArray = array ();
$totalSize = 0;
$dataSize = 0;
$indexSize = 0;
//loop all the results
while ( $size = mysqli_fetch_assoc ( $statusSQL ) )
{
$dataSize += $size [ 'Data_length' ];
$indexSize += $size [ 'Index_length' ];
}
$totalSize = $dataSize + $indexSize;
$sizeArray [] = array (
'totalSize' => $totalSize, 'dataSize' => $dataSize, 'indexSize' => $indexSize
);
return $sizeArray;
}
/**
* I get the status of the all the tables for a database.
*
* @param [string] $database the database
* @return [array]
*/
private function _db_getStatus( $database )
{
return $this->queryToARRAY ( "SHOW TABLE STATUS FROM $database" );
}
/**
* I execute a query and return the results as json.
*
* @param [string] $sql the query to be executed
* @return [json] the result in json
*/
private function queryToJSON( $sql )
{
$result = $this->realQuery ( $sql );
while ( $row = mysqli_fetch_assoc ( $result ) )
{
$array [] = $row;
}
return json_encode ( $array );
}
/**
* I execute a query and return the result as an array.
*
* @param [string] $sql the query to be executed
* @return [array] the result array
*/
public function queryToARRAY( $sql )
{
$query = $this->realQuery ( $sql );
$array = array ();
while ( $row = mysqli_fetch_assoc ( $query ) )
{
$array [] = $row;
}
return $array;
}
/**
* I get the query status
*
* @param [string] $sql
* @return [json] mysql status with the ('_') striped out
*/
public function queryStatusToJSON( $sql )
{
$result = $this->realQuery ( $sql );
while ( $row = mysqli_fetch_assoc ( $result ) )
{
//replace some of the names
$row = str_replace ( 'Com_', '', $row );
//take out the _ of the rows
$row = str_replace ( '_', ' ', $row );
$array [] = $row;
}
sort ( $array );
return json_encode ( $array );
}
/**
* I dump vars
*
* @param [string] $title the title of the dump
* @param [var] $var the var
*/
public function dump( $title, $var )
{
print "<h4>$title</h4>";
print "<pre>";
print_r ( $var );
print "</pre>";
}
/**
* @return [link]
*/
public function getMysqli()
{
return $this->mysqli;
}
/**
* @param [link] $mysqli
*/
public function setMysqli( $mysqli )
{
$this->mysqli = $mysqli;
}
}
?>
liamchapman/PHP Array Debug ( PHP)
function debug_r($array=null,$dump=false,$string_type='serialize')
{
echo '<pre>'."\r\n";
if(is_array($array))
{
if(count($array) > 0)
{
if($dump)
{
var_dump($array);
}else{
print_r($array);
}
}else{
echo "Array empty"."\r\n";
}
}else if(is_string($array))
{
switch($string_type)
{
default:
case 'serialize':
unserialize($array);
break;
case 'json':
json_decode($array);
break;
}
if(count($array) > 0)
{
if($dump)
{
var_dump($array);
}else{
print_r($array);
}
}else{
echo "Array empty"."\r\n";
}
}else{
echo 'Invalid format'."\r\n";
}
echo '</pre>'."\r\n";
}//end debug_r
a more useful way of printing php arrays in a nice readable format and able to decode serialized and json_encoded strings.
Raymond Hettinger/DBM with Dict-speed accesses and Multiple standard file formats ( python)
'''Alternate DB based on a dict subclass
Runs like gdbm's fast mode (all writes all delayed until close).
While open, the whole dict is kept in memory. Start-up and
close time's are potentially long because the whole dict must be
read or written to disk.
Input file format is automatically discovered.
Output file format is selectable between pickle, json, and csv.
All three are backed by fast C implementations.
'''
import pickle, json, csv
import os, shutil
class DictDB(dict):
def __init__(self, filename, flag=None, mode=None, format=None, *args, **kwds):
self.flag = flag or 'c' # r=readonly, c=create, or n=new
self.mode = mode # None or octal triple like 0x666
self.format = format or 'csv' # csv, json, or pickle
self.filename = filename
if flag != 'n' and os.access(filename, os.R_OK):
file = __builtins__.open(filename, 'rb')
try:
self.load(file)
finally:
file.close()
self.update(*args, **kwds)
def sync(self):
if self.flag == 'r':
return
filename = self.filename
tempname = filename + '.tmp'
file = __builtins__.open(tempname, 'wb')
try:
self.dump(file)
except Exception:
file.close()
os.remove(tempname)
raise
file.close()
shutil.move(tempname, self.filename) # atomic commit
if self.mode is not None:
os.chmod(self.filename, self.mode)
def close(self):
self.sync()
def dump(self, file):
if self.format == 'csv':
csv.writer(file).writerows(self.iteritems())
elif self.format == 'json':
json.dump(self, file, separators=(',', ':'))
elif self.format == 'pickle':
pickle.dump(self.items(), file, -1)
else:
raise NotImplementedError('Unknown format: %r' % self.format)
def load(self, file):
# try formats from most restrictive to least restrictive
for loader in (pickle.load, json.load, csv.reader):
file.seek(0)
try:
return self.update(loader(file))
except Exception:
pass
raise ValueError('File not in recognized format')
def dbopen(filename, flag=None, mode=None, format=None):
return DictDB(filename, flag, mode, format)
if __name__ == '__main__':
import random
os.chdir('/dbm_sqlite/alt')
print(os.getcwd())
s = dbopen('tmp.shl', 'c', format='json')
print(s, 'start')
s['abc'] = '123'
s['rand'] = random.randrange(10000)
s.close()
f = __builtins__.open('tmp.shl', 'rb')
print (f.read())
f.close()
dbdict: a dbm based on a dict subclass.
On open, loads full file into memory. On close, writes full dict to disk (atomically). Supported output file formats: csv, json, and pickle. Input file format automatically discovered.
Usable by the shelve module for fast access.
thiagotargino/Like/Dislike Fanpages Facebook ( PHP)
<?php
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
// var_dump($data);die();
$pageLiked = $data['page']['liked'];
if($pageLiked){
header('Location: like-on.html');
}
else{
header('Location: like-off.html');
}
?>
Redirecionamento Like/Dislike para Fanpages do Facebook.
zack/Get data from a URL using cURL PHP ( PHP)
<?php
$url='http://twitter.com/statuses/user_timeline/16387631.json'; //rss link for the twitter timeline
print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
/* gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
qrist0ph/jquery CheatSheet ( HTML)
<html>
<head>
<title>Titel der Webseite</title>
<!-- Evtl. weitere Kopfinformationen -->
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function doSomething(){
alert($("#username").val());
$("#eineDiv").html ='<h1 id="toll">textfsfsf</h1>'
}
/*
* Ajax XmlHttpRequest senden
*/
function login(){
$.getJSON('/login_ajax/',{ name: $("#username").val(), password: "einPassword" }, login_callback);
}
/*
* Ajax Callback Funktion
*/
function login_callback(json,text){
setCookie('sessionid',json.sessionid,365);
}
/*
* Funktionen an bestimmte Elemente haengen
*/
$(function () {
$('.add-row').click(function() {
return addForm(this, 'form');
});
})
</script>
</head>
<body onload="doSomething()">
<div id="eineDiv"> Webseite</div>
<input type="text" id="username" />
<input type="button" name="Text 1" value="Text 1 anzeigen" onclick="login()">
</body>
</html>
/// ------ Die login callback (mit Pyyhon) -----
from django.utils import simplejson
return HttpResponse(simplejson.dumps({'sessionid':request.session.session_key,'username':user.username}),status=200, mimetype='application/javascript')
dtbaker/Get the number of Facebook Likes using PHP ( PHP)
<?php
// the url to check how many likes
$url = 'http://www.likewizard.com/like-67';
// build the facebook query
$fburl = "http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url='$url'&format=atom";
// grab the atom dump via facebook api url call above
$ch = curl_init($fburl); // url for page
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$atom_data = curl_exec($ch);
// it returns something like this:
/* <fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
<link_stat>
<like_count>9</like_count>
</link_stat>
</fql_query_response> */
// grab the like count out, i hate dom parsing, so just use regex:
preg_match('#like_count>(\d+)<#',$atom_data,$matches);
$like_count = $matches[1];
echo "The URL $url has $like_count likes on facebook";
// OPTION 2 >>> keeping it to a 1 liner:
$data = json_decode(file_get_contents("http://api.facebook.com/method/fql.query?query=select%20like_count%20from%20link_stat%20where%20url='$url'&format=json"));
echo "The URL $url has " . $data[0]->like_count . " likes on facebook";
?>
Use the facebook api to grab how many \\'likes\\' a url has had.
asimmittal/Scrape list of all NBA players ( Python)
import urllib2, time, traceback, json
# Removes non ascii characters from a string 's'
def removeNonAscii(s): return "".join(i for i in s if ord(i)<128)
if __name__ == '__main__':
baseUrl = "http://www.nba.com/historical/search/index.jsp?fl=(%l)&pager.offset=(%o)#results"
letter = "(%l)"
offset = "(%o)"
startTime = time.asctime();
# ASCII values for letters from A to Z
ordAllLetters = range(ord('A'), ord('Z') + 1)
resultsPerPage = 50
countPlayers = 0
countPagesScraped = 0
dictPlayerPageUrls = {}
# Keep scraping if there are letters still left in the list of all letters
while len(ordAllLetters) > 0:
# Grab the first letter from the list and try scraping the pages for that letter
# For every letter, there are multiple pages. Each page contains 50 names of
# athletes
each = ordAllLetters[0];
curLetter = chr(each)
curUrl = baseUrl.replace(letter,curLetter).replace(offset,str(''))
# For this letter, find out how many pages exist. We can do this by scraping
# the "rnav" div at the bottom of the page. The number of <a> in that div
# will tell you how many pages there are against this letter. Each page has
# 50 results.
try:
html = urllib2.urlopen(curUrl, timeout=5).read();
resultsCountDivTagStart = '<div class="rnav">'
resultsCountDivTagStop = "</div>"
indexResultCountStart = html.find(resultsCountDivTagStart)
indexResultCountStop = html.find(resultsCountDivTagStop, indexResultCountStart)
resultsArea = html[indexResultCountStart:indexResultCountStop]
linkSearchTag = "</a>"
lstResultsLink = resultsArea.split(linkSearchTag)
del lstResultsLink[0]
# number of items in lstResultsLink represents the number of offset pages
# this letter has. Each page has 50 results per page. The offset values
# range from 0, 50, 100... and so on. So let's create a list that contains
# the exact offset values that we need to add to the baseUrl to get the appropriate
# page for this letter.
listPageOffsets = [i * resultsPerPage for i in range(0,len(lstResultsLink))]
if listPageOffsets == [] : listPageOffsets = [0]
print '\nLetter:', curLetter, ' pages:', len(lstResultsLink), ' offset values:', listPageOffsets
# Now listPageOffsets contains [0,50,100,150...]. For each letter, we have all
# the offsets. So let's create the url for every offset, download that page
# and grab the links for every player's individual pages.
for eachOffsetPage in listPageOffsets:
# We don't want to overload the NBA.com webserver with mulitple calls in a short
# span of time, cuz if you do you'll notice they will lock you out (calls time out)
# So we'll slow things down by making calls only every half a second
time.sleep(0.5)
# So for this page offset, construct the url and GET it
strOffset = str(eachOffsetPage)
curUrl = baseUrl.replace(letter,curLetter).replace(offset,strOffset)
countPagesScraped += 1
try:
# fetch the page and grab the results table. That contains
# the list of players.
htmlPage = urllib2.urlopen(curUrl).read();
indexStart = htmlPage.find('<div id="tableContainer">')
indexStop = htmlPage.find("<script>",indexStart)
resultsBody = htmlPage[indexStart:indexStop]
lstResults = resultsBody.split('<tr class="resultsTable" >')
del lstResults[0];
print '--------> offset: ', eachOffsetPage
# Okay now each element in lstResults represents the html
# surrounding a player. Let's process each element in lstResults
# to extract player data
for eachPlayerHtml in lstResults:
indexStart = eachPlayerHtml.find("<a href='") + len("<a href='")
indexEnd = eachPlayerHtml.find("</a>")
lstPlayerInfo = eachPlayerHtml[indexStart:indexEnd].split("'>")
link = "http://nba.com/"+lstPlayerInfo[0]; name = lstPlayerInfo[1]
key = link[link.find('=') + 1:]
active = True if ('ACTIVE' in eachPlayerHtml) else False
# Now we have a player's details - Name, PageURL & if still active
# Let's save this in a dictionary so that we can use this later
dictPlayerPageUrls[key] = {
'index': countPlayers,
'name' : removeNonAscii(name),
'link' : link,
'active' : active
};
countPlayers+=1
print '------------------------->',countPlayers,":",name
except:
traceback.print_exc()
print 'Exception in player page. Letter: ' + curLetter + ' Offset: ' + strOffset
# All the pages for this letter have been scraped. Let's get rid
# of it and go to the next letter
del ordAllLetters[0]
except :
# Something went wrong while trying to scrape this letter's pages
# so the letter has not been removed from the list 'ordAllLetters'
# and we'll try scraping its pages again and again until its done
print '*********> Exception on letter: ', curLetter
print 'Started at: ', startTime
print 'Ended at: ', time.asctime()
print 'Total number of Players: ', countPlayers
print 'Total pages scraped:', countPagesScraped
# When the control reaches here, all the URLs for the players in the nba.com historical
# stats are scraped and stored in the dictionary. Let's save that in a JSON file for later
try:
strJSON = json.dumps(dictPlayerPageUrls)
f = open('players.json','w')
f.write(strJSON)
f.close()
except UnicodeDecodeError:
print '\n\n\nError in JSON string with some non utf-8 characters'
This is a python script that allows you to scrape historical player names and links from NBA.com historical data