Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Skrypt wykonuje się ociężale i "szarpie"
FoxiK13
post 8.04.2019, 11:34:03
Post #1





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 8.04.2019

Ostrzeżenie: (0%)
-----


Witam, mam taki problem, że skrypt ze spoilera wykonuję się bardzo wolno (4.5 minuty przy 1500 komentarzach do pobrania). Dokładniej wygląda to tak:

pobranie w ciągu ułamka sekundy kilkadziesięciu wyników -> kilkanaście sekund (+/-) przerwy -> znowu pobiera -> znowu przerwa i tak aż do końca wykonywania. Może jakaś dobra dusza rzucić okiem i sprawdzić co jest nie tak, że trwa to aż tyle?


  1. <?php
  2.  
  3. $VIDEO_URL = \\\\\\\"MdIpZisWECA\\\\\\\";
  4.  
  5. if (!file_exists(__DIR__ . \\\\\\\'/vendor/autoload.php\\\\\\\')) {
  6. throw new Exception(sprintf(\\\\\\\'Please run \\\\\\\"composer require google/apiclient:~2.0\\\\\\\" in \\\\\\\"%s\\\\\\\"\\\\\\\', __DIR__));
  7. }
  8. require_once __DIR__ . \\\\\\\'/vendor/autoload.php\\\\\\\';
  9.  
  10. $client = new Google_Client();
  11. $client->setApplicationName(\\\\\\\'API code samples\\\\\\\');
  12. $client->setDeveloperKey(\\\\\\\'AIzaSyC4Xxxxxxxxxxxxxxxxm2XHnY\\\\\\\');
  13.  
  14. // Define service object for making API requests.
  15. $service = new Google_Service_YouTube($client);
  16.  
  17. $queryParams = [
  18. \\\\\\\'maxResults\\\\\\\' => 100,
  19. \\\\\\\'moderationStatus\\\\\\\' => \\\\\\\'published\\\\\\\',
  20. \\\\\\\'order\\\\\\\' => \\\\\\\'relevance\\\\\\\',
  21. \\\\\\\'textFormat\\\\\\\' => \\\\\\\'plainText\\\\\\\',
  22. \\\\\\\'videoId\\\\\\\' => $VIDEO_URL,
  23. \\\\\\\'prettyPrint\\\\\\\' => true
  24. ];
  25.  
  26. $response = $service->commentThreads->listCommentThreads(\\\\\\\'replies\\\\\\\', $queryParams);
  27.  
  28. //print_r($response);
  29. $TotalResults = $response->getPageInfo()->getTotalResults();
  30. $ResultsPerPage = $response->getPageInfo()->getResultsPerPage();
  31. ?><table class=\\\\\\\"tabelka\\\\\\\"><?php
  32.  
  33. $is_last = false;
  34.  
  35. for(;;){
  36. for($i=0; $i<$TotalResults; $i++){
  37.  
  38. $ID_aktualnego_komentarza = $response->getItems()[$i]->getId();
  39.  
  40. $queryParams = [
  41. \\\\\\\'id\\\\\\\' => $ID_aktualnego_komentarza,
  42. \\\\\\\'maxResults\\\\\\\' => 100
  43. ];
  44.  
  45. $komentarz = $service->comments->listComments(\\\\\\\'snippet\\\\\\\', $queryParams);
  46.  
  47. ?><tr><td class=\\\\\\\"tabelka\\\\\\\"><?php
  48. echo $komentarz->getItems()[0]->getSnippet()->getAuthorDisplayName();
  49. ?></td><td class=\\\\\\\"tabelka\\\\\\\"><?php
  50. echo $komentarz->getItems()[0]->getSnippet()->getTextDisplay();
  51. ?></td></tr><?php
  52. }
  53.  
  54. if($is_last)
  55. break;
  56.  
  57. $NextPageToken = $response->getNextPageToken();
  58. $queryParams1 = [
  59. \\\\\\\'maxResults\\\\\\\' => 100,
  60. \\\\\\\'moderationStatus\\\\\\\' => \\\\\\\'published\\\\\\\',
  61. \\\\\\\'order\\\\\\\' => \\\\\\\'relevance\\\\\\\',
  62. \\\\\\\'textFormat\\\\\\\' => \\\\\\\'plainText\\\\\\\',
  63. \\\\\\\'videoId\\\\\\\' => $VIDEO_URL,
  64. \\\\\\\'pageToken\\\\\\\' => $NextPageToken,
  65. \\\\\\\'prettyPrint\\\\\\\' => true
  66. ];
  67.  
  68. $response = $service->commentThreads->listCommentThreads(\\\\\\\'replies\\\\\\\', $queryParams1);
  69. $TotalResults = $response->getPageInfo()->getTotalResults();
  70. $ResultsPerPage = $response->getPageInfo()->getResultsPerPage();
  71.  
  72. if($TotalResults != $ResultsPerPage){
  73. $is_last=true;
  74. }
  75. }
  76. ?></table>


Ten post edytował FoxiK13 8.04.2019, 23:34:50
Go to the top of the page
+Quote Post
gitbejbe
post 8.04.2019, 19:46:54
Post #2





Grupa: Zarejestrowani
Postów: 515
Pomógł: 63
Dołączył: 27.08.2012

Ostrzeżenie: (0%)
-----


wygląda na to że spamujesz requestami i Cię blokują. nie znam tego api, używasz najnowszej wersji ? Skąd masz skrypt ? Poczytaj dokumentacje api
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 20.04.2024 - 00:42