Pman/Reddit/DataObjects/Reddit_livefeed.php
[web.Reddit] / Pman / Reddit / DataObjects / Reddit_livefeed.php
1 <?php
2 /**
3  * Table Definition for cash_costing_map
4  */
5 require_once 'DB/DataObject.php';
6
7
8 class Pman_Reddit_DataObjects_Reddit_livefeed extends DB_DataObject 
9 {
10     ###START_AUTOCODE
11     /* the code below is auto generated do not remove the above tag */
12
13     public $__table = 'reddit_livefeed';                // table name
14     public $id;                              // int(11)  not_null primary_key auto_increment
15     public $body;                      // string(254)  not_null
16     public $uid;                      // int(11)  not_null
17     public $created_dt;                      // int(11)  not_null
18     public $embeds;                    // int(11)  not_null
19     public $body_html;                      // int(11)  not_null
20     public $prev_uid;
21     
22     
23     /* the code above is auto generated do not remove the tag below */
24     ###END_AUTOCODE
25     
26     function fetchFrom($from='')
27     {
28         
29         $base = "https://www.reddit.com/live/tnc30xhiiqom.json";
30         $after =  strlen($from) ? ('after=LiveUpdate_' . $from) : '';
31         $count = strlen($from) ? 'count=75' : '';
32         $url = $base . '?' . implode('&', array($after, $count));
33         
34         echo "$url\n";
35         
36         $raw = file_get_contents($url);
37         
38         $data = json_decode($raw);
39         if (!count($data->data->children)) {
40             var_dump($raw);
41             return '';
42         }
43         echo  date('Y-m-d H:i:s', $data->data->children[0]->data->created) . "\n";
44 /*
45 kind
46 data => 
47    modhash
48    children 
49    after
50    before
51
52 --> children:
53   kind:
54   data:
55      body:
56      name:
57      created:
58      embeds: array(url / width / height)
59      author
60      created_utc
61      body_html
62      stricken
63      id
64      */
65         $added = 0;
66         $last = $from;
67         foreach($data->data->children as $child) {
68             $ch = $child->data;
69             $ch->uid = $ch->id;
70             $ch->body_html = html_entity_decode($ch->body_html);
71             $ch->prev_uid = $last;
72             $ch->created_dt = date('Y-m-d H:i:s', $ch->created);
73             $ch->embeds = json_encode($ch->embeds);
74             $ch = (array) $ch;
75             //print_r($ch);exit;
76             
77             unset($ch['id']);
78             $x = DB_DataObject::factory('reddit_livefeed');
79             if ($x->get('uid', $ch['uid'])) {
80                 $last = $ch['uid'];
81                 continue;
82             }
83             $x->setFrom($ch);
84             $x->insert();
85             $last = $x->uid;
86             $added++;
87             
88         }
89         
90         return $last;
91         
92         // what next....
93         // if we did not add any....??
94         // look for the oldest???
95         
96      
97          
98         
99     }
100       function height()
101     {
102         $r = json_decode($this->embeds);
103         return $r[0]->height;
104     }
105     function width()
106     {
107         $r = json_decode($this->embeds);
108         return $r[0]->width;
109     
110     }
111     function url()
112     {
113         $r = json_decode($this->embeds);
114         return $r[0]->url;
115     }
116     
117     function fetchEmbed()
118     {
119         /*
120         $r = json_decode($this->embeds);
121         // output to
122         foreach($r as $i=>$em) {
123         //$file =  /home/reddit/ date('Y/m/d', strtotime($this->created_dt . " - 8 HOURS")) . "/" . $this->uid . '.' . $i . ".pdf"
124             if (file_exist)($file) {
125                 continue
126             }
127             $dir = dirname($file);
128             if (!file_exist($dir)) {
129                 mkdir($dir,660, true);
130             }
131             $url = http://redit....... 
132             $cmd = "/usr/bin/wkhtmltopdf $url $file";
133         }
134         */
135         exit;
136         
137         
138         
139         
140     }
141     
142      
143 }