Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- include => warning
- include "fich";
- include_once "caminho/fich";
- require => error
- require "fich/sdsd/sdsD/f";
- */
- require_once "AmUtil.php";
- //https://codeshare.luismeneses.pt/
- //https://curl.haxx.se/docs/sslcerts.html
- //https://curl.se/docs/caextract.html
- //edit php.ini and add
- //curl.cainfo="C:/<path to my php installation folder>/cacert.pem"
- define ("TEST_URL", "https://apod.nasa.gov/apod/ap141231.html");
- // ... "<IMG SRC="image/2011/M78... "
- //https://apod.nasa.gov/apod/image/2011/M78_LDN1622_BarnardsLoop_SEP27_28_Oct15_final1024.jpg
- class ApodBot{
- //consts?
- const APOD_URL = "https://apod.nasa.gov/apod/";
- const DESIRED_NUMBER_OF_DIGITS = 2;
- //const BOT_SIGNATURE = "For educational tests only";
- /*
- * the structure of an APOD URL:
- * https://apod.nasa.gov/apod/apYYMMDD.html
- * e.g.: https://apod.nasa.gov/apod/ap141231.html
- */
- private
- $mCurrentYear,
- $mCurrentMonth,
- $mCurrentDay;
- public function __construct(){
- /*
- * Y - format for year with 4 digits
- * m - format for month with 2 digits
- * d - format for day with 2 digits
- */
- $strYMD = date("Y-m-d"); //"2020-11-05"
- $aDateParts = explode("-", $strYMD);
- /*
- * $aDateParts = [
- * 0 => "2020", 1 => "11", 2 => "05"
- * ];
- */
- $this->mCurrentYear =
- intval($aDateParts[0]);
- $this->mCurrentMonth =
- intval($aDateParts[1]);
- $this->mCurrentDay =
- intval($aDateParts[2]);
- }//__construct
- //data members?
- //methods?
- //https://apod.nasa.gov/apod/ap141231.html
- /*
- * receives 3 ints: year, month, day
- * returns a string in the format
- * https://apod.nasa.gov/apod/apYYMMDD.html
- */
- public function urlForDay(
- int $pYear=0, //2020
- int $pMonth=0, //12->"12" 5->"05"
- int $pDay=0 //31->"31" 1->"01"
- ){
- $strUrlForDay = "";
- $strYear=$strMonth=$strDay="";
- //pattern for default values for parameters
- $pYear = empty($pYear) ?
- $this->mCurrentYear
- :
- $pYear;
- $pMonth =
- empty($pMonth) ?
- $this->mCurrentMonth
- :
- $pMonth;
- $pDay =
- empty($pDay) ?
- $this->mCurrentDay
- :
- $pDay;
- /*
- 293029 (6 excede 2 = 6-2=4)
- 9999 (4 excede 2 em 4-2=2)
- 999 (3 excede 2 em 3-2=1)
- */
- $strYear = $pYear . "";
- if (
- $iSizeOfYear=strlen($strYear)
- >
- self::DESIRED_NUMBER_OF_DIGITS
- ){
- $iExcessDigits =
- $iSizeOfYear
- -
- self::DESIRED_NUMBER_OF_DIGITS;
- $strYear = substr(
- $strYear,
- $iExcessDigits-1, //start position
- self::DESIRED_NUMBER_OF_DIGITS //2 symbols
- );
- }//if there is need to fix the string for year
- $strMonth = $pMonth<10 ? "0".$pMonth : $pMonth."";
- $strDay = $pDay<10 ? "0".$pDay : $pDay."";
- /*
- * sprintf is "print into a string following a format"
- * https://apod.nasa.gov/apod/apYYMMDD.html
- */
- $strUrlForDay = sprintf(
- "%sap%s%s%s.html",
- self::APOD_URL, //"https://apod.nasa.gov/apod/"
- $strYear,
- $strMonth,
- $strDay
- );
- return $strUrlForDay;
- }//urlForDay
- const IMAGE_MARK = "<img src=\"";
- public function identifyImgInSourceCode(
- $pSrcCode
- ){
- $iWhereDoesImgElementStart =
- stripos(
- $pSrcCode,
- self::IMAGE_MARK
- );
- //it exists!
- if ($iWhereDoesImgElementStart!==false){
- $strContainsTheImgAddress =
- substr(
- $pSrcCode,
- $iWhereDoesImgElementStart
- +
- strlen(self::IMAGE_MARK)
- );
- $strImgUrl =
- substr(
- $strContainsTheImgAddress,
- 0,
- strpos(
- $strContainsTheImgAddress,
- "\"" //the closing double-quote
- )
- );
- return self::APOD_URL.$strImgUrl;
- }
- }//identifyImgInSourceCode
- public function downloadImgForDay(
- $pY=0,
- $pM=0,
- $pD=0
- ){
- $bDownloadTodaysImg = empty($pY) && empty($pM) && empty($pD);
- //e.g. https://apod.nasa.gov/apod/ap201110.html
- $strUrlForTheHtmlPageThatPublishedTheImg = "";
- if ($bDownloadTodaysImg){
- $strUrlForTheHtmlPageThatPublishedTheImg =
- $this->urlForDay();
- }//if
- else{
- $strUrlForTheHtmlPageThatPublishedTheImg =
- $this->urlForDay($pY, $pM, $pD);
- }//else
- //source code of the html page
- $strHtmlSourceCode = AmUtil::consumeUrl($strUrlForTheHtmlPageThatPublishedTheImg);
- //this is the image direct URL
- $strUrlForTheImg = $this->identifyImgInSourceCode(
- $strHtmlSourceCode
- );
- //string/bytes array which is the published image
- //this is the download
- $bytesForTheImg = AmUtil::consumeUrl(
- $strUrlForTheImg
- );
- $iBytesWrittenOrFalse =
- file_put_contents(
- "saved.jpg",
- $bytesForTheImg
- );
- return $iBytesWrittenOrFalse;
- }//downloadImgForDay
- }//ApodBot
- $bot = new ApodBot(); //constructor
- //$bot->getImageOfTheDay(); //image of the day (today)
- $strUrl = $bot->urlForDay();
- echo $strUrl.PHP_EOL;
- $strUrl = $bot->urlForDay(2019, 12, 25);
- echo $strUrl.PHP_EOL;
- //$sourceCode = ApodBot::consumeUrl(TEST_URL);
- $sourceCode = AmUtil::consumeUrl($bot->urlForDay());
- echo $sourceCode;
- $urlForTodaysImg = $bot->identifyImgInSourceCode(
- $sourceCode
- );
- echo $urlForTodaysImg.PHP_EOL;
- /*
- $bot->getImageOfTheDay(2019, 12, 25);//image on that date
- $bot->getEntireCollection();
- $bot->getAllImagemFromYear(2000);
- $bot->getAllImagemFromMonth(2000, 4);
- */
- echo $bot->downloadImgForDay();
Advertisement
Add Comment
Please, Sign In to add comment