am_dot_com

ACA20201112

Nov 12th, 2020 (edited)
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.31 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. include => warning
  5. include "fich";
  6. include_once "caminho/fich";
  7.  
  8. require => error
  9. require "fich/sdsd/sdsD/f";
  10. */
  11. require_once "AmUtil.php";
  12.  
  13. //https://codeshare.luismeneses.pt/
  14. //https://curl.haxx.se/docs/sslcerts.html
  15. //https://curl.se/docs/caextract.html
  16.  
  17. //edit php.ini and add
  18. //curl.cainfo="C:/<path to my php installation folder>/cacert.pem"
  19.  
  20. define ("TEST_URL", "https://apod.nasa.gov/apod/ap141231.html");
  21. // ... "<IMG SRC="image/2011/M78... "
  22. //https://apod.nasa.gov/apod/image/2011/M78_LDN1622_BarnardsLoop_SEP27_28_Oct15_final1024.jpg
  23.  
  24. class ApodBot{
  25.     //consts?
  26.     const APOD_URL = "https://apod.nasa.gov/apod/";
  27.     const DESIRED_NUMBER_OF_DIGITS = 2;
  28.     //const BOT_SIGNATURE = "For educational tests only";
  29.     /*
  30.      * the structure of an APOD URL:
  31.      * https://apod.nasa.gov/apod/apYYMMDD.html
  32.      * e.g.: https://apod.nasa.gov/apod/ap141231.html
  33.      */
  34.  
  35.     private
  36.         $mCurrentYear,
  37.         $mCurrentMonth,
  38.         $mCurrentDay;
  39.  
  40.     public function __construct(){
  41.         /*
  42.          * Y - format for year with 4 digits
  43.          * m - format for month with 2 digits
  44.          * d - format for day with 2 digits
  45.          */
  46.         $strYMD = date("Y-m-d"); //"2020-11-05"
  47.  
  48.         $aDateParts = explode("-", $strYMD);
  49.         /*
  50.          * $aDateParts = [
  51.          * 0 => "2020", 1 => "11", 2 => "05"
  52.          * ];
  53.          */
  54.  
  55.         $this->mCurrentYear =
  56.             intval($aDateParts[0]);
  57.  
  58.         $this->mCurrentMonth =
  59.             intval($aDateParts[1]);
  60.  
  61.         $this->mCurrentDay =
  62.             intval($aDateParts[2]);
  63.     }//__construct
  64.  
  65.     //data members?
  66.  
  67.     //methods?
  68.     //https://apod.nasa.gov/apod/ap141231.html
  69.  
  70.     /*
  71.      * receives 3 ints: year, month, day
  72.      * returns a string in the format
  73.      * https://apod.nasa.gov/apod/apYYMMDD.html
  74.      */
  75.     public function urlForDay(
  76.         int $pYear=0, //2020
  77.         int $pMonth=0, //12->"12" 5->"05"
  78.         int $pDay=0 //31->"31" 1->"01"
  79.     ){
  80.         $strUrlForDay = "";
  81.         $strYear=$strMonth=$strDay="";
  82.  
  83.         //pattern for default values for parameters
  84.         $pYear = empty($pYear) ?
  85.             $this->mCurrentYear
  86.             :
  87.             $pYear;
  88.  
  89.         $pMonth =
  90.             empty($pMonth) ?
  91.             $this->mCurrentMonth
  92.             :
  93.             $pMonth;
  94.  
  95.         $pDay =
  96.             empty($pDay) ?
  97.             $this->mCurrentDay
  98.             :
  99.             $pDay;
  100.         /*
  101.         293029 (6 excede 2 = 6-2=4)
  102.         9999 (4 excede 2 em 4-2=2)
  103.         999 (3 excede 2 em 3-2=1)
  104.         */
  105.  
  106.         $strYear = $pYear . "";
  107.         if (
  108.             $iSizeOfYear=strlen($strYear)
  109.             >
  110.             self::DESIRED_NUMBER_OF_DIGITS
  111.         ){
  112.             $iExcessDigits =
  113.                 $iSizeOfYear
  114.                 -
  115.                 self::DESIRED_NUMBER_OF_DIGITS;
  116.             $strYear = substr(
  117.                 $strYear,
  118.                 $iExcessDigits-1, //start position
  119.                 self::DESIRED_NUMBER_OF_DIGITS //2 symbols
  120.             );
  121.         }//if there is need to fix the string for year
  122.  
  123.         $strMonth = $pMonth<10 ? "0".$pMonth : $pMonth."";
  124.  
  125.         $strDay = $pDay<10 ? "0".$pDay : $pDay."";
  126.  
  127.         /*
  128.          * sprintf is "print into a string following a format"
  129.          * https://apod.nasa.gov/apod/apYYMMDD.html
  130.          */
  131.         $strUrlForDay = sprintf(
  132.             "%sap%s%s%s.html",
  133.             self::APOD_URL, //"https://apod.nasa.gov/apod/"
  134.             $strYear,
  135.             $strMonth,
  136.             $strDay
  137.         );
  138.  
  139.         return $strUrlForDay;
  140.     }//urlForDay
  141.  
  142.     const IMAGE_MARK = "<img src=\"";
  143.     public function identifyImgInSourceCode(
  144.         $pSrcCode
  145.     ){
  146.         $iWhereDoesImgElementStart =
  147.             stripos(
  148.                 $pSrcCode,
  149.                 self::IMAGE_MARK
  150.             );
  151.  
  152.         //it exists!
  153.         if ($iWhereDoesImgElementStart!==false){
  154.             $strContainsTheImgAddress =
  155.                 substr(
  156.                     $pSrcCode,
  157.                     $iWhereDoesImgElementStart
  158.                     +
  159.                     strlen(self::IMAGE_MARK)
  160.                 );
  161.  
  162.             $strImgUrl =
  163.                 substr(
  164.                     $strContainsTheImgAddress,
  165.                     0,
  166.                     strpos(
  167.                         $strContainsTheImgAddress,
  168.                         "\"" //the closing double-quote
  169.                     )
  170.                 );
  171.  
  172.             return self::APOD_URL.$strImgUrl;
  173.         }
  174.     }//identifyImgInSourceCode
  175.  
  176.     /*
  177.      * receives the URL of some "image of the day"
  178.      * e.g. https://apod.nasa.gov/apod/image/2011/C2020M3Orion_CharlesBracken1024.jpg
  179.      * returns a valid file name
  180.      * e.g. "C2020M3Orion_CharlesBracken1024.jpg"
  181.      */
  182.     public function extractFileNameForDownload(
  183.         string $pUrl
  184.     ){
  185.         /*
  186.          * trim(" a b c ") --> "a b c"
  187.          * strlen("abc") ---> 3
  188.          */
  189.         $bCheck = trim(strlen($pUrl))>0;
  190.  
  191.         /*
  192.          * returns the leftmost ocurrence of the 2nd arg on the 1st
  193.          * strpos("ABCDEFBC", "BC") --> 1
  194.          * rightmost sensitive
  195.          * strrpos ("ABCDEFBC", "BC") --> 6
  196.          * strrpos ("ABCDEFBC", "bc") --> false
  197.          * leftmost, insensitive
  198.          * stripos ("ABCDEFBC", "bc") --> 1
  199.          * rightmost, insensitive
  200.          * strripos ("ABCB", "b") --> 3
  201.          */
  202.         if ($bCheck){
  203.             $iWhereDoesTheLastFwSlashBeginOrFalse =
  204.                 strripos(
  205.                     $pUrl,
  206.                     "/"
  207.                 );
  208.  
  209.             //if the forward slash occurs in the URL
  210.             if ($iWhereDoesTheLastFwSlashBeginOrFalse!==false){
  211.                 $strFileName =
  212.                     substr(
  213.                         $pUrl,
  214.                         $iWhereDoesTheLastFwSlashBeginOrFalse+1
  215.                     );
  216.  
  217.                 return $strFileName;
  218.             }//if
  219.         }//if
  220.         return false;
  221.     }//extractFileNameForDownload
  222.  
  223.     public function downloadImgForDay(
  224.         $pY=0,
  225.         $pM=0,
  226.         $pD=0
  227.     ){
  228.         $bDownloadTodaysImg = empty($pY) && empty($pM) && empty($pD);
  229.  
  230.         //e.g. https://apod.nasa.gov/apod/ap201110.html
  231.         $strUrlForTheHtmlPageThatPublishedTheImg = "";
  232.         if ($bDownloadTodaysImg){
  233.             $strUrlForTheHtmlPageThatPublishedTheImg =
  234.                 $this->urlForDay();
  235.         }//if
  236.         else{
  237.             $strUrlForTheHtmlPageThatPublishedTheImg =
  238.                 $this->urlForDay($pY, $pM, $pD);
  239.         }//else
  240.  
  241.         //source code of the html page
  242.         $strHtmlSourceCode = AmUtil::consumeUrl($strUrlForTheHtmlPageThatPublishedTheImg);
  243.  
  244.         //this is the image direct URL
  245.         $strUrlForTheImg = $this->identifyImgInSourceCode(
  246.             $strHtmlSourceCode
  247.         );
  248.  
  249.         //string/bytes array which is the published image
  250.         //this is the download
  251.         $bytesForTheImg = AmUtil::consumeUrl(
  252.             $strUrlForTheImg
  253.         );
  254.  
  255.         $strOriginalFilename =
  256.             $this->extractFileNameForDownload($strUrlForTheImg);
  257.  
  258.         $iBytesWrittenOrFalse =
  259.             file_put_contents(
  260.                 //"saved.jpg",
  261.                 $strOriginalFilename,
  262.                 $bytesForTheImg
  263.             );
  264.  
  265.         //return $iBytesWrittenOrFalse;
  266.         return [
  267.             self::KEY_SIZE =>  $iBytesWrittenOrFalse,
  268.             self::KEY_FILE_NAME => $strOriginalFilename
  269.         ];
  270.     }//downloadImgForDay
  271.     const KEY_SIZE = "KEY_SIZE";
  272.     const KEY_FILE_NAME = "KEY_FILE_NAME";
  273.  
  274.     public function downloadAllImagesForMonth(
  275.         $pY,
  276.         $pM,
  277.         $pStartDay = 1,
  278.         $pbFeedback = true
  279.     ){
  280.         for (
  281.             $iCurrentDay = $pStartDay;
  282.             $iCurrentDay<=AmUtil::numberOfDaysInMonth($pY, $pM);
  283.             $iCurrentDay++
  284.         ){
  285.             $oRet = $this->downloadImgForDay($pY, $pM, $iCurrentDay);
  286.             $iImageSizeInBytes = $oRet[self::KEY_SIZE];
  287.             $strFileName = $oRet[self::KEY_FILE_NAME];
  288.  
  289.             if ($pbFeedback){
  290.                 $strMsg = sprintf(
  291.                     "Current date=%d-%d-%d\nSave %d bytes in file: %s".PHP_EOL,
  292.                     $pY, $pM, $iCurrentDay,
  293.                     $iImageSizeInBytes,
  294.                     $strFileName
  295.                 );
  296.                 echo $strMsg;
  297.             }//if feedback
  298.  
  299.         }
  300.     }//downloadAllImagesForMonth
  301.  
  302.     //**
  303.     //June 16 1995
  304.     public function ripThemAll(){
  305.         $this->downloadAllImagesForMonth
  306.         (1996, 1, 16);
  307.  
  308.         for(
  309.             $iYear=1997;
  310.             $iYear<=2019;
  311.             $iYear++
  312.         ){
  313.             for ($iMonth=1; $iMonth<=12; $iMonth++)
  314.                 $this->downloadAllImagesForMonth($iYear, $iMonth);
  315.         }//for
  316.  
  317.         //TODO: current year
  318.     }//ripThemAll
  319. }//ApodBot
  320.  
  321. $bot = new ApodBot(); //constructor
  322.  
  323. /*
  324. //$bot->getImageOfTheDay(); //image of the day (today)
  325. $strUrl = $bot->urlForDay();
  326. echo $strUrl.PHP_EOL;
  327.  
  328. $strUrl = $bot->urlForDay(2019, 12, 25);
  329. echo $strUrl.PHP_EOL;
  330.  
  331. //$sourceCode = ApodBot::consumeUrl(TEST_URL);
  332. $sourceCode = AmUtil::consumeUrl($bot->urlForDay());
  333. echo $sourceCode;
  334.  
  335. $urlForTodaysImg = $bot->identifyImgInSourceCode(
  336.     $sourceCode
  337. );
  338.  
  339. echo $urlForTodaysImg.PHP_EOL;
  340.  
  341. /*
  342. $bot->getImageOfTheDay(2019, 12, 25);//image on that date
  343. $bot->getEntireCollection();
  344. $bot->getAllImagemFromYear(2000);
  345. $bot->getAllImagemFromMonth(2000, 4);
  346. */
  347. //echo $bot->downloadImgForDay(2015, 12, 23);
  348.  
  349. //$bot->downloadAllImagesForMonth(1999, 1);
  350. //single threaded
  351.  
  352. //$bot->ripThemAll();
  353.  
  354. while(1){
  355.     /*
  356.     $pedido = obterPedido();
  357.     reagirPedido();
  358.     */
  359.     $bot->downloadImgForDay();
  360.     echo "Will continue in 24 hours time".PHP_EOL;
  361.     sleep(60*60*24); //1 day pause
  362. }//while
  363.  
  364.  
  365. **
  366.  
  367. <?php
  368.  
  369. class AmUtil{
  370.     const IMPOSSIBLE_MONTH = -1;
  371.     const BOT_SIGNATURE = "For educational tests only";
  372.  
  373.     public static function leapYear(
  374.         $pY
  375.     ){
  376.         return ($pY%400 === 0) || ($pY%4===0 && ($pY%100!==0));
  377.     }//leapYear
  378.  
  379.     public static function numberOfDaysInMonth(
  380.         $pY,
  381.         $pM
  382.     ){
  383.         switch($pM){
  384.             case 1: case 3:case 5:case 7:case 8: case 10;case 12: return 31;
  385.             case 4: case 6:case 9:case 11: return 30;
  386.             case 2: return (self::leapYear($pY) ? 29 :  28);
  387.             default: return self::IMPOSSIBLE_MONTH;
  388.         }//switch
  389.     }//numberOfDaysInMonth
  390.  
  391.     public static function consumeUrl(
  392.         $pUrl //can be an HTML page, can be a JPG, ...
  393.     ){
  394.         //$bValid = is_string($pUrl) && strlen($pUrl);
  395.         $ch = curl_init($pUrl);
  396.         if ($ch){
  397.             //curl_setopt(CURLOPT_URL, $pUrl);
  398.             /*
  399.              * makes it explic that the request
  400.              * will happen using HTTP GET
  401.              */
  402.             curl_setopt(
  403.                 $ch,
  404.                 CURLOPT_HTTPGET,
  405.                 true
  406.             );
  407.  
  408.             /*
  409.              * disables the verification of SSL
  410.              * certificates
  411.              * useful when not using cacert.pem
  412.              */
  413.             curl_setopt(
  414.                 $ch,
  415.                 CURLOPT_SSL_VERIFYPEER,
  416.                 true
  417.             );
  418.  
  419.             /*
  420.              * sets a user agent string for our
  421.              * software
  422.              */
  423.             curl_setopt(
  424.                 $ch,
  425.                 CURLOPT_USERAGENT,
  426.                 self::BOT_SIGNATURE
  427.             );
  428.  
  429.             //if set to true, curl_exec will return
  430.             //the data consumed at the URL
  431.             //instead of just true/false
  432.             curl_setopt(
  433.                 $ch,
  434.                 CURLOPT_RETURNTRANSFER,
  435.                 true
  436.             );
  437.  
  438.             /*
  439.              * makes it clear that we want all the bytes
  440.              */
  441.             curl_setopt(
  442.                 $ch,
  443.                 CURLOPT_BINARYTRANSFER, //deprecated
  444.                 true
  445.             );
  446.  
  447.             /*
  448.              * sets automatic handling of the encoded
  449.              * data
  450.              */
  451.             curl_setopt(
  452.                 $ch,
  453.                 CURLOPT_ENCODING,
  454.                 ""
  455.             );
  456.  
  457.             $bin = curl_exec($ch);
  458.  
  459.             return $bin;
  460.         }//if
  461.         return false;
  462.     }//consumeUrl
  463. }//AmUtil
Advertisement
Add Comment
Please, Sign In to add comment