Last active
January 27, 2018 03:49
-
-
Save smalleel/800dd08a759ae4f607700ca928008520 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| When this script is called, it tweets "Someone wants to fuck (Wikipedia article title)". | |
| Crontab: 0,30 * * * * /usr/bin/python3 /home/pi/bots/wiki.py | |
| Demo: https://twitter.com/wikifuckbot | |
| """ | |
| import tweepy | |
| import wikipedia | |
| def main(): | |
| tweet = get_tweet() | |
| if tweet: | |
| send_tweet(tweet) | |
| def get_tweet(): | |
| """ | |
| Gets random Wikipedia page, formats it into a tweet. | |
| Returns tweet as a string or re-calls main tweet if too long. | |
| """ | |
| title = wikipedia.random(pages=1) | |
| url = 'https://wikipedia.org/wiki/' + title.replace(' ', '_') | |
| if (len(title) + len(url) + 25) <= 280: | |
| return 'Someone wants to fuck:\n' + title + '\n' + url | |
| elif (len(title) + 23) <= 280: | |
| return ('Someone wants to fuck:\n' + title) | |
| elif len(title) <= 280: | |
| return title | |
| else: | |
| main() | |
| def send_tweet(tweet): | |
| """Takes a string and posts it to twitter.""" | |
| auth = tweepy.OAuthHandler('xxx', 'xxx') | |
| auth.set_access_token('xxx', 'xxx') | |
| api = tweepy.API(auth) | |
| api.update_status(tweet) | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment