TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Ras Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • The Joker
    • Hologram Machine
    • Google Vision: Camera Tell
    • Yoda Tweets
    • Pi Phone
    • Darth Beats
    • Twitter Keyword Finder
    • Crimbo Lights Hack
    • Xmas Elf
    • Halloween 2016
    • Halloween Hack 2015
    • Socrative Zombie
    • Voice Translation
    • The Blue-Who Finder
    • GPIO, Twitter
    • Pi Chat Bot >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • LED Dance Suit
    • Ferminal
    • Crypto Tracker
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me
  • Random Hacks
    • Movile

What is it?


This simple guide will get you started with Twitter and the Raspberry Pi.  You will be able to send a Tweet, stream your timeline and pull down your mentions, all from the terminal window in your Raspberry Pi.

Getting Started?


Firstly pop over to the Twitter API site and register your details.  This is required to create the Authentication Token and Access Token.    
Picture
These KEYS will be unique to you, keep them secure as they will be used in the Python code to stream the Twitter feed.  
Picture
Ensure that the under the application settings the Application Access Type is set to read and write, this will allow posting and reading timelines.
Picture
Once you have created your own account and you have the authorisation codes and tokens the program can be created.

First Update the Raspberry Pi
sudo apt-get update 

Upgrade your Pi
sudo apt-get upgrade 

Next install the python setup tools and pip, use this to install the Twython module that will allow the communication between the twitter API and Python.
sudo apt-get install python-setuptools 
sudo easy_install twython
or
sudo pip install twython

Using Twitter


There are three basic commands that are used to stream your tweets, your timeline and mentions.

Sending a tweet: api.update_status(status=say)
Your timeline: api.get_home_timeline(screen_name='your_screen_name')
Your mentions: api.get_mentions_timeline()

Sending a Tweet


###From @TeCoEd###
#!/usr/bin/env python
import sys
from twython import Twython

CONSUMER_KEY = 'replace with yours'
CONSUMER_SECRET = 'replace with yours'
ACCESS_KEY = 'replace with yours'
ACCESS_SECRET = 'replace with yours'

api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET) 
say = raw_input("What is your tweet? ")
api.update_status(status=say) #sends the tweet

Save the python program, for example tweet.py and launch the LX terminal, run the code using sudo python tweet.py

Streaming your Timeline


###From @TeCoEd###
#!/usr/bin/env python
import sys
from twython import Twython

CONSUMER_KEY = 'replace with yours'
CONSUMER_SECRET = 'replace with yours'
ACCESS_KEY = 'replace with yours'
ACCESS_SECRET = 'replace with yours'

api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET) 

tweets = api.get_home_timeline(screen_name='your_screen_name')
while True:
    for tweet in tweets:
        print(tweet['text'])
        time.sleep(5)


Save the python program, for example timeline.py and launch the LX terminal, run the code using sudo python timeline.py

Getting your Mentions


###From @TeCoEd###
#!/usr/bin/env python
import sys
from twython import Twython

CONSUMER_KEY = 'replace with yours'
CONSUMER_SECRET = 'replace with yours'
ACCESS_KEY = 'replace with yours'
ACCESS_SECRET = 'replace with yours'

api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET) 

tweets = api.get_mentions_timeline()
while True:
    for tweet in tweets:
        print(tweet['text'])
        time.sleep(2)


Save the python program, for example mentions.py and launch the LX terminal, run the code using sudo python mention.py
Powered by Create your own unique website with customizable templates.