I don’t usually recommend auto-posting in social media however some applications require this behavior, and there should be an ethical way of doing this. I was experimenting a security project and wanted to share a tips for mysql usage with auto-tweeting.
The idea is to have twitter account which is connected to a database of pre-configured tweets, and tweeting randomly every hour or so. A pretty simple table of tweets :
-- Table structure for table
CREATE TABLE IF NOT EXISTS
id bigint(21) NOT NULL AUTO_INCREMENT,
tweet varchar(150) NOT NULL,
status tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (
A Cron job that select random tweet and share it to the world :
Select tweet from tweets order by RAND() Limit 0,1;
Now in addition to the “id, tweet”, you noticed that I have added a status field which will be incremented every time the message is tweeted. And to make the behavior not annoying to followers, I had to tweak this to keep tweets during the whole day, but in the same time not repeating myself !
So a quick check in the database I noticed these status values : 0,2,4,2,2,0,0,1,0,1…
Which means that some tweets have been shared 4 times and more, while others have not been tweeted at all. Let’s check this on database :
SELECT STATUS FROM
tweets ORDER BY RAND( )
Refresh the results few times :
So you can notice that selecting randomly a tweet that have not being broadcasted before is not very common in this quick test. To tweak this you should order by status equal zero first, then randomize. Which will give something like :
SELECT id,STATUS FROM
tweets ORDER BY status ASC,RAND()
Refresh the results few times again :
Now you notice that all status values are zeros, the non zeros values will be in the end, so in case I will add a limit the select will always give me a status zero !
Hope this help, enjoy !