Skip to content

Instantly share code, notes, and snippets.

@httpspace
Created June 18, 2020 11:27
Show Gist options
  • Save httpspace/b86b64bac9e9603ee38cb8a58c5d696c to your computer and use it in GitHub Desktop.
Save httpspace/b86b64bac9e9603ee38cb8a58c5d696c to your computer and use it in GitHub Desktop.
wxx
<?php
namespace App\Schedule;
use \Medoo\Medoo as Medoo;
use Httpspace\Medoos as Medoos;
use Httpspace\Fastimage as FastImage;
use App\Model\Imgur;
class Wow
{
public static $cacheimg = [];
public static $from_site = 1;
// case "1":
// class_return = "擷取中";
// break;
// case "2":
// class_return = "處理中";
// break;
// case "3":
// class_return = "刪除";
// break;
// case "4":
// class_return = "處理完成";
// break;
// case "5":
// class_return = "處理失敗";
// break;
// case "6":
// class_return = "暫不處理";
// break;
// case "7":
// class_return = "已轉移";
// break;
//步驟一蒐集
public static function getList()
{
$type = [
'en',
'ja',
'es',
'id',
'fr',
'it',
'nl',
'pt',
'ms',
'hi',
'te',
'ar',
'th',
'hu',
'ru',
'ro',
'bn',
'ia',
'vi',
'tr',
'pl',
'sv',
'el',
'cs',
'tl',
];
$rand_lan = $type[rand(0, count($type) - 1)];
// $rand_lan = 'ja';
$url = "https://api.wowwquiz.com/v5/quiz_recent_list.php?page=0&size=500&lang={$rand_lan}";
$result = httpGet($url, true);
show($url);
if ($result['code'] == 4000 && isset($result['data']) && $result['data'] !== false) {
foreach ($result['data'] as $k => $v) {
$insert = [
'title' => $v['title'],
'locale' => $rand_lan,
'cover_img' => $v['preview'],
'from_id' => $v['id'],
'from_url' => $v['link'],
'mode' => 2,
'status' => 1,
'from_site' => self::$from_site,
'created_at' => date('Y-m-d H:i:s')
];
self::save($insert);
}
}
show(Medoos::log());
}
public static function save($data)
{
$find_id = Medoos::get('games_copy', 'from_id', ['from_id' => $data['from_id']]);
if (!$find_id) {
Medoos::insert('games_copy', $data);
}
}
//處理步驟二
public static function parse($id = false)
{
if ($id) {
$parse = Medoos::get('games_copy', '*', ['id' => $id]);
} else {
$parse = Medoos::get('games_copy', '*', ['status' => 1]);
}
$from_id = $parse['from_id'];
$id = $parse['id'];
$url = "https://api.wowwquiz.com/v5/quiz_info.php?id={$from_id}";
// echo $url;
$result = httpGet($url, true);
if ($result['code'] == 4000) {
$update = [
'result_origin[JSON]' => $result,
'status' => 2
];
} else {
//資料錯誤
$update = ['status' => 5];
}
// show($result);
Medoos::update("games_copy", $update, ['id' => $id]);
show(Medoos::log());
}
//處理步驟三
public static function trans($id = null)
{
if ($id) {
$data = Medoos::get('games_copy', ['id', 'result_origin[JSON]'], [
'id' => $id,
]);
} else {
$data = Medoos::get('games_copy', ['id', 'result_origin[JSON]'], [
'status' => 2,
]);
}
if (!$data) exit('無資料');
//$FB_INPUT_TEXT
// showx($data);
$id = $data['id'];
if (!$data['result_origin']) exit('無資料塗層');
$layers = $data['result_origin']['data']['layers'];
$can_trans = self::is_multiple_img($layers);
if (!$can_trans) {
echo "系統不支援";
$update = ['status' => 6];
Medoos::update("games_copy", $update, ['id' => $id]);
return;
}
echo '是否進行:';
show($can_trans);
show($layers);
echo '<br />';
$layers_trans = self::trans_data($layers);
if (empty($layers_trans)) {
Medoos::update('games_copy', ['result[JSON]' => $layers_trans, 'status' => 6], ['id' => $data['id']]);
} else {
Medoos::update('games_copy', ['result[JSON]' => $layers_trans, 'status' => 4], ['id' => $data['id']]);
}
show($layers_trans);
show(Medoos::log());
}
//處理方法
public static function trans_data($layers)
{
// showx($layers);
$common_layer = [];
$common_txt_layer = [];
$common_multi_layer = [];
$callFastImage = false;
$vals = [];
$width = 0;
$height = 0;
$r_key = -1;
foreach ($layers as $lk => $layer) {
if ($layer['rnd'] == 1) {
foreach ($layer['elements'] as $k => $elements) {
if (!isset($elements['data'])) break;
if (strpos($elements['data'], 'wowwquiz.com/images') !== false) {
// show($layer['elements']);
// showx(count($layer['elements']));
if (count($layer['elements']) == 1) {
if (!$callFastImage) {
$image = new FastImage($elements['data']);
list($width, $height) = $image->getSize();
$callFastImage = true;
}
if ($layer['id'] <= 2) {
$common_layer[] = [
"type" => "img",
"name" => "圖層-底圖",
"active" => false,
"display" => ($lk == 1) ? false : true,
"type" => "img",
"config" => [
"image" => $elements['data'],
"x" => 0,
"y" => 0,
"width" => $width,
"height" => $height
]
];
} else {
$common_txt_layer[] = [
"type" => "img",
"name" => "圖層-固定結果圖",
"active" => false,
"display" => true,
"type" => "img",
"config" => [
"image" => $elements['data'],
"x" => 0,
"y" => 0,
"width" => $width,
"height" => $height
]
];
}
}
} else if ($elements['data'] == '$CUSTOM_PIC' && isset($elements['cus_pic'])) {
$common_layer[] = [
"type" => "img",
"name" => "圖層-底圖",
"active" => false,
"display" => true,
"type" => "img",
"config" => [
"image" => $elements['cus_pic'],
"x" => 0,
"y" => 0,
"width" => $width,
"height" => $height
]
];
}
if (strpos($elements['data'], 'wowwquiz.com/images') !== false) {
if (count($layer['elements']) > 1) {
$common_multi_layer[] = [
"type" => "img",
"name" => "圖層-結果圖" . $k,
"active" => false,
"display" => true,
"type" => "img",
"config" => [
"image" => $elements['data'],
"x" => 0,
"y" => 0,
"width" => $width,
"height" => $height
]
];
}
}
if ($layer['data'] == '' && isset($elements['font'])) {
if (count($layer['elements']) == 1) {
$layer_name = (strpos($elements['data'], '$FB_USER_NAME') === false) ? '變數圖層' : '圖層-姓名';
$common_txt_layer[] = [
"type" => "txt",
"name" => $layer_name,
"active" => false,
"display" => true,
"config" => [
"color" => $elements['color'],
"size" => $elements['size'],
"x" => (int) $elements['x'] + 10,
"y" => (int) $elements['y'] + 25,
"opacity" => "1",
"textAlign" => $elements['align'],
"words" => str_replace('$FB_USER_NAME', '{{姓名}}', $elements['data'])
]
];
} else {
$now_data = str_replace('$FB_USER_NAME', '{{姓名}}', $elements['data']);
if ($k == 0) {
$layer_name = (strpos($elements['data'], '$FB_USER_NAME') === false) ? '變數圖層' : '圖層-姓名';
$r_key = $r_key + 1;
$r_val = "{{變數" . ($r_key + 1) . "}}";
$vals[$r_key] = [
"key" => $r_key + 1,
"type" => 1,
"before" => $r_val,
"after" => [$now_data]
];
$common_txt_layer[] = [
"type" => "txt",
"name" => $layer_name,
"active" => false,
"display" => true,
"config" => [
"color" => $elements['color'],
"size" => $elements['size'],
"x" => (int) $elements['x'] + 10,
"y" => (int) $elements['y'] + 25,
"opacity" => "1",
"textAlign" => $elements['align'],
"words" => $r_val
]
];
} else {
$vals[$r_key]["after"][] = $now_data;
}
}
}
// "type": "txt",
// "name": "新圖層-文字",
// "active": false,
// "display": true,
// "config": {
// "color": "#000000",
// "size": "30",
// "x": "276",
// "y": "223",
// "opacity": "1",
// "textAlign": "left",
// "words": "{{姓名}}:"
}
}
if ($layer['rnd'] == 0) {
foreach ($layer['elements'] as $k => $elements) {
if (!isset($elements['data'])) break;
if ($layer['data'] == '' && isset($elements['font'])) {
$layer_name = (strpos($elements['data'], '$FB_USER_NAME') === false) ? '變數圖層' : '圖層-姓名';
$common_txt_layer[] = [
"type" => "txt",
"name" => $layer_name,
"active" => false,
"display" => true,
"config" => [
"color" => $elements['color'],
"size" => $elements['size'],
"x" => (int) $elements['x'] + 10,
"y" => (int) $elements['y'] + 25,
"opacity" => "1",
"textAlign" => $elements['align'],
"words" => str_replace('$FB_USER_NAME', '{{姓名}}', $elements['data'])
]
];
}
}
}
}
$mylayer = [];
$mylayer_base = [
"config" => [
"width" => $width,
"height" => $height
],
"val" => $vals
];
foreach ($common_multi_layer as $k => $v) {
$mylayer[$k] = $mylayer_base;
$mylayer[$k]['layer'] = array_reverse(array_merge($common_layer, [$v], $common_txt_layer));
$mylayer[$k]['image'] = $v['config']['image'];
// showx($v);
// showx($mylayer[$k]['layer']);
// $mylayer[$k]['layer'] = $common_layer;
}
// show($common_layer);
// echo "txt----";
// show($common_txt_layer);
// echo "x----";
// show($common_multi_layer);
// echo "----";
return $mylayer = $mylayer;
}
//如果是多圖隨機的組合不適用系統 放棄
public static function is_multiple_img($layers)
{
$rand_img_set = 0;
$ok = true;
foreach ($layers as $key => $layer) {
if ($layer['rnd'] == 1) {
// echo 'layer at:';
// show($layer);
// rnd
foreach ($layer['elements'] as $k => $elements) {
if (isset($elements['buddle'])) {
$ok = false;
break;
}
if (!isset($elements['data'])) break;
if (strpos($elements['data'], 'wowwquiz.com/images') !== false) {
// show($layer['elements']);
// echo '$images';
if (count($layer['elements']) > 1) {
$rand_img_set++;
break;
}
}
if ($elements['data'] == '$CUSTOM_PIC') {
//目前無法處理題型
// echo '$CUSTOM_PIC';
if (count($layer['elements']) > 1) {
$rand_img_set++;
break;
}
if ($elements['x'] !== 0 || $elements['x'] !== 0) {
$ok = false;
break;
}
// $rand_img_set++;
}
// show($elements);
// break;
}
}
}
echo '多圖組:';
show($rand_img_set);
echo '是否支援:';
show($ok);
if ((int) $rand_img_set > 1) {
return false;
}
if (!$ok) {
return false;
}
return true;
}
//轉圖片
public static function toLocalImg($id = false)
{
try {
if ($id) {
$log = false;
$data = Medoos::get('games_copy', ['id', 'cover_img', 'cover_img_cache', 'result[JSON]'], [
'id' => $id
]);
} else {
$log = false;
$data = Medoos::get('games_copy', ['id', 'cover_img', 'cover_img_cache', 'result[JSON]'], [
'status' => 4
]);
$id = $data['id'];
}
// showx($data);
// if (strpos($data['cover_img'], 'app-files.funfunquiz.com') === false) {
$data['cover_img'] = self::imgApi($data['cover_img']);
$data['cover_img_cache'] = Imgur::upload(strtotime('now'), $data['cover_img'], 'url');
foreach ($data['result'] as $key => $val) {
$data['result'][$key]['image'] = self::imgApi($val['image']);
// show($data['result']);
foreach ($val['layer'] as $k => $v) {
if ($v['type'] == 'img') {
$data['result'][$key]['layer'][$k]['config']['image'] = self::imgApi($v['config']['image']);
}
}
}
$update = [
'cover_img' => $data['cover_img'],
'cover_img_cache' => $data['cover_img_cache'],
'result[JSON]' => $data['result'],
];
Medoos::update('games_copy', $update, [
'id' => $id
]);
// }
Medoos::init();
$insert = $data = Medoos::get('games_copy', [
"locale",
"title",
"cover_img",
"mode",
"cover_img_cache",
"result",
"type",
], [
'id' => $id
]);
// show($insert);
$insert['account_id'] = 10;
$insert['created_source'] = 2;
$insert['created_at'] = date('Y-m-d H:i:s');
Medoos::insert('games', $insert);
$update = [
'status' => 7,
'to_id' => Medoos::id(),
];
Medoos::update('games_copy', $update, [
'id' => $id
]);
if ($log) {
show(Medoos::log());
}
return true;
} catch (\Exception $e) {
return false;
}
}
public static function imgApi($url)
{
if (isset(static::$cacheimg[$url])) return static::$cacheimg[$url];
$data = httpGet("https://app-files.funfunquiz.com/api/grab_img.php?url={$url}", true);
if ($data['status']) {
static::$cacheimg[$url] = $data['url'];
return $data['url'];
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment