Giter VIP home page Giter VIP logo

n0toose / tinyflowerbeds Goto Github PK

View Code? Open in Web Editor NEW
12.0 2.0 6.0 47 KB

Educational bot that posts a tiny flower bed on Twitter every few hours. Check it out if you're new to Python and open source!

Home Page: https://twitter.com/tinyflowerbeds

License: GNU General Public License v3.0

Python 100.00%
twitter twitter-api twitter-bot tweepy tweepy-api flowers social-media bot hacktoberfest newcomers

tinyflowerbeds's Introduction

TinyFlowerBeds

"TinyFlowerBeds" is a bot that posts a tiny flower bed on Twitter every few hours. It is licensed under the GPL license.

Getting started

In order to use this bot, you'll need to have access to the Twitter API.

You can request access and get your credentials here.

The bot will check for a file called config.txt.

config.txt should have the following format:

CONSUMER_KEY = '<CONSUMER KEY>'
CONSUMER_SECRET = '<CONSUMER SECRET>'
ACCESS_KEY = '<ACCESS KEY>'
ACCESS_SECRET = '<ACCESS SECRET>'

By default, this bot will prioritize environment variables over the config.txt file.

If you believe that environment variables are more convenient for you, then you will need to register the following keys:

  • CONSUMER_KEY
  • CONSUMER_SECRET
  • ACCESS_KEY
  • ACCESS_SECRET ... and assign those keys to their respective values.

Prerequisites

Live Demo

A live demonstration the my bot can be viewed here.

Should you host your own variation of this bot, please include the link to my account in the biography of the account. The config dictionary can be freely adjusted to your needs.

Contributors

TinyFlowerBeds is the result of the effort of multiple hobbyists and Python enthusiasts. You can view a list of the people who have contributed to the project here.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

tinyflowerbeds's People

Contributors

gteti avatar kandasamyc avatar molyvius avatar n0toose avatar oxi-git avatar rohan2734 avatar shankhanil avatar sopneshgandhi avatar

Stargazers

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

Watchers

 avatar  avatar

tinyflowerbeds's Issues

Fix the bot!

So, I recently, carelessly merged #17 into the source of my Twitter bot, TinyFlowerBeds. Something went completely wrong with configparser, however.

Here's what you'll need to do!

  • Get the bot to read from a configuration file!
  • Prove (with a screenshot or a log) that the bot can now parse the credentials properly!

Do not tweet if the minimum interval is bigger than the time since the latest tweet.

  • Make sure that #2 has been resolved. minInterval should now be in the configuration (config.py) file, and the randomization should take place in the bot.py file.
  • Fetch the latest tweet's date from Twitter using tweepy (Documentation: http://docs.tweepy.org/en/latest/).
  • Check whether the time since the latest tweet is smaller than minInterval. If that's the case, then do not tweet.
  • If the bot shouldn't tweet, then what should it do? Your call.
  • Open a pull request!

Here's a drawing that may help with understanding what needs to be done better;
image

Respond to replies when the bot is mentioned (asyncio introduction)

Currently, the bot only posts tweets every once in a while and does not interact with any users in any capacity.

Wouldn't it be a great idea for people to be able to get their virtual flowers whenever they need them? The bot should monitor and detect replies and then react to them by responding with a newly generated flower bed.

Here's some documentation on how to update your status on Twitter. Take note of the optional in_reply_to_status_id parameter!

However, that's the easy part. The harder part would be finding the status IDs (the large numbers that appear in the URL bar when opening tweets) that correspond to the replies that the bot gets. Wouldn't it be simpler if there was a way to just fetch all the tweets in your notifications or maybe search for those that contain the username of the bot?

Well, I guess that there's a way. With tweepy, you can use the Stream API of Twitter. You can just create a listener that tracks for specific words and the bot will be able to respond to requests for flower beds across the Twitterverse!

Before you proceed with this issue, here are a few things to note:

  • Make sure to use the asynchronous version of the listener. That way, the part of the bot that responds to replies can run simultaneously with that part that waits for a specific time interval before making the next tweet!

  • There's a problem with the current version of the bot. The bot is not currently meant to be running asynchronously! However, due to the way that the bot is structured, it should be quite easy to rewrite the parts that cannot possibly support that, so that they can run simulatenously with other processes. Also, the function time.sleep() will block the execution of the bot entirely, so for example, if you set a listener to run alongside the part that waits for a few days before tweeting again, time.sleep() will block the listener! Here's a great introductory tutorial that may help you out:

  • Python sleep(): How to Add Time Delays to Your Code - Mike Driscoll

  • Getting Started With Async Features in Python - Doug Farroll

  • Async IO in Python: A Complete Walkthrough - Brad Solomon

  • Since these changes are going to be relatively large, it would be a very good idea to create an entirely separate Twitter account for this and test it out for yourself. You will need a Twitter account with developer privileges on it-- you can use your main account and then create a throwaway one after you get accepted, it doesn't matter. You can gain access to Twitter's developer platform here. There's also a tutorial by Ian Anderson Gray which can be found here.

