Pman/Reddit
authorAlan Knowles <alan@roojs.com>
Mon, 8 Dec 2014 05:27:51 +0000 (13:27 +0800)
committerAlan Knowles <alan@roojs.com>
Mon, 8 Dec 2014 05:27:51 +0000 (13:27 +0800)
Pman/Reddit/DataObjects/Reddit_livefeed.php [new file with mode: 0644]
Pman/Reddit/DataObjects/reddit.sql [new file with mode: 0644]
Pman/Reddit/Day.php [new file with mode: 0644]
Pman/Reddit/Import.php [new file with mode: 0644]
Pman/Reddit/templates/reddit.html [new file with mode: 0644]

diff --git a/Pman/Reddit/DataObjects/Reddit_livefeed.php b/Pman/Reddit/DataObjects/Reddit_livefeed.php
new file mode 100644 (file)
index 0000000..70997a2
--- /dev/null
@@ -0,0 +1,117 @@
+<?php
+/**
+ * Table Definition for cash_costing_map
+ */
+require_once 'DB/DataObject.php';
+
+
+class Pman_Reddit_DataObjects_Reddit_livefeed extends DB_DataObject 
+{
+    ###START_AUTOCODE
+    /* the code below is auto generated do not remove the above tag */
+
+    public $__table = 'reddit_livefeed';                // table name
+    public $id;                              // int(11)  not_null primary_key auto_increment
+    public $body;                      // string(254)  not_null
+    public $uid;                      // int(11)  not_null
+    public $created_dt;                      // int(11)  not_null
+    public $embeds;                    // int(11)  not_null
+    public $body_html;                      // int(11)  not_null
+    public $prev_uid;
+    
+    
+    /* the code above is auto generated do not remove the tag below */
+    ###END_AUTOCODE
+    
+    function fetchFrom($from='')
+    {
+        
+        $base = "https://www.reddit.com/live/tnc30xhiiqom.json";
+        $after =  strlen($from) ? ('after=LiveUpdate_' . $from) : '';
+        $count = strlen($from) ? 'count=75' : '';
+        $url = $base . '?' . implode('&', array($after, $count));
+        
+        echo "$url\n";
+        
+        $raw = file_get_contents($url);
+        
+        $data = json_decode($raw);
+        if (!count($data->data->children)) {
+            var_dump($raw);
+            return '';
+        }
+        echo  date('Y-m-d H:i:s', $data->data->children[0]->data->created) . "\n";
+/*
+kind
+data => 
+   modhash
+   children 
+   after
+   before
+
+--> children:
+  kind:
+  data:
+     body:
+     name:
+     created:
+     embeds: array(url / width / height)
+     author
+     created_utc
+     body_html
+     stricken
+     id
+     */
+        $added = 0;
+        $last = $from;
+        foreach($data->data->children as $child) {
+            $ch = $child->data;
+            $ch->uid = $ch->id;
+            $ch->body_html = html_entity_decode($ch->body_html);
+            $ch->prev_uid = $last;
+            $ch->created_dt = date('Y-m-d H:i:s', $ch->created);
+            $ch->embeds = json_encode($ch->embeds);
+            $ch = (array) $ch;
+            //print_r($ch);exit;
+            
+            unset($ch['id']);
+            $x = DB_DataObject::factory('reddit_livefeed');
+            if ($x->get('uid', $ch['uid'])) {
+                $last = $ch['uid'];
+                continue;
+            }
+            $x->setFrom($ch);
+            $x->insert();
+            $last = $x->uid;
+            $added++;
+            
+        }
+        
+        return $last;
+        
+        // what next....
+        // if we did not add any....??
+        // look for the oldest???
+        
+     
+         
+        
+    }
+      function height()
+    {
+        $r = json_decode($this->embeds);
+        return $r[0]->height;
+    }
+    function width()
+    {
+        $r = json_decode($this->embeds);
+        return $r[0]->width;
+    
+    }
+    function url()
+    {
+        $r = json_decode($this->embeds);
+        return $r[0]->url;
+    }
+     
+}
diff --git a/Pman/Reddit/DataObjects/reddit.sql b/Pman/Reddit/DataObjects/reddit.sql
new file mode 100644 (file)
index 0000000..a5365f5
--- /dev/null
@@ -0,0 +1,15 @@
+
+CREATE TABLE reddit_livefeed 
+(
+   id INT(11)  NOT NULL auto_increment,
+   body TEXT,
+  uid varchar(64) default '',
+  created_dt datetime,
+  embeds TEXT,
+  author varchar(64),
+  body_html TEXT,
+  PRIMARY KEY (id)
+
+);
+
+alter table reddit_livefeed add column last_uid varchar(64) not null default '';
\ No newline at end of file
diff --git a/Pman/Reddit/Day.php b/Pman/Reddit/Day.php
new file mode 100644 (file)
index 0000000..8ffab5c
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+require_once 'Pman.php';
+
+class Pman_Reddit_Day extends Pman
+{
+    var $masterTemplate = 'reddit.html';
+    
+    function getAuth()
+    {
+        return true;
+    }
+    
+    function get($day='')
+    {
+        //DB_DataObject::debugLevel(1);
+        
+        $day = empty($day) ? date('Y-m-d') : date('Y-m-d', strtotime($day));
+        $f = DB_DataObject::factory('reddit_livefeed');
+        $f->selectAdd('created_dt - INTERVAL 8 HOUR as created_local_dt');
+        
+        $f->whereAdd("
+                created_dt - INTERVAL 8 HOUR > '{$day} 00:00:00'
+                AND
+                created_dt - INTERVAL 8 HOUR < '{$day} 00:00:00' + INTERVAL 1 DAY                
+        ");
+        
+        $f->orderBy('created_dt ASC');
+        $this->feed = $f->fetchAll();
+        
+        
+    }
+  
+  
+}
diff --git a/Pman/Reddit/Import.php b/Pman/Reddit/Import.php
new file mode 100644 (file)
index 0000000..3d2ab8e
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+
+require_once 'Pman.php';
+
+class Pman_Reddit_Import extends Pman {
+    
+    function getAuth()
+    {
+        $ff = HTML_FlexyFramework::get();
+        if (!$ff->cli) {
+            die("not cli");
+        }
+        return true;
+    }
+    function get($arg = '')
+    {
+        //DB_DataObject::debugLevel(1);
+        $dd = DB_DataObject::factory('reddit_livefeed');
+        $dd->orderBy('created_dt ASC');
+        $dd->limit(1);
+        $dd->find(true);
+        $next = $dd->uid;
+        
+        $dd = DB_DataObject::factory('reddit_livefeed');
+        
+        
+        //$next = '';
+        while (true) {
+            $next = $dd->fetchFrom($next);
+            if(!strlen($next)) {
+                break;
+            }
+            sleep(1);
+        }
+        die('done');
+    }
+    
+}
diff --git a/Pman/Reddit/templates/reddit.html b/Pman/Reddit/templates/reddit.html
new file mode 100644 (file)
index 0000000..ff31375
--- /dev/null
@@ -0,0 +1,39 @@
+<head>
+    
+    <title>[live] Occupy Central Civil Disobedience Movement (Umbrella Revolution)</title>
+    <link rel="stylesheet" type="text/css" href="//www.redditstatic.com/reddit.mLSG3fve1T8.css" media="all">
+    <link rel="stylesheet" type="text/css" href="//www.redditstatic.com/old-markdown.Lcx8i-O12_8.css" media="all">
+    <link rel="stylesheet" type="text/css" href="//www.redditstatic.com/liveupdate.Prtaco0M08M.css" media="all">
+    
+    </head>
+    <body>
+        
+        <ol class="liveupdate-listing">
+                
+            <li flexy:foreach="feed,r" data-fullname="LiveUpdate_{r.uid}"
+                class="liveupdate id-LiveUpdate_{r.uid}">
+                <a href="/live/tnc30xhiiqom/updates/{r.uid}" target="_blank">
+                    <time title="{r.created_local_dt}" datetime="{r.created_local_dt}" class="live-timestamp">{r.created_local_dt}</time>
+                </a>
+                <div class="body">
+                    
+                    {r.body_html:h}
+                    
+                    <div class="md" flexy:if="r.url()">
+                        <iframe class="embedFrame" id="embed-LiveUpdate_{r.uid}"
+                                src="//www.redditmedia.com/mediaembed/liveupdate/tnc30xhiiqom/LiveUpdate_{r.uid}/0"
+                                height="300"
+                                width="800"
+                                scrolling="no"
+                                frameborder="0"></iframe>
+                    </div>
+                    
+                    <br/>
+                    <a href="/user/{r.author}" class="author" data-name="{r.author}">/u/{r.author}</a>
+                </div>
+            </li>
+        </ol>
+        
+
+    </body>
+</html>
\ No newline at end of file