upload changes
[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 convertEmbed(){
118         $r = json_decode($this->embeds);
119         DB_DataObject::debugLevel(1);
120         foreach($r as $i=>$em) {
121             echo $i . "\n\n\n";
122             $x = DB_DataObject::factory('reddit_livefeed_embed');
123             $x->feed_id = $this->id;
124             $x->seqid = $i;
125             if ($x->count()) {
126                 continue;
127             }
128             $x->feed_id = $this->id;
129             $x->seqid = $i;
130             $x->url = $em->url;
131             $x->insert();
132         }
133     }
134     
135     function fetchEmbed()
136     {
137         /*
138         $r = json_decode($this->embeds);
139         // output to
140         foreach($r as $i=>$em) {
141         //$file =  /home/reddit/ date('Y/m/d', strtotime($this->created_dt . " - 8 HOURS")) . "/" . $this->uid . '.' . $i . ".pdf"
142             if (file_exist)($file) {
143                 continue
144             }
145             $dir = dirname($file);
146             if (!file_exist($dir)) {
147                 mkdir($dir,660, true);
148             }
149             $url = http://redit....... 
150             $cmd = "/usr/bin/wkhtmltopdf $url $file";
151         }
152         */
153         
154         $r = json_decode($this->embeds);
155         foreach($r as $i=>$em) {
156             $file = '/home/reddit/' . date('Y/m/d',strtotime($this->created_dt . " - 8 HOURS")) . "/" . $this->uid . '.' . $i . ".pdf";
157             if(file_exists($file)){
158                 echo "file exists already.";
159                 continue;
160             }
161             $dir = dirname($file);
162             if(!file_exists($dir)){
163                 echo $dir . "\n";
164                 mkdir($dir,0775,true);
165             }
166             $url='http://roojs.com/reddit/index.php/Embedly?url=' . $em->url;
167             echo $em->url;
168             //escapeshellarg('http://www.redditmedia.com/mediaembed/liveupdate/tnc30xhiiqom/LiveUpdate_' . $this->uid . '/' . $i);
169             //$cmd = "/usr/bin/xvfb-run /usr/local/bin/wkhtmltopdf --javascript-delay 10000 --load-error-handling ignore '" . $url . "' " . $file;
170             
171             $cmd = "seed main.js -u  " . $url . " -d 5000 -f " . $file;
172             echo $cmd;
173             echo `$cmd`;
174             sleep(1);
175         }
176         
177         
178     }
179     
180      
181 }