am_dot_com

ACA 20201110

Nov 10th, 2020 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.40 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.     public function downloadImgForDay(
  177.         $pY=0,
  178.         $pM=0,
  179.         $pD=0
  180.     ){
  181.         $bDownloadTodaysImg = empty($pY) && empty($pM) && empty($pD);
  182.  
  183.         //e.g. https://apod.nasa.gov/apod/ap201110.html
  184.         $strUrlForTheHtmlPageThatPublishedTheImg = "";
  185.         if ($bDownloadTodaysImg){
  186.             $strUrlForTheHtmlPageThatPublishedTheImg =
  187.                 $this->urlForDay();
  188.         }//if
  189.         else{
  190.             $strUrlForTheHtmlPageThatPublishedTheImg =
  191.                 $this->urlForDay($pY, $pM, $pD);
  192.         }//else
  193.  
  194.         //source code of the html page
  195.         $strHtmlSourceCode = AmUtil::consumeUrl($strUrlForTheHtmlPageThatPublishedTheImg);
  196.  
  197.         //this is the image direct URL
  198.         $strUrlForTheImg = $this->identifyImgInSourceCode(
  199.             $strHtmlSourceCode
  200.         );
  201.  
  202.         //string/bytes array which is the published image
  203.         //this is the download
  204.         $bytesForTheImg = AmUtil::consumeUrl(
  205.             $strUrlForTheImg
  206.         );
  207.  
  208.         $iBytesWrittenOrFalse =
  209.             file_put_contents(
  210.                 "saved.jpg",
  211.                 $bytesForTheImg
  212.             );
  213.  
  214.         return $iBytesWrittenOrFalse;
  215.     }//downloadImgForDay
  216. }//ApodBot
  217.  
  218. $bot = new ApodBot(); //constructor
  219. //$bot->getImageOfTheDay(); //image of the day (today)
  220. $strUrl = $bot->urlForDay();
  221. echo $strUrl.PHP_EOL;
  222.  
  223. $strUrl = $bot->urlForDay(2019, 12, 25);
  224. echo $strUrl.PHP_EOL;
  225.  
  226. //$sourceCode = ApodBot::consumeUrl(TEST_URL);
  227. $sourceCode = AmUtil::consumeUrl($bot->urlForDay());
  228. echo $sourceCode;
  229.  
  230. $urlForTodaysImg = $bot->identifyImgInSourceCode(
  231.     $sourceCode
  232. );
  233.  
  234. echo $urlForTodaysImg.PHP_EOL;
  235.  
  236. /*
  237. $bot->getImageOfTheDay(2019, 12, 25);//image on that date
  238. $bot->getEntireCollection();
  239. $bot->getAllImagemFromYear(2000);
  240. $bot->getAllImagemFromMonth(2000, 4);
  241. */
  242. echo $bot->downloadImgForDay();
Advertisement
Add Comment
Please, Sign In to add comment