phpDocumentor deviantartfeed
[ class tree: deviantartfeed ] [ index: deviantartfeed ] [ all elements ]

Source for file deviantart.php

Documentation is available at deviantart.php

  1. <?php
  2.  
  3. /**
  4.  * Scrapes a recent deviations feed together from deviantart.
  5.  *
  6.  * Uses a json formatted string from deviantart which works currently, however
  7.  * any changes will most likley break this. Hopefully only the xsl would need to
  8.  * be updated in such an event.
  9.  *
  10.  * GENERAL DISCLAIMER
  11.  *
  12.  * I am not personally affiliated with DeviantART in any way. Use of the
  13.  * DeviantART website by its Terms Of Service. I am not responsible for the way
  14.  * you use this script. The use of this script to scrape your DeviantART user
  15.  * gallery may be in breach of your agreement with DeviantART.
  16.  *
  17.  * LICENSE
  18.  *
  19.  * This program is free software: you can redistribute it and/or modify it under
  20.  * the terms of the GNU General Public License as published by the Free Software
  21.  * Foundation, either version 3 of the License, or (at your option) any later
  22.  * version.
  23.  *
  24.  * This program is distributed in the hope that it will be useful, but WITHOUT
  25.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  26.  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  27.  * details.
  28.  *
  29.  * You should have received a copy of the GNU General Public License along with
  30.  * this program. If not, see <http://www.gnu.org/licenses/>.
  31.  *
  32.  * @author Stephen Ingram <code@jixor.com>
  33.  * @copyright Copyright (c) 2008, Stephen Ingram
  34.  * @package deviantartfeed
  35.  * @category deviantartfeed
  36.  * @todo The title and date regex is quick and dirty, it could easily be tripped
  37.  *        up.
  38.  * @todo Investigate further what sort of output the DeviantART "DiFi" script
  39.  *        will generate.
  40.  */
  41. {
  42.  
  43.     /**
  44.      * Location of config file
  45.      *
  46.      * If you move or rename the deviantart.ini file you must edit this path.
  47.      *
  48.      * @var string 
  49.      */
  50.     const config_file 'deviantart.ini';
  51.  
  52.     /**
  53.      * Holds loaded config array
  54.      *
  55.      * @var array 
  56.      */
  57.     public $conf;
  58.  
  59.     /**
  60.      * Filesystem path to cache file.
  61.      *
  62.      * @var string 
  63.      */
  64.     public $cache;
  65.  
  66.  
  67.  
  68.     /**
  69.      * Constructor does all the work
  70.      */
  71.     public function __construct()
  72.     {
  73.  
  74.         $this->conf = parse_ini_file(self::config_filetrue);
  75.  
  76.         $this->cache = $this->conf['cache']['path']
  77.             . $this->conf['cache']['file'];
  78.  
  79.         if (file_exists($this->cache)
  80.             && filemtime($this->cachetime(86400)
  81.             return $this->output_cache();
  82.  
  83.         /**
  84.          * Set url, broken into multiple lines for readability. Well...
  85.          */
  86.         $url "http://{$this->conf[feed][user]}artist.deviantart.com/global/difi/"
  87.             . "?c%5B%5D=Resources;htmlFromQuery;gallery%3A{$this->conf[feed][user]}"
  88.             . "%20sort%3Atime,{$this->conf[feed][start]},{$this->conf[feed][request]}"
  89.             . ",thumb150,artist%3A0&t=json";
  90.  
  91.         /**
  92.          * Spoof user agent, not really necessary.
  93.          */
  94.         ini_set(
  95.             'user_agent',
  96.             'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1)'
  97.             );
  98.  
  99.         /**
  100.          * Get json string
  101.          */
  102.         if (!$result file_get_contents($url))
  103.             return;
  104.  
  105.         /**
  106.          * Decode json string
  107.          */
  108.         $result_object json_decode($result);
  109.  
  110.         /**
  111.          * Define xml file, includes attributes on channel that are used by the
  112.          * php function calls.
  113.          */
  114.         $xml "<channel
  115.             imageurl=\"{$this->conf[imagecache][url]}\"
  116.             imagecache=\"{$this->conf[imagecache][path]}\"
  117.             >
  118.             <title>{$this->conf[feed][user]}'s deviantART gallery</title>
  119.             <link>http://{$this->conf[feed][user]}.deviantart.com</link>";
  120.  
  121.         /**
  122.          * Add any additional channel elements as defined in ini
  123.          */
  124.         if (isset($this->conf['channel'])
  125.             && !empty($this->conf['channel'])
  126.             )
  127.             foreach ($this->conf['channel'as $name => $value)
  128.                 $xml .= "<{$name}>{$value}</{$name}>";
  129.  
  130.         $xml .= "<items>";
  131.  
  132.         /**
  133.          * Add xml entry for each item in the response object.
  134.          */
  135.         foreach($result_object->DiFi->response->calls[0]->response->content->resources as $item)
  136.             $xml .= '<item>' $item[2'</item>';
  137.  
  138.         /**
  139.          * Initialize the dom object and load the xml file from the string.
  140.          */
  141.         $dom new DOMDocument;
  142.         $dom->loadXML($xml '</items></channel>');
  143.  
  144.         /**
  145.          * Initialize the xsl dom object and load the xsl file form the
  146.          * filesystem.
  147.          */
  148.         $xsl new DOMDocument;
  149.         $xsl->load($this->conf['xsl']['path']);
  150.  
  151.         /**
  152.          * Initialize the XSLT processor and enable php function calls
  153.          */
  154.         $proc new XSLTProcessor();
  155.         $proc->registerPHPFunctions();
  156.  
  157.         /**
  158.          * Import the xsl object
  159.          */
  160.         $proc->importStyleSheet($xsl);
  161.  
  162.         /**
  163.          * Process the xml translation and save to the cache file.
  164.          */
  165.         file_put_contents($this->cache$proc->transformToXML($dom));
  166.  
  167.         /**
  168.          * Call deviantart::output_cahce() to send the rss feed to the user,
  169.          * if enabled
  170.          */
  171.         return $this->output_cache();
  172.  
  173.     }
  174.  
  175.  
  176.  
  177.     /**
  178.      * Return or send cache depenging on setting
  179.      *
  180.      * If not in cron mode sends the contents of the cache file to the user,
  181.      * otherwise simply returns void.
  182.      *
  183.      * @return void 
  184.      */
  185.     public function output_cache()
  186.     {
  187.  
  188.         if($this->conf['general']['cron'])
  189.             return;
  190.  
  191. /* or redirect to the feed.
  192.         header('Location: '
  193.             . $this->conf['general']['url']
  194.             . $this->conf['cache']['url']
  195.             . $this->conf['cache']['file']
  196.             );
  197. */
  198.  
  199.         if (strpos($_SERVER['HTTP_USER_AGENT']'Mozilla')) {
  200.             header('Content-type: text/xml');
  201.         else {
  202.             header('Content-Type: application/rss+xml');
  203.         }
  204.  
  205.         header('Last-Modified: ' date('r'filemtime($this->cache)));
  206.  
  207.         echo file_get_contents($this->cache);
  208.  
  209.         return;
  210.  
  211.     }
  212.  
  213.  
  214.  
  215.     /**
  216.      * Downloads a local cache of specified file.
  217.      *
  218.      * @param string $url 
  219.      * @param string $base URL base to apply to output URL
  220.      * @param string $path Path base for local filesystem path.
  221.      * @return string URL to local cache.
  222.      */
  223.     public static function xsl_make_local_cache($url$base ''$path '')
  224.     {
  225.  
  226.         ini_set(
  227.             'user_agent',
  228.             'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/Firefox/3.0.1'
  229.             );
  230.  
  231.         /**
  232.          * Get file name
  233.          */
  234.         $file substr($urlstrrpos($url'/'1);
  235.  
  236.         if (file_exists($path $file))
  237.             return $base $file;
  238.  
  239.         file_put_contents($path $filefile_get_contents($url));
  240.  
  241.         return $base $file;
  242.  
  243.     }
  244.  
  245.  
  246.  
  247.     /**
  248.      * Gets the artwork title from the proviced string
  249.      *
  250.      * @param string $string Vale of title attribute from input xml
  251.      * @return string Artwork title
  252.      */
  253.     public static function xsl_get_title($string)
  254.     {
  255.  
  256.         preg_match(
  257.             '/(.*?) by/',
  258.             $string,
  259.             $title
  260.             );
  261.  
  262.         return $title[1];
  263.  
  264.     }
  265.  
  266.  
  267.  
  268.     /**
  269.      * Gets the artwork date in RFC 2822 format from the proviced string
  270.      *
  271.      * I'm assuming that deviantart records dates in UTC time, however I could
  272.      * be wrong.
  273.      *
  274.      * @param string $string Vale of title attribute from input xml
  275.      * @return string Artwork date
  276.      */
  277.     public static function xsl_get_date($string)
  278.     {
  279.  
  280.         /**
  281.          * I'm assuming that you can't have a , in your artork title.
  282.          */
  283.         preg_match(
  284.             '/, (.*)/',
  285.             $string,
  286.             $date
  287.             );
  288.  
  289.         date_default_timezone_set('UTC');
  290.  
  291.         return date('r'strtotime($date[1]));
  292.  
  293.     }
  294.  
  295. }
  296.  

Documentation generated on Thu, 04 Sep 2008 02:21:51 +1000 by phpDocumentor 1.4.1