GithubHelp home page GithubHelp logo

joaquinlpereyra / twitterimgbot Goto Github PK

View Code? Open in Web Editor NEW
110.0 15.0 42.0 73 KB

Ever wanted to randomly tweet pictures from a folder? No? Well, maybe you do now.

License: GNU General Public License v3.0

Python 100.00%

twitterimgbot's Introduction

twitterImgBot

There are way too many pretty pictures in the world to tweet all of them. twitterImgBot tries to solve just that.

The primary objective of this script is to tweet a random picture from a given folder. But it does some other things too and has some pretty nice things built in as well. See the features.

It can easily be used together with cron or another task scheduler to create a twitter bot. All you need is a computer with internet connection, python, tweepy and folder with a bunch of images.

Features

  • Separate config file. You don't have to touch a line of code. Of course can also do that if you wish <3.
  • Deletes and bans tweets upon command: The bot listens to a master account for a tweet with a specified command to delete the tweet that was indicated by the reply id of the master command. In simple terms, that means you can delete a tweet by replying to it with a custom command from your master account. It will also ban the image posted in the deleted tweet, never uploading it again.
  • Handles requests: The bot will also listen to tweets containing a custom string from any user and will post a random picture for them. A list of possible text answers to be posted along with the image can be given and the bot will choose one them at random. If a tweet from user A with the custom string is found and the tweet also has a "to @(user B)" the bot will interpret that the user A is gifting an image to user B. A list of possible text answers to such "gifts" can also be given.
  • No repeat: The bot won't repeat images until a custom amount of pictures have been posted in between.
  • Tweets at random intervals: The bot can tweet at random intervals, behaving more like a human, instead of tweeting at totally predictable times.
  • Can post tweet number and an user defined text with each tweet: You can make your bot tweet only the images, you can add the post number to your tweets and you can add a custom text also.
  • Pretty fast: The bot checks requests, commands from the master account and checks if it needs to upload a picture in less than a second. Of course, if the bot actually needs to upload something, it will probably take more time.
  • Logs: This actually needs to be improved to use Python's standard logging module, but it is good as it is and it gets the job done. The log contains date, image posted and the twitt that was being replied (if any).
  • Doesn't need much: Really, you're all set with python, tweepy, a task scheduler like cron and a bunch of images in a folder. Of course, you'll also need to create a Twitter app to get your API key and such.

Setup

You need:

You'll probably also want a task scheduler like cron to fully automate the bot.

Python3 comes with pretty much every modern distro, but you probably don't have tweepy installed. You should install pip.

sudo apt-get install python3-pip

and then tweepy

pip install tweepy

You should then complete the settings file inside the settings folder. There's a detailed explanation of every setting and also an example config file in the Options section. Please:

  • Make sure all settings are filled correctly.
  • Double check paths.
  • Do use full paths (not ~/bot/ but /home/username/bot/).}
  • Do end paths with / (forward slash).

The bot WILL probably fail if these conditions are not met.

Options

Explanation

  • [Twitter]
  • [App]
    • image_folder: source folder for the bot to look up the images to be posted.
    • execution_chance: this makes the bot tweet at random intervals. Set it to a low value and have to bot execute very often: most of the time it will not tweet. I find that 1 per cent execution_chance works well if the script runs every minute.
    • allow_repeat_after: how many images the bot must have posted before it is allowed to repeat a picture.
    • log_file: full path to the log file. You probably want to use BOT_PATH/logs/log.
    • bot_account: the username for the bot account. DO start with @.
    • master_account: the username which the bot will listen to for delete commands. DO start with @.
    • dont_tweet_file: full path to the banned images list file. You probably want to use BOT_PATH/logs/banned
  • [Orders]
    • ban_command: if master_account tweets something starting with this, will delete last tweet and ban last image posted.
    • request_command: if anybody tweets something starting with this, it will give them a picture. If tweet has a "to @(userB)" in it somewhere and starts with the request_command, bot will interpret as a gift from user posting to userB.
    • time_tolerance: How old should a request be for it to be ignored? Do not set this to be less than the frequency with which you'll set the bot on cron.
  • [Texts]
    • tweet_post_number: True/False. This will prepend your tweets with a text looking like "Nr. X" where X is your post number. Do read the Backwards Compatibility section if you were using a previous version of the bot or if you will start using this bot in non empty twitter account.
    • tweet_this_text: Just write whatever you want to be tweeted with your image. This will come after the post number, if you set tweet_post_number to True. This would most probably be a URL asociated with your bot, but you can set it to whatever you want. If you want nothing to customized text to be tweeted, just leave it blank
    • requests_answers: bot will choose randomly from here when complying to request from a request_command with no "to @". the reply will look like "@user request_answer"
    • requests_to_third_answers: bot will choose ramdonly from here when complying to requests from a request_command with "to @" in it. the reply will look like "@userB request_to_third_answers @requester"

