Recovering Missing Tweets with Drupal 6 Twitter Module
My long-time followers are well aware that I have neglected this StockValues.Org blog, which BTW runs on Drupal 6, for much of 2013, even reducing the site to maintenance mode, making it completely inaccessible to the mere mortals, for many months.
In the meantime, I continued to use Twitter for quick updates and to share my thoughts. If Twitter wouldn't have made changes to their API, upgrading it to API 1.1 during this time period, all my tweets would have been recorded to this blog's database automatically by a dutiful cron, which never missed a beat. Well, Twitter changes happened and so my updates didn't.
You would think that to get my old tweets imported, all I would have to do would be to update the Drupal Twitter Module to the new version running Twitter API 1.1. However, by default, the new Twitter API will only pull the latest 20 tweets from a user's timeline and for some good reasons the Drupal 6 Twitter Module (latest stable version is currently 6.x-5.1), does nothing to alter this default behavior.
Of course, I didn't know any of this and so after I updated the Drupal Twitter Module and the cron job ran, I was left with a gaping whole in my Stock Values tweets database. Here is what I did and you can do as well, if you find yourself in a similar predicament, to fix things up:
1. Make backup copies of your twitter_account and twitter database tables, just in case.
2. For each user identified in the twitter database table by a unique screen_name, find twitter_id of the last tweet record that was pulled in prior to the breakdown.
3. For each such affected user, delete from your twitter database table all records of the tweets that have been imported after the gap.
4. For each such affected user, look up in the em>twitter_account database table the twitter_uid corresponding to the screen_name.
5. For each such affected user, in your twitter_account database table, set last_refresh equal to 0.
6. In the twitter.lib.php file found in your site's modules/twitter directory, following the line: $params['user_id'] = $id; add the following three lines of code, where twitter_uid_n's are all the twitter_uid's from step 4 and twitter_id_n's are their corresponding last tweet record twitter_id's defined in step 2:
$since_id = array(twitter_uid_1 => twitter_id_1,twitter_uid_2 => twitter_id_2,twitter_uid_3 => twitter_id_3, ...);
$params['count'] = 200;
$params['since_id'] = $since_id[$id];
7. Log in to your site as an administrator and visit its admin/reports/status/run-cron url, which will run the cron job and update the tweets database records.
The new Twitter API 1.1 will not return more than 200 records per call no matter what you do, so if any of your users produced more than 200 tweets from the time you initially stopped receiving updates, you will need to repeat the above process until you get them all. And once you are done and have verified that all is good, don't forget to restore your twitter.lib.php file to its original pristine state and delete extra backup copies of your database tables.
In my case, I only had to deal with my one global Twitter account and less than 200 new tweets to import, so there was only minimal manual labor involved and the process went very quickly. Of course, if you have more than a handful of users and loads of tweets for each one of them to import, you will want to automate the process further!