If you do make some progress with this, feel free to open a pull request before finishing it for good, in order for me to be able to provide feedback and any necessary corrections. Don't worry about the number of commits-- I will squash all your changes before merging them to the upstream version of the project.

Update intro screen

I initially started TinyFlowerBeds in order to better understand Python's useful (and silly) applications and how to create apps that will interact with social media. After I took a simple approach for it, I made an intro screen that shows the name of the project, a copyright notice for the project's license and also added my name with a copyright sign.

https://github.com/AlwaysLivid/TinyFlowerBeds/blob/ad525983d4a143db1f34802e38ea2460c55bab4d/src/bot.py#L18

I essentially abandoned the project, but I've revived it, because I noticed that it's a simple fun thing to work on and that others could learn some useful stuff by fiddling around with it (just as I did). When I started working on it, however, I did not expect anybody else to improve the thing that I did. There has been some interest revolving around it and there have been people that have contributed their own source code! According to the GNU General Public License v3, the rights to the source code that they wrote belongs to them. I'm not the only copyright holder anymore!

So, here's what should change;

  • The year should be updated from 2018-2019 to 2018-2020.
  • Change my alias (AlwaysLivid) to something that includes my name, as well as the contributors, as well as a link that lists all of the people that have contributed to the bot on GitHub.
  • Make sure that the alignment remains intact. Feel free to make changes all over the string in order to maintain that.

Bot account suspended

Just before the 2020 US Elections, Twitter seems to have gotten rid of a lot of accounts that displayed a lot of bot-like behaviour, which also included many hobby projects hosted on the platform.

As a result, the TinyFlowerBeds Twitter bot seems to have gotten suspended for an undisclosed reason. I'm not sure if that suspension can be appealed successfully, but I've merged a few outstanding pull requests as a result without extensively testing them. Huge thanks to everyone who contributed to the bot.

Create a GitHub Actions workflow that deploys new commits to Heroku!

GitHub Actions are pretty nifty as far as avoiding mundane tasks is concerned. Here's one for instance-- I normally deploy new versions of the bot by myself, manually, on Heroku, which is where the bot is being hosted.

There's an action called Deploy to Heroku in the GitHub Marketplace. Use that and have your workflow push new revisions of the bot to Heroku immediately.

Use ${{secrets.X}} variables for the application data related to Heroku-- I'll fill out the variables by myself. :D

Automate the setup process for new users

For new users wanting to use the bot, setting up the bot might be a bit intimidating. This issue deals with automating the setup process for new users, with interactive walkthrough

Automate the setup process for new users

For new users wanting to use the bot, setting up the bot might be a bit intimidating. This issue deals with automating the setup process for new users, with interactive walkthrough

Support/Handle exception for rate limits.

In its current state, the bot tests whether it can login to Twitter and does not handle or sufficiently log any sort of rate limits imposed by Twitter.

Of course, reaching a rate limit can be hard for an account that just tweets once every few days, but a few external factors may also come into play, impose limitations on the bot account and then crash the bot itself.

That alone resulted in an outage that lasted for a few days, after my personal account (as well as all other accounts that are owned by me) got locked out, due to Twitter's mitigation efforts regarding COVID-19 misinformation.

The exception that's normally used by tweepy is tweepy.RateLimitError. But since the exception inherits from [tweepy.TweepError], that results in the bot mishandling the situation and treating a rate limit as an "authentication error".

The bot should check for rate limits FIRST when an exception arises and overlook the error, without interrupting the execution of the bot itself.

https://github.com/AlwaysLivid/TinyFlowerBeds/blob/9b571a32425c854c4cab8093932da1ca0395f679/src/bot.py#L118

Refactor config to use configparser

configparser is a widespread, simple-to-use module that's primarily used to create and manipulate configuration files.

Currently, one can use this bot by entering their Twitter credentials in a file called credentials.py in the src folder in a way that's slightly unorthodox and non-standard and may even be intimidating towards new contributors.

https://github.com/AlwaysLivid/TinyFlowerBeds/blob/856445843b986ff27a1619012332a2cd0b87d539/src/bot.py#L82

The part that loads the credentials for the Twitter bot should be rewritten, so that it loads an actual configuration file instead, rather than a Python module. It should contain the same variables and the same functionality. We shouldn't import a "module" with 4 variables.

You can see how a user can load credentials under normal circumstances in the README file.

You can find official documentation and instructions in the Python docs. Here's a checklist of what you should do;

  • Have the bot import configparser
  • Rewrite the lines 79-87 (as of 8564458).
  • Add a sample configuration file inside of the src folder.
  • Replace the instructions in the README file file in a separate commit.
  • Open a pull request :D

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.