Example

# Replace where appropriate with your own settings.

[Twitter]
api_key = your_api_key
secret_key = your_secret_key
token = your_token
secret_token = your_secret_token

[App]
image_folder = /home/botImages/
execution_chance = 1
allow_repeat_after = 50
log_file = /home/bot/logs/log
bot_account = @gentelindaOK
master_account = @amemulo
dont_tweet_file = /home/bot/logs/banned

[Orders]
#caps agnostic
ban_command = @gentelindaOK don't
request_command = dear @gentelindaOK
time_tolerance = 5

[Texts]
# Respect indentation: it is TWO spaces
# tweet_post_number is either True or False, nothing else
# tweet this text doesn't requiere "" or '', just write the text
tweet_post_number = True
tweet_this_text = 
request_answers = specially for you. xoxo
  only because you asked nicely. xoxo
  this is one of my best. be grateful. xoxo
  when you see your human partner, think of me. xoxo
  remember: if you like it put a password on it. xoxo
  humans are warmer. but what if you like the cold?. xoxo
  can you feel your species deprecation?. xoxo
request_to_third_answers = you just got a nice gift from
  apparently this person likes you

Backwards Compatibilty

Please note that if you were using a previous version of the bot you'll have to execute the bot once (and only once!) like this from the terminal after enabling the option. This is specially important if you want to use the tweet_post_number option.

python twitterbot.py --tweet --tweetnumber X

Where X is the number of the tweet you'll post right now (that is, the number of posts the bot has tweeted plus one, you can see the number so far on tweeter).

This will log that tweet in a way so that the bot can read the post number from the log, so it will know now how many posts there have been. The --tweet option is there just to force the bot to tweet and ignore the execution chance.

If you don't do this, your log will register the bot's post_number wrongly, starting from one since you updated. Of course, if you use the tweet_post_number function, this'll mean the post numbers the bot tweetes will make no sense.

Usage

Execute it. That's it.

You also have a couple of useful arguments:

optional arguments:
  -h, --help  show this help message and exit
  --tweet     Ignores execution chance, always run
  --test      Wont't tweet, just write to log
  --postnumber Sets the post number to be posted if tweet_post_number is true. 

Live example and full automation idea

You can check out gentelindaOK (NSFW) to see a working account using this twitter bot.

@gentelindaOK is a fully automatic bot. My modified version of RedditImageGrab downloads new images to my PC from a multireddit every morning. Then, a silly one line bash command removes any image that is smaller than 50kb (to filter glitches and very small pictures). This twitter bot is set up on cron on my PC to execute every minute with a 1 percent execution chance set on the script's config file.

twitterimgbot's People

Contributors

drdub avatar joaquinlpereyra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

twitterimgbot's Issues

Bot should work even if executed less than once every 5 minutes

That.

The problem would be in the hardcoded 5 minutes limits found in some functions on some functions in the /bot/requests.py.

Fixing it should be quite easy. The thing is deciding when a request is too old to be even considered. Maybe adding a setting to load how often the bot executes, then deciding a request is too old if interval_between_execution*2 has passed?

The *2 is there because twitter search sometimes takes a while showing new tweets, so it's better to be safe than sorry and be sure to not miss any potential requests.

Option to not let users request images

As far as I've seen there's no way of stopping users from requesting pictures from the bot. I was about to make a bot which simply tweets pictures to a set amount of time.

No such file or directory logs

so i keep running into this issue, i'm using bash on windows to execute, and i just can't seem to get it to launch without that error.
root@DESKTOP-R7HT1HJ:/mnt/c/Users/name/Desktop/twitterImgBot-master# python twitterbot.py
Traceback (most recent call last):
File "twitterbot.py", line 209, in
main()
File "twitterbot.py", line 197, in main
post_number = get_post_number(manual_post_number)
File "twitterbot.py", line 145, in get_post_number
return get_post_number_from_log(config.log_file)
File "twitterbot.py", line 131, in get_post_number_from_log
post_number = open(log_file, 'r').readlines()[-1]
IOError: [Errno 2] No such file or directory: u'C:/Users/name/Desktop/twitterImgBot-master/logs/'
root@DESKTOP-R7HT1HJ:/mnt/c/Users/name/Desktop/twitterImgBot-master# python test/twitterbot_tests.py
python: can't open file 'test/twitterbot_tests.py': [Errno 2] No such file or directory
root@DESKTOP-R7HT1HJ:/mnt/c/Users/name/Desktop/twitterImgBot-master# python test/twitterbot_tests.py
python: can't open file 'test/twitterbot_tests.py': [Errno 2] No such file or directory
root@DESKTOP-R7HT1HJ:/mnt/c/Users/name/Desktop/twitterImgBot-master# python test/twitterbot_tests.py
python: can't open file 'test/twitterbot_tests.py': [Errno 2] No such file or directory
if someone could point me to what i'm doing wrong? i've tried all kinds of configurations at this point

KeyError from configparser.py

untitled-1

Hello, I keep getting this error and im not sure what to do to fix it. I used the same keys in a different bot to test them and it worked fine so im not sure what the issue is here.

Thanks!

Use parent folder name as text?

Hi, I'm just starting using your bot and so far, so good.

I have a folder with subfolders for each artist. As I want to credit the artist, I would like to tweet the name of the parent folder. Any idea how to do this?

Images repeat before the amount of photos listed in settings

I'm having an issue where images are repeating, even though the number of photos before repeat listed in the settings (200) has not been reached.

This is what my settings look like:
screenshot_145

Here is the log, where it repeats:
screenshot_146

Could it be because I don't have 200 pictures in my folder?
All I want is for it never to repeat images until all of them have been choosen already. I also add into the folder everyday, so I have to change the number of photos before repeat to something high.

Any help would be appreciated! Thank you.

allow_repeat_after and tweet_post_number not working

I've been having allow_repeat_after to 144, running every 30 min so should be not reposting the same image after 3 days but it still does.

Also I've had the tweet_post_number to False and tweet_this_text to nothing but when it tweets it would still do "No. " + str(post_number) + " " + raw_text

No such file or directory: '/home/logs/log'

It says it cant find it, am i doing something wrong? Like what should i type instead of: /home/logs/log
when its here: D:\Mine Dokumenter\twitterImgBot-master\logs
i dont know :/ please help

Looked cool, doesn't work

Tried setting up the bot, didn't seem to work, took a look at your test bot account which also didn't appear to be working. No error was written to the log file.
Based on the dates I suspect this no longer works and you are no longer maintaining

SyntaxError: invalid syntax

Receive following error when running the twitterbot.py script. My setup is "Python 3.5.1 |Anaconda 4.0.0 (64-bit)|"

File "twitterbot.py", line 69
media = random.choice(media_list)
^
SyntaxError: invalid syntax

ImportError: No module named configparser

jelle@jelle-OptiPlex-GX620:~/twitterImgBot$ python twitterbot.py
Traceback (most recent call last):
File "twitterbot.py", line 3, in
from settings import config
File "/home/jelle/twitterImgBot/settings/config.py", line 2, in
import configparser
ImportError: No module named configparser

It seems like im missing a module?

ValueError: invalid literal for int() with base 10: ''

Exception seems to happen when executing the program in config.py

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 1035, in _handle_fromlist
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Barzarin\PycharmProjects\twitterImgBot-master\twitterImgBot-master\settings\config.py", line 23, in <module>
    tolerance = int(app_config['allow_repeat_after'])
ValueError: invalid literal for int() with base 10: ''

Other info:
Python version: Python 3.7.1

Allow config.request_command to trigger as long as it finds keywords

currently, the request_command only works if the bot receives a tweet that matches the entire string
how do I make it so that as long as any part of the string is involved, it'll accept it?

Example:

  • request command = how are you
  • User tweets at the bot "@bot hello how are you?"
    the bot will respond because "how are you" is found in the string

thanks in advance

KeyError: 'Twitter' + 1 Other Issue

Running into an error when I try to run the script.
I'm not very savvy in coding, so I'm not quite sure what it is exactly, but here's what it looks like:
screenshot_143

If you could direct me in fixing this problem, I would be very gracious.

EDIT: Sometimes I run into this error instead:
screenshot_144

Tests are missing

The bot and its modules need tests. There's one on my computer but it's not made public because its the most horrible and unprofessional thing ever. There needs to be done some good 'ol testing using one of python's tools.

Stopp workring after som while

so on 2 different pc it stops working after a while and gives this error
Tried on the windows and ubuntu
But works fine for like 1 or 2 hours then it stops.
and when I try it it gives this error.
Looks some things hangs up.
Somebody who knows what to do.

timeline.delete_tweet_by_id(tweet.in_reply_to_status_id, api)
File "/home/user/Skrivebord/twitterboot/twiter/bot/timeline.py", line 12, in delete_tweet_by_id
api.destroy_status(id_to_delete)
File "/home/user/.local/lib/python3.7/site-packages/tweepy/binder.py", line 245, in _call
method = APIMethod(args, kwargs)
File "/home/user/.local/lib/python3.7/site-packages/tweepy/binder.py", line 71, in init
self.build_path()
File "/home/user/.local/lib/python3.7/site-packages/tweepy/binder.py", line 120, in build_path
raise TweepError('No parameter value found for path variable: %s' % name)
tweepy.error.TweepError: No parameter value found for path variable: id

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.