Skip to content

Instantly share code, notes, and snippets.

@ollybritton
Created November 19, 2017 13:23
Show Gist options
  • Save ollybritton/a74ebbe20834c9066deacbb846b04bbd to your computer and use it in GitHub Desktop.
Save ollybritton/a74ebbe20834c9066deacbb846b04bbd to your computer and use it in GitHub Desktop.
echo "This script provides an easy way to set up a twitter bot that tells you what is currently playing on your radio station First we just need to install some stuff, so you will need to enter your password. If anything fails, please contanct Olly. Press <ENTER> to continue: "
read
sudo apt-get update
sudo apt-get install python3-pip
sudo pip install tweepy
# Use special options
echo "First we need to create a twitter app and use its credentials. This can be done by going to apps.twitter.com/app/new on the account you wish to make tweets from. Press <ENTER> to continue. "
read
echo "1. Name: 'Now Playing Information' or similar."
echo "2. Description: 'Shows currently playing on <RADIO NAME>.' or similar."
echo "3. Website: If you have a website for the station, use it (don't forget to prefix it with http://) or else just use a placeholder like http://example.com"
echo "4. Callback URL: Leave empty."
echo ""
echo "Don't forget to tick the 'Yes, I have read and agree to the Twitter Development Agreement' box."
echo "Press <ENTER> to continue. "
read
echo "Now you should be brought to a page contaaining the information regarding your app. You need to click on the 'Keys and Access Tokens' section. Press <ENTER> to continue. "
read
echo "There should be a field called 'Consumer Key'. Please enter the text in the field, making sure you enter it correctly. "
read consumer_key
echo "Next, there should be a field called 'Consumer Secret', underneath the first field. Please enter the text in the field, making sure you enter it correctly. "
read consumer_secret
echo "Now you have to scroll down to the section where it says something along the lines of 'Generate My Access Token'. Click on that, and two more fields should appear. Press <ENTER> to continue."
read
echo "Please enter the 'Access Token', making sure to put it in correctly. "
read access_token
echo "Please enter the 'Access Token Secret', making sure to put it in correctly. "
read access_token_secret
echo "Please find the file which contains the now playing information. For 321xmas, this was 'http://321xmas.com:8000/stats?sid=1&json=1'. Please enter it using the full http://. "
read request_website
echo "Ok, now we just need to set the amount of time between updates. Please give the number in minutes. "
read wait_time
echo "And finally, what website would you like them to visit? "
read website
echo "The code will now create a directory called 'now_playing' and run the code contained inside it. Press <ENTER> to continue."
read
mkdir now_playing
cd now_playing
touch main.py
touch credentials.py
cat >main.py <<EOL
import tweepy, random, requests, time, sys
from credentials import *
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
REPEAT_TIME = ${wait_time} # Minutes
while True:
data = requests.get("${request_website}").json()
name = data["songtitle"].split(" - ")[1]
artist = data["songtitle"].split(" - ")[0]
print("Tweeting Message: \"" + "#NowPlaying " + name + " by " + artist + " on ${website}" + "\". ")
try:
api.update_status(" + "#NowPlaying " + name + " by " + artist + " on ${website}")
except:
print("Woah... there was an error. Either you tried to tweet a duplicate status, or you lost auth. May god have faith on you. (If this is repetitive, you probably lost auth. We had a good run.)")
for i in range(REPEAT_TIME * 60):
print(str( (REPEAT_TIME * 60) - (i + 1) ) + " seconds until next tweet.")
sys.stdout.write("\033[F")
time.sleep(1)
EOL
cat >main.py <<EOL
consumer_key = '${consumer_key}'
consumer_secret = '${consumer_secret}'
access_token = '${access_token}'
access_token_secret = '${access_token_secret}'
EOL
echo "Hopefully all setup is done! Just a few common errors: "
echo ""
echo "1. Something about 'auth'. This usually means that the credentials you entered were wrong."
echo "2. Something about 'no module'. This means that you don't have one of the dependencies installed."
echo ""
echo "Press <ENTER> to run!"
read
python3 main.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment