sub_categories_array = array(); foreach ($pt->sub_categories as $cat_key => $subs) { $pt->sub_categories_array["'".$cat_key."'"] = ''; foreach ($subs as $sub_key => $sub_value) { $selected = ''; if (!empty($_GET['sub_category_id'])) { $selected = ($_GET['sub_category_id'] == $sub_key) ? 'selected' : ''; } $pt->sub_categories_array["'".$cat_key."'"] .= ''; } } $query = ''; $offset = ''; $cursor = ''; $multi_query = ''; $limit = 50; $respond_error = 0; $twitch_user_id = 0; $page_id = 1; $page = 'current'; if (empty($pt->config->twitch_api)) { $respond_error = 1; $error_text = 'please put your Twitch Client Id in Settings > General Settings to start import videos from Twitch.'; } if (!empty($_GET['query']) || !empty($_GET['multi-query'])) { if (empty($pt->config->twitch_access_token)) { header("Location: https://id.twitch.tv/oauth2/authorize?client_id=".$pt->config->twitch_api."&scope=bits:read+openid&response_type=token%20id_token&redirect_uri=".PT_LoadAdminLinkSettings('import-from-twitch')); exit(); } if (!empty($_GET['query'])) { $query = PT_Secure($_GET['query']); } $clientId = $pt->config->twitch_api; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.twitch.tv/helix/search/channels?query='.$query); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); $headers = array(); $headers[] = 'Client-ID: ' . $clientId; $headers[] = 'Authorization: Bearer '.$pt->config->twitch_access_token; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch); if (!empty($result)) { $json = json_decode($result,true); if (!empty($json) && !empty($json['data'])) { $twitch_user_id = $json['data'][0]['id']; if (!empty($_GET['limit']) && $limit < 101) { $limit = (int) PT_Secure($_GET['limit']); } if (!empty($_GET['page-query'])) { $page = $_GET['page-query']; } if (!empty($_GET['cursor'])) { $cursor = PT_Secure($_GET['cursor']); } $query = urlencode($query); for ($i=0; $i < 100; $i++) { $channelsApi = 'https://api.twitch.tv/helix/videos?first='.$limit.'&user_id='.$twitch_user_id; if (!empty($cursor) && $page == 'next') { $channelsApi = 'https://api.twitch.tv/helix/videos?first='.$limit.'&after='.$cursor.'&user_id='.$twitch_user_id; } if (!empty($cursor) && $page == 'previous') { $channelsApi = 'https://api.twitch.tv/helix/videos?first='.$limit.'&before='.$cursor.'&user_id='.$twitch_user_id; } $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_HTTPHEADER => array( 'Client-ID: ' . $clientId, 'Authorization: Bearer '.$pt->config->twitch_access_token ), CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $channelsApi )); $response = curl_exec($ch); curl_close($ch); if (!empty($response)) { $get_videos = json_decode($response,true); if (!empty($get_videos['data'])) { if (!empty($get_videos['pagination']) && !empty($get_videos['pagination']['cursor'])) { $cursor = PT_Secure($get_videos['pagination']['cursor']); $page = 'next'; } $ids = array(); foreach ($get_videos['data'] as $key => $video) { $check_if_exists = $db->where('twitch', $video['id'])->getValue(T_VIDEOS, 'count(*)'); if ($check_if_exists == 0) { $ids[] = $video['id']; } } if (!empty($ids)) { break; } else{ } } elseif (!empty($get_videos) && !empty($get_videos['error']) && !empty($get_videos['message'])) { $respond_error = 1; $error_text = $get_videos['message']; break; } } else{ $respond_error = 1; $error_text = 'No videos found'; } } } elseif (!empty($json) && !empty($json['error']) && !empty($json['message'])) { if (strtolower($json['error']) == 'unauthorized') { header("Location: https://id.twitch.tv/oauth2/authorize?client_id=".$pt->config->twitch_api."&scope=bits:read+openid&response_type=token%20id_token&redirect_uri=".PT_LoadAdminLinkSettings('import-from-twitch')); exit(); } $respond_error = 1; $error_text = $json['message']; } } else{ $respond_error = 1; $error_text = 'Something went wrong Please try again later'; } } $user_id = 0; $username = ''; if (!empty($_GET['username']) && isset($_GET['username'])) { $username = PT_Secure($_GET['username']); $user = $db->where('username', $username)->getOne(T_USERS); if (!empty($user)) { $user_id = $user->id; } else{ $respond_error = 1; $error_text = 'user not found'; } } $category_filter = array_keys(ToArray($pt->categories))[0]; if (!empty($_GET['category_id'])) { if (in_array($_GET['category_id'], array_keys(ToArray($pt->categories)))) { $category_filter = $_GET['category_id']; } } $get_ini = ini_get('max_input_vars'); $auto_import = 0; if (!empty($_GET['auto_import'])) { $auto_import = 1; } ?>