Guest User

timeline.php

a guest
Aug 8th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.74 KB | None | 0 0
  1. <?php
  2.     require_once('TwitterAPIExchange.php');
  3.     require_once('Memory.php');
  4.  
  5.     date_default_timezone_set("UTC");
  6.     error_reporting(E_ALL);
  7.     ini_set('display_errors', 1);
  8.  
  9.     $memory = Memory::getInstance();
  10.     if($memory->active()){
  11.         //$events = $memory->forget('_events');
  12.         $events = $memory->get('_events');
  13.     }
  14.     if(!$events){
  15.         $settings = array(
  16.             'oauth_access_token' => "",
  17.             'oauth_access_token_secret' => "",
  18.             'consumer_key' => "",
  19.             'consumer_secret' => ""
  20.         );
  21.  
  22.         $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
  23.         $getfield = '?screen_name=jagexclock&count=30';
  24.         $requestMethod = 'GET';
  25.  
  26.         $twitter = new TwitterAPIExchange($settings);
  27.         $rawTweets = json_decode($twitter->setGetfield($getfield)
  28.             ->buildOauth($url, $requestMethod)
  29.             ->performRequest());
  30.  
  31.         $events = array();
  32.  
  33.         foreach($rawTweets as $rawTweet){
  34.             $event = new stdClass();
  35.             $districts = array();
  36.  
  37.             $rawTweet->created_at = preg_replace("/\+0000 /","", $rawTweet->created_at);
  38.             $event->time = new DateTime();
  39.             $event->time->setTimestamp(strtotime($rawTweet->created_at));
  40.  
  41.             if(preg_match("/in the ([a-zA-Z]{1,9}) and ([a-zA-Z]{1,9}) districts/", $rawTweet->text, $districts)){
  42.                 $event->text = "Prifddinas: {$districts[1]} and {$districts[2]}";
  43.                 $event->districts = array($districts[1],$districts[2]);
  44.                 $event->type = "prifddinas";
  45.             } else if (preg_match("/Wilderness Warbands/", $rawTweet->text)) {
  46.                 $event->text = "Warbands in 15m";
  47.                 $event->type = "warband";
  48.             } else {
  49.                 continue;
  50.             }
  51.  
  52.             $events[] = $event;
  53.         }
  54.  
  55.         $now = new DateTime();
  56.         $yesterday = new DateTime();
  57.         $yesterday->modify('-1 day');
  58.         $yesterdayMidnight = new DateTime();
  59.  
  60.         $yesterdayMidnight->setTimestamp(strtotime($yesterday->format("d-m-Y ") . "0:00:00"));
  61.  
  62.         //caches
  63.         $catchTime = clone $yesterdayMidnight;
  64.         $catchTime->modify('-5 minutes');
  65.        
  66.         while($catchTime < $now){
  67.             $event = new stdClass();
  68.             $event->type = "cache";
  69.             $event->text = "Guthix Cache in 5m";
  70.             $event->time = clone $catchTime;
  71.  
  72.             $events[] = $event;
  73.  
  74.             $catchTime->modify('+3 hours');
  75.         }
  76.  
  77.         //Goebies
  78.         $goebiesTime = clone $yesterdayMidnight;
  79.         $goebiesTime->modify('-10 minutes');
  80.  
  81.         while($goebiesTime < $now){
  82.             $event = new stdClass();
  83.             $event->type = "goebies";
  84.             $event->text = "Goebies Supply Run in 15m";
  85.             $event->time = clone $goebiesTime;
  86.  
  87.             $events[] = $event;
  88.  
  89.             $goebiesTime->modify('+12 hours');
  90.         }
  91.  
  92.  
  93.         usort($events, function($a, $b) {
  94.           $ad = $a->time;
  95.           $bd = $b->time;
  96.  
  97.           if ($ad == $bd) {
  98.             return 0;
  99.           }
  100.  
  101.           return $ad > $bd ? -1 : 1;
  102.         });
  103.  
  104.         if($memory->active()){
  105.             $memory->set('_events',$events,60);
  106.         }
  107.     }
  108.  
  109.     echo json_encode($events);
Advertisement
Add Comment
Please, Sign In to add comment