Skip to content

Commit

Permalink
Merge pull request #3 from ibnux/development
Browse files Browse the repository at this point in the history
importer from google drive
  • Loading branch information
ibnux authored Dec 22, 2020
2 parents d69c287 + ea7beab commit 0fbd2da
Show file tree
Hide file tree
Showing 12 changed files with 325 additions and 2 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ data/games/nsz/**
!data/games/nsz/index.html
data/games/xci/**
!data/games/xci/index.html
import
!data/sms/.htaccess
!data/user/.htaccess
import/folder.txt
data/base.db
data/base.db
import/token/**
import/temp/**
!import/token/.htaccess
!import/temp/.htaccess
78 changes: 78 additions & 0 deletions import/drive2db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Import files inside google drive
*/
include "../vendor/autoload.php";
require '../config.php';
require 'function.php';

$dbpath = '../'.$dbpath;
require '../function.php';

$db = getDatabase();

$drives = explode("\n",str_replace("\r","",file_get_contents("./folder.txt")));
foreach($drives as $drive){
echo "$drive\n";
$drive = explode(" ",$drive);
$idfolder = trim($drive[0]);
$folder = trim($drive[1]);

$jsoncredential = json_decode(file_get_contents("data/credentials.txt"),true);
$sisa = time()-filemtime("token/credentials.txt");
if($sisa>$jsoncredential['expires_in']-300){
updateToken();
}
$pathPageToken = "./temp/$idfolder.token";
$pageToken = (file_exists($pathPageToken))?file_get_contents($pathPageToken):null;

ulang:
echo $pageToken;

$list = listFiles($idfolder,$pageToken);

foreach($list['items'] as $item){
if($item['fileExtension']=='nsz' || $item['fileExtension']=='xci'){
if($item['parents'][0]['id'] == $idfolder && !empty($item['title'])){
$gameid = getGameID($item['title']);
$gameName = $db->get("t_switch","name",['titleid'=>$gameid]);
if(empty($gameName)) $gameName = str_replace([".xci",".nsp",".nsz"],"",$item['title']);
$db->insert('t_games_url',[
'url'=>$item['id'],
'filename'=>$item['title'],
'title'=>$gameName,
'titleid'=>$gameid,
'fileSize'=>$item['fileSize'],
'md5Checksum'=>$item['md5Checksum'],
'root'=>$idfolder,
'owner'=>trim($item['owners'][0]['emailAddress']),
'folder'=>$folder,
'shared'=>($item['shared'])?"1":"0",
]);
$id = $db->id();
if($id){
echo $db->id()." - ".$item['title']."\n";
}else{
echo json_encode($db->error())."\n".$item['title']."\n";
}
}else{
echo "Parents different ".$item['parents'][0]['id']."\n";
}
}else{
echo "NOT XCI/NSZ - ".$item['title']."\n";
}
}


if(isset($list['nextLink']) && !empty($list['nextLink'])){
$pageToken = $list['nextLink'];
file_put_contents("$pathPageToken",$pageToken);
if(!empty($pageToken))
goto ulang;
else die("EMPTY $idfolder");
}

file_put_contents("$pathPageToken",'');
echo "$idfolder FINISH\n\n";
}

13 changes: 13 additions & 0 deletions import/file2db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Reserved for scanning folder and add it to database
*/
include "../vendor/autoload.php";
require '../config.php';
require 'function.php';

$dbpath = '../'.$dbpath;
require '../function.php';

$db = getDatabase();

1 change: 1 addition & 0 deletions import/folder.example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
folderid foldername note
Binary file added import/folder.example.txt.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added import/folder.example.txt.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions import/function.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

function updateToken(){
global $client_id ,$client_sc,$jsoncredential;
echo "update Token\n";
$refresh_token = file_get_contents("token/refresh.token");
$ch = curl_init('https://oauth2.googleapis.com/token');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode([
'client_id' => $client_id,
'client_secret' => $client_sc,
'refresh_token' => $refresh_token,
'grant_type' => 'refresh_token'
]));
$authToken = curl_exec($ch);
curl_close($ch);
$jsoncredential = json_decode($authToken,true);
if(isset($jsoncredential['access_token'])){
file_put_contents("token/credentials.txt", $authToken);
}
}


function listFiles($folderID,$nextToken){
global $jsoncredential,$proxy;
if(!empty($nextToken)){
$url = $nextToken;
}else{
$url = 'https://www.googleapis.com/drive/v2/files?q=\''.$folderID.'\'+in+parents' ;
}
echo "$url\n";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization: Bearer '.$jsoncredential['access_token']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result,true);
}

function getGameID($str){
$regex = '/\[\w{16,16}\]/';
preg_match_all($regex, $str, $matches);
$hasil = str_replace("[","",str_replace("]","",$matches[0][0]));
echo "GameID $hasil\n";
return $hasil;
}
126 changes: 126 additions & 0 deletions import/importGameData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* Just run from Commandline php importGameData.php
*/
include "../vendor/autoload.php";
include "../config.php";
$dbpath = '../'.$dbpath;
include "../function.php";
$db = getDatabase();

#nswdb

$xmls = (array)simplexml_load_string(file_get_contents('http://nswdb.com/xml.php'));

$releases = $xmls['release'];
echo count($releases) . " titles\n";
sleep(4);
foreach ($releases as $release) {
$data = (array) $release;
$titleid = preg_replace("/[^a-zA-Z0-9]+/", "", $data['titleid']) . "";
$game = $db->get("t_games", ['titleid', 'size'], ['titleid' => $titleid]);
if (empty($game)) {
try {
$db->insert("t_games", [
'titleid' => preg_replace("/[^a-zA-Z0-9]+/", "", $data['titleid']) . "",
'name' => $data['name'] . "",
'publisher' => $data['publisher'] . "",
'languages' => $data['languages'] . "",
'size' => $data['trimmedsize'] . "",
'description' => "",
'image' => "",
'rating' => "",
'size' => 0
]);
if($db->id()>0){
echo $titleid . " " . $db->id() . "\n";
}else{
print_r($db->error());
}
} catch (Exception $e) {
print_r($data);
echo "\n";
echo $data['name'] . " ERROR\n";
}
} else {
if (empty($game['size'])) {
$db->update("t_games", ['size' => $data['trimmedsize'] * 1], ['titleid' => $titleid]);
echo $titleid . " " . $data['trimmedsize'] . "\n";
}
}
}
#tinfoil
$releases = json_decode(file_get_contents("https://raw.githubusercontent.com/blawar/titledb/master/US.en.json"), true);
echo count($releases) . " titles\n";
sleep(4);
foreach ($releases as $version => $data) {
try {
if (!empty($data['name'])) {
if (strlen($data['id']) >= 16) {
$titleid = substr(preg_replace("/[^a-zA-Z0-9]+/", "", $data['id']), 0, 16);
if ($db->has('t_games', ['titleid' => $data['id']])) {
if (!empty($data['iconUrl'] && empty($db->get("t_games", 'image', ['titleid' => $titleid])))) {
echo "image update " . $data['name'] . "\n";
$db->update(
"t_games",
[
'image' => $data['iconUrl'] . ""
],
['titleid' => $titleid]
);
}
if (!empty($data['rating'] && empty($db->get("t_games", 'rating', ['titleid' => $titleid])))) {
echo "rating update " . $data['name'] . "\n";
$db->update(
"t_games",
[
'rating' => $data['rating'] . ""
],
['titleid' => $titleid]
);
}
if (!empty($data['description'] && empty($db->get("t_games", 'description', ['titleid' => $titleid])))) {
echo "description update " . $data['name'] . "\n";
$db->update(
"t_games",
[
'description' => $data['description'] . ""
],
['titleid' => $titleid]
);
}
if (empty($db->get("t_games", 'size', ['titleid' => $titleid]))) {
$hasil = $db->update(
"t_games",
[
'size' => $data['size'] * 1
],
['titleid' => $titleid]
);
if ($hasil->rowCount() > 0) {
echo $hasil->rowCount() . " size update " . $data['size'] . "\n";
} else {
print_r($db->error());
}
}
} else {
echo "insert " . $data['name'] . "\n";
$db->insert("t_games", [
'titleid' => preg_replace("/[^a-zA-Z0-9]+/", "", $data['id']) . "",
'name' => $data['name'] . "",
'publisher' => $data['publisher'] . "",
'languages' => implode(",", (array)$data['languages']),
'image' => $data['iconUrl'] . "",
'rating' => $data['rating'] . "",
'description' => $data['description'] . "",
'size' => $data['size'] * 1
]);
}
}
}
} catch (Exception $e) {
print_r($data);
echo "\n";
echo $data['name'] . " ERROR\n";
}
}
1 change: 1 addition & 0 deletions import/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This folder only run from command line
49 changes: 49 additions & 0 deletions import/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* open this from browser
* Oauth2 login for google
* its simple, why i don't use any library
*/



function connectWithGoogle($scopes, $redirectURI)
{
global $client_id ,$client_sc;

if (!isset($_GET['code'])) {
header("Location: https://accounts.google.com/o/oauth2/v2/auth?response_type=code&prompt=consent&access_type=offline&client_id=".$client_id.
"&redirect_uri=".urlencode($redirectURI)."&scope=".urlencode(implode(' ',$scopes)));
} else {
$ch = curl_init('https://oauth2.googleapis.com/token');
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode([
'code' => urldecode($_GET['code']),
'client_id' => $client_id,
'client_secret' => $client_sc,
'redirect_uri' => $redirectURI,
'grant_type' => 'authorization_code'
]));
$authToken = curl_exec($ch);
curl_close($ch);
$json = json_decode($authToken,true);
if(isset($json['access_token'])){
//save refresh token
file_put_contents("token/refresh.token",$json['refresh_token']);
//save real token
file_put_contents("token/credentials.txt",$authToken);
}else{
echo "gagal";
}
echo $authToken;
}
}

connectWithGoogle(
$scopes,
$redirect
);
1 change: 1 addition & 0 deletions import/temp/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deny from all
1 change: 1 addition & 0 deletions import/token/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deny from all

0 comments on commit 0fbd2da

Please sign in to comment.