Advertisement
terorama

twitter statuses

Dec 25th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.66 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. //--------------------------------------------
  5. function get_mysqli() {
  6.    
  7.    
  8.    return  new mysqli('','','','');
  9. }
  10.  
  11.  
  12. //--------------------------------------------
  13. function mkbase() {
  14.    $sqls = array ('drop table if exists twit',
  15.      'drop table if exists twits',
  16.    'create table twit(id int(11) not null auto_increment,
  17.    title varchar(500), description varchar(500),
  18.    link varchar(500), pubdate datetime, primary key(`id`) )');
  19.  
  20.  
  21.    $msq = get_mysqli();
  22.  
  23.    while (list($key,$sq) = each($sqls)) {
  24.      
  25.       echo 'executing sql: '.$sq.'<br/><br/>';
  26.  
  27.       $msq->query($sq);
  28.       if ($msq->errno!=0) {
  29.          echo 'error '.$msq->error.'<br/><br/>';
  30.       } else
  31.         echo 'executed successfully<br/><br/>';
  32.    }
  33.  
  34.    $msq->close();
  35. }
  36. //--------------------------------------------
  37.  
  38.  
  39. //--------------------------------------------
  40. function get_twits($since) {
  41.    
  42.    global $context;
  43.    global $baseaddr;
  44.  
  45.    echo $since.'<br/>';
  46.    $addr = sprintf($baseaddr, $since);
  47.    
  48.    if ($since==0)
  49.       $addr = substr($addr, 0, strrpos($addr,'&'));
  50.  
  51.    echo $addr.'<br/><br/>';
  52.  
  53.    //$s = file_get_contents($addr, false, $context);
  54.    
  55.    $ch = curl_init();
  56.    curl_setopt($ch, CURLOPT_URL, $addr);
  57.    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  58.    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  59.    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)");
  60.  
  61.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  62.    
  63.    $s = curl_exec($ch);
  64.  
  65.    if (curl_errno($ch))
  66.       throw new Exception('curl error: '.curl_error($ch));
  67.  
  68.    echo '<br/><br/>'.$s.'<br/><br/>';
  69.    curl_close($ch);
  70.  
  71.    return $s;
  72. }
  73.  
  74. //--------------------------------------------
  75. function parse_xml($inf) {
  76.  
  77.    $tz = array();
  78.  
  79.    $xml = simplexml_load_string($inf);
  80.  
  81.    foreach ($xml->channel->item as $item) {
  82.  
  83.       $itout = array();
  84.       $itout['title'] = (string)$item->title;
  85.       $itout['description'] = (string)$item->description;
  86.       $itout['link']  = (string)$item->link;
  87.       $itout['pubdate'] = (string)$item->pubDate;
  88.  
  89.       array_push ($tz, $itout);
  90.    }
  91.  
  92.    return $tz;
  93. }
  94.  
  95. //--------------------------------------------
  96. function savetodb($inf) {
  97.  
  98.    $msq = get_mysqli();
  99.  
  100.    $stmt = $msq->prepare('insert into twit(title,description,link,pubdate) '.
  101.    'values(?,?,?,?)');
  102.  
  103.    for ($i=0; $i<count($inf); $i++) {
  104.  
  105.       extract($inf[$i]);
  106.  
  107.       $stmt->bind_param('sssd', $title, $description, $link, $pubdate);
  108.       $stmt->execute();
  109.  
  110.       if ($stmt->errno!=0)
  111.           echo 'error '.$stmt->error.' while inserting twit: '.$title.'<br/><br/>';
  112.       else
  113.           echo 'inserted '.$stmt->insert_id.' twit: '.$link.'<br/>';
  114.  
  115.    }
  116.    $stmt->close();
  117.    $msq->close();
  118. }
  119.  
  120. //--------------------------------------------
  121. function showTwits() {
  122.  
  123.    $msq = get_mysqli();
  124.  
  125.    $result = $msq->query('select * from twit order by link desc');
  126.    if ($msq->errno!=0)
  127.       throw new Exception('mysqli error: '.$msq->error);
  128.  
  129.    echo '<table cellspacing="0" cellpadding="0" border="1" bordercolor="#eee">';
  130.  
  131.    while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  132.      
  133.       echo '<tr><td>'.
  134.         $row['title'].'</td><td>'.$row['link'].'</td><td>'.'</td></tr>';
  135.    }
  136.    echo '</table>';
  137.  
  138.    $result->close();
  139.    $msq->close();
  140. }
  141.  
  142. //--------------------------------------------
  143. function clearTwits() {
  144.  
  145.    session_start();
  146.    if (isset($_SESSION['nstart']))
  147.       unset ($_SESSION['nstart']);
  148.  
  149.    session_commit();
  150.  
  151.    $msq = get_mysqli();
  152.    $msq->query('delete from twit');
  153.  
  154.    if ($msq->errno!=0) {
  155.       throw new Exception('mysqli error: '.$msq->error);
  156.    }
  157.  
  158.    echo 'twit cleared, '.$msq->affected_rows.' deleted';
  159.  
  160.    $msq->close();
  161. }
  162. //--------------------------------------------
  163. function getStart() {
  164.    
  165.    $msq = get_mysqli();
  166.  
  167.    $result  = $msq->query ('select link from twit where id=(select max(id) from twit)');
  168.  
  169.    if ($msq->errno!=0) {
  170.       echo 'mysql error: '.$msq->error.'<br/><br/>';
  171.       throw new Exception('db error');
  172.    }
  173.  
  174.    if ($row= $result->fetch_array(MYSQLI_NUM))
  175.       $rez = substr(strrchr($row[0],'/'),1);
  176.    else
  177.       $rez = 0;
  178.  
  179.    $result->close();
  180.    $msq->close();
  181.  
  182.    return $rez;
  183.    
  184. }
  185. //--------------------------------------------
  186. function processTwits() {
  187.  
  188.    session_start();
  189.  
  190.    $reload = <<<STR
  191.    
  192.    <script type="text/javascript">
  193.  
  194.       el= document.createElement('DIV');
  195.       document.body.appendChild(el);
  196.  
  197.       el.num = 1;
  198.       el.zz = setInterval( function () {
  199.          
  200.           el.innerHTML = 'reload after '+el.num+'s';
  201.           el.num--;
  202.           if (el.num == 0) {
  203.              clearInterval(el.zz);
  204.              location.replace(location.href);
  205.           }
  206.       }, 1000);
  207.    </script>
  208.  
  209. STR;
  210.  
  211.    if (isset($_SESSION['nstart'])) {
  212.       $nstart = $_SESSION['nstart'];
  213.    } else {
  214.       $nstart = getStart();
  215.    }
  216.  
  217.  
  218.    echo 'starting position: '.$nstart.'<br/><br/>';
  219.  
  220.    $inf = get_twits($nstart);
  221.    $inf4 = parse_xml($inf);
  222.  
  223.    savetodb($inf4);
  224.  
  225.    $link = $inf4[count($inf4)-1]['link'];
  226.    
  227.    $nstart = substr(strrchr($link,'/'),1);
  228.    echo $nstart;
  229.  
  230.    $_SESSION['nstart']= $nstart;
  231.  
  232.    echo $reload;
  233. }
  234.  
  235. //--------------------------------------------
  236. $baseaddr = 'https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=drunktwi&count=200&max_id=%s';
  237.  
  238. $opts = array (
  239.    'http'=> array(
  240.       'method'=>'GET',
  241.       'header'=>  "User-Agent: Mozilla/5.0\r\n"));
  242.  
  243. $context = stream_context_create($opts);
  244.  
  245. if (isset($_GET['mkbase'])) {
  246.    mkbase();
  247.    exit();
  248.    }
  249.  
  250. if (isset($_GET['gettwits'])) {
  251.    processTwits();
  252.    exit();
  253. }
  254.  
  255. if (isset($_GET['showtwits'])) {
  256.    showTwits();
  257.    exit();
  258. }
  259.  
  260. if (isset($_GET['cleartwits'])) {
  261.    clearTwits();
  262.    exit();
  263. }
  264.  
  265. if (isset($_GET['clearsess'])) {
  266.    session_start();
  267.    if (isset($_SESSION['nstart']))
  268.       unset ($_SESSION['nstart']);
  269.  
  270.    session_commit();
  271. }
  272.  
  273.  
  274.  
  275. if (isset($_GET['query'])) {
  276.  
  277. ?>
  278.  
  279. <form action="<? echo $_SERVER["PHP_SELF"]."?query4";?>" method="POST" enctype="application/x-www-form-urlencoded">
  280.  
  281. <?php
  282. echo '<'.'textarea rows="30" cols="30" name="sqgo">'.
  283. 'zz'.
  284. '<'.'/textarea>';
  285.  
  286. ?>
  287. <p><input type="text" name="pass"/>
  288. <p><input type="submit" name="submit" value="go" /></p>
  289. </form>
  290.  
  291. <?php
  292.  
  293. exit();
  294.  
  295. }
  296.  
  297. if (isset($_GET['query4'])) {
  298.  
  299.    if ($_POST['pass']!='') {
  300.       echo 'inf';
  301.       exit();
  302.    }
  303.  
  304.    $msq = get_mysqli();
  305.    $result = $msq->query($_POST['sqgo']);
  306.  
  307.    echo '<table cellspacing="0" cellpadding="0" border="1" bordercolor="#999">';
  308.  
  309.    $flds = $result->fetch_fields();
  310.    echo '<tr>';
  311.    foreach ($flds as $fld) {
  312.       echo '<td>'.$fld->name.'</td>';
  313.    }
  314.    echo '</tr>';
  315.  
  316.    while ($row = $result->fetch_array(MYSQLI_NUM)) {
  317.       echo '<tr>';
  318.       foreach ($row as $vall) {
  319.          echo '<td>'.$vall.'</td>';
  320.       }
  321.       echo '</tr>';
  322.    }
  323.    echo '</table>';
  324.    $result->close();
  325.    $msq->close();
  326.    exit();
  327. }
  328.  
  329. session_start();
  330. ?>
  331.  
  332. <html>
  333. <head>
  334.  
  335. <style type="text/css">
  336.  
  337.  
  338. </style>
  339.  
  340. </head>
  341.  
  342. <body>
  343.  
  344. <?php
  345.  
  346.  
  347. echo '<pre>'.print_r($_SESSION,true).'</pre>'; ?>
  348. <ul><li><a href="<?php echo $_SERVER["PHP_SELF"]."?mkbase";?>">create database</a></li>
  349. <li><a href="<?php echo $_SERVER["PHP_SELF"]."?gettwits";?>">retrieve twits</a></li>
  350. <li><a href="<?php echo $_SERVER["PHP_SELF"]."?showtwits";?>">show twits</a></li>
  351. <li><a href="<?php echo $_SERVER["PHP_SELF"]."?clearsess";?>">clear sess</a></li>
  352. <li><a href="<?php echo $_SERVER["PHP_SELF"]."?query";?>">query</a></li>
  353. <li><a href="<?php echo $_SERVER["PHP_SELF"]."?cleartwits";?>">delete twits</a></li></ul>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement