mrbellek/twitterbot

Picture bot: fetch picture's descriptions from a JSON file

Closed this issue · 6 comments

Hello,

Firstly, thank you for the job you made for this tool, it is very useful for people who have only an intermediary level in PHP (as I am).

I would like to ask for an enhancement about the Picture bot's script. It works well but it is still very rough concerning the tweet text content which is associated with the pictures. It is especially currently impossible to add any links, which would be very useful to mention the sources of the files, or to mention a webpage associated with the topic of the pictures.

That's why it would be very useful to have an option to declare a JSON file in which the bot could fetch extra informations to add to the tweets. (JSON files are better than database in this case, in my opinion, because it is very light and easily editable).

Thank you in advance !

Hello, I'm glad you like the script. :) That's a pretty good idea! I made this as a quick way to post random pictures from a folder, but I'm sure it's easy to expand it with additional info from json file.

@laspic I've added a function to read extra info from a .json file. It works like this:

  • it looks for the json file as [bot username]-extra.json
  • contents are an array, keyed by the filename, with multiple possible entries per filename
  • each entry is an additional key-value pair to use when building the tweet.

E.g. contents of mypicturebot-extra.json:
{ "myfile.jpg": { "title": "my wedding photo" } }

And the php file (excluding api keys and such):
<?php
require_once('../picturebot.php');
``
$oTwitterBot = new PictureBot(array(
` 'sUsername' => 'MyPictureBot',`
` 'sPictureFolder' => MYPATH . '/pics',`
` 'sTweetFormat' => rtrim(':dirname - :basename (:title)'),`
`));`
`$oTwitterBot->run();`

Oh! A million thanks for you. I can't wait to test this. You made my day! :)

Edit: Ok, tested, it works perfectly (at least at home for now). Wonderful!

Since yesterday, I'm trying to make it works on my server with cron, without success. The pictures are well sent but the bot doesn't manage to fetch the description.

After several tests, here is what happens.

  • if I run it manually in console mode, directly from the folder where the script is located : everything is fine!
  • if I run it automatically with cron : descriptions are missing.
  • if I run manually in console mode, from the folder where the cron script is located : descriptions are missing.

Can we assume that there is a problem with the extra file declaration?

Here is my command script in it may help:

<?php

define('MYPATH', dirname(__FILE__));

require_once('credentials.php');
require_once('picturebot.php');

$oTwitterBot = new PictureBot(array(
   'sUsername'         => 'laspic',	
   'sPictureFolder' => MYPATH . '/pics', 
    'sTweetFormat'    => '#1day1legislature' 
						. chr(13) . chr(10) . chr(13) . chr(10) . 
						':description (:country).'.  
						chr(13) . chr(10) . chr(13) . chr(10) . 
						'(source: :source)',
));

$oTwitterBot->run();
?>

To see what the bot is supposed to, see https://twitter.com/search?q=%231day1legislature.

Alright, I've found a workaround: adding the following line at the top of the command script:
chdir(dirname(__FILE__));

Source: https://stackoverflow.com/questions/1969374/relative-path-not-working-in-cron-php-script.

If you have a better solution, I'll take it.

Hi Laspic, I'm not all that familiar with Unix, so that solution probably works as good as anything. Nice job! :)