| 1 |
<?php
|
| 2 |
// $Id: dailytwitter.module,v 1.4 2008/12/16 01:31:09 jadestorm Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Code for Daily Twitter module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_help().
|
| 11 |
*/
|
| 12 |
function dailytwitter_help($section) {
|
| 13 |
switch ($section) {
|
| 14 |
case 'admin/help#dailytwitter':
|
| 15 |
$output = '<p>The dailytwitter module allows users to cull together their twitter posts over the course of a day and display a add nicely formatted list of those posts to their own blog. The functionality is effectively the same as LoudTwitter, except done from within your own Drupal site.</p>';
|
| 16 |
return $output;
|
| 17 |
}
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_cron().
|
| 22 |
*/
|
| 23 |
function dailytwitter_cron() {
|
| 24 |
$send_last = variable_get('dailytwitter_send_last', 0);
|
| 25 |
$send_interval = variable_get('dailytwitter_send', 86400);
|
| 26 |
$send_time = time()-30;
|
| 27 |
if (time() - $send_last > $send_interval) {
|
| 28 |
_dailytwitter_update();
|
| 29 |
variable_set('dailytwitter_send_last', $send_time);
|
| 30 |
}
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implementation of hook_user().
|
| 35 |
*/
|
| 36 |
function dailytwitter_user($type, &$edit, &$user, $category = NULL) {
|
| 37 |
switch ($type) {
|
| 38 |
case 'delete':
|
| 39 |
db_query('DELETE FROM {dailytwitter} WHERE uid = %d', $user->uid);
|
| 40 |
break;
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Implementation of hook_perm().
|
| 46 |
*/
|
| 47 |
function dailytwitter_perm() {
|
| 48 |
return array('access dailytwitter');
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Implementation of hook_menu().
|
| 53 |
*
|
| 54 |
* @return array
|
| 55 |
*/
|
| 56 |
function dailytwitter_menu() {
|
| 57 |
$items = array();
|
| 58 |
|
| 59 |
$items['user/%user/dailytwitter'] = array(
|
| 60 |
'title' => 'Daily Tweets',
|
| 61 |
'page callback' => 'drupal_get_form',
|
| 62 |
'page arguments' => array('dailytwitter_user_settings_form', 1),
|
| 63 |
'access callback' => 'user_access',
|
| 64 |
'access arguments' => array('access dailytwitter'),
|
| 65 |
'type' => MENU_LOCAL_TASK
|
| 66 |
);
|
| 67 |
|
| 68 |
return $items;
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Menu callback; show user notification options.
|
| 73 |
*/
|
| 74 |
function dailytwitter_user_settings_form(&$form_state, $arg) {
|
| 75 |
global $user;
|
| 76 |
if ($user->uid != $arg->uid && $user->uid != 1) {
|
| 77 |
drupal_access_denied();
|
| 78 |
return;
|
| 79 |
}
|
| 80 |
|
| 81 |
$account = user_load(array('uid' => $arg->uid));
|
| 82 |
if (!is_object($account)) {
|
| 83 |
drupal_not_found();
|
| 84 |
return;
|
| 85 |
}
|
| 86 |
|
| 87 |
$result = db_query('SELECT uid, screen_name FROM {dailytwitter} WHERE uid = %d', $account->uid);
|
| 88 |
$dailytwitter = db_fetch_object($result);
|
| 89 |
$form = array();
|
| 90 |
|
| 91 |
$form['dailytwitter_page_master']['screen_name'] = array(
|
| 92 |
'#type' => 'textfield',
|
| 93 |
'#title' => 'Twitter username',
|
| 94 |
'#default_value' => $dailytwitter->screen_name,
|
| 95 |
'#size' => 40,
|
| 96 |
'#maxlength' => 255,
|
| 97 |
'#description' => 'What is your twitter username?',
|
| 98 |
);
|
| 99 |
$form['uid'] = array('#type' => 'value', '#value' => $account->uid);
|
| 100 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save settings'));
|
| 101 |
|
| 102 |
return $form;
|
| 103 |
}
|
| 104 |
|
| 105 |
function dailytwitter_user_settings_form_submit($form, &$form_state) {
|
| 106 |
unset($form);
|
| 107 |
db_query('DELETE FROM {dailytwitter} WHERE uid = %d', $form_state['values']['uid']);
|
| 108 |
db_query("INSERT INTO {dailytwitter} (uid, screen_name) VALUES (%d, '%s')", $form_state['values']['uid'], $form_state['values']['screen_name']);
|
| 109 |
drupal_set_message(t('Daily twitter settings saved.'));
|
| 110 |
}
|
| 111 |
|
| 112 |
function _dailytwitter_pull_yesterdays_posts($username) {
|
| 113 |
$start = strtotime("yesterday midnight");
|
| 114 |
$end = strtotime("today midnight");
|
| 115 |
$datastr = file_get_contents("http://twitter.com/statuses/user_timeline/". $username .".xml?count=20");
|
| 116 |
$xml = simplexml_load_string($datastr);
|
| 117 |
|
| 118 |
$statuses = $xml->{'status'};
|
| 119 |
$str = "";
|
| 120 |
foreach ($statuses as $s) {
|
| 121 |
$created_at = $s->{'created_at'};
|
| 122 |
$created_time = strtotime($created_at);
|
| 123 |
if ($created_time >= $end || $created_time < $start) {
|
| 124 |
continue;
|
| 125 |
}
|
| 126 |
$time = strftime("%H:%M", $created_time);
|
| 127 |
$text = $s->{'text'};
|
| 128 |
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\">\\0</a>", $text);
|
| 129 |
$text = preg_replace('/\@(\w+)/', '@<a href="http://twitter.com/\\1">\\1</a>', $text);
|
| 130 |
$str .= "<i>". $time ."</i> ". $text ." <a href='http://twitter.com/". $username ."/statuses/". $s->{'id'} ."'>#</a>\n";
|
| 131 |
}
|
| 132 |
return $str;
|
| 133 |
}
|
| 134 |
|
| 135 |
function _dailytwitter_add_tweets_to_blog($uid, $username) {
|
| 136 |
$account = user_load(array('uid' => $uid));
|
| 137 |
$tweets = _dailytwitter_pull_yesterdays_posts($username);
|
| 138 |
if ($tweets != "") {
|
| 139 |
// switch to user that we are posting for
|
| 140 |
global $user;
|
| 141 |
$original_user = $user;
|
| 142 |
session_save_session(FALSE);
|
| 143 |
$user = user_load(array('uid' => $uid));
|
| 144 |
|
| 145 |
// Create a new node
|
| 146 |
$start = strtotime("yesterday midnight");
|
| 147 |
$startdate = strftime("%m/%d/%Y", $start);
|
| 148 |
module_load_include('inc', 'node', 'node.pages');
|
| 149 |
$node = array('type' => 'blog');
|
| 150 |
$form_state = array();
|
| 151 |
$form_state['values']['title'] = 'Daily Twitter Posts - '. $startdate;
|
| 152 |
$form_state['values']['body'] = $tweets;
|
| 153 |
$form_state['values']['name'] = $account->name;
|
| 154 |
$form_state['values']['format'] = "1";
|
| 155 |
$form_state['values']['op'] = t('Save');
|
| 156 |
drupal_execute('blog_node_form', $form_state, (object)$node);
|
| 157 |
|
| 158 |
// switch back to original running user
|
| 159 |
$user = $original_user;
|
| 160 |
session_save_session(TRUE);
|
| 161 |
}
|
| 162 |
}
|
| 163 |
|
| 164 |
function _dailytwitter_update() {
|
| 165 |
$sql = 'SELECT uid,screen_name FROM {dailytwitter}';
|
| 166 |
$results = db_query($sql);
|
| 167 |
while ($row = db_fetch_array($results)) {
|
| 168 |
if ($row['screen_name'] != "") {
|
| 169 |
_dailytwitter_add_tweets_to_blog($row['uid'], $row['screen_name']);
|
| 170 |
}
|
| 171 |
}
|
| 172 |
}
|