Skip to content

ucla-its/twitterBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Revision UCLA Twitter Bot

Twitter bot posts an image correlated with the geoid and webpage to revision.

Bot was created by David James with help from Sophie and Parham in conjunction with the Institute of Transportation Studies (ITS).

Design

  1. Bot is implemented on Python 3.8 and is running on Amazon Web Services (AWS) lambda service
  2. It maintains the following dependencies
    Packages
    twython
    reader (AWS provided)
    OrderedDict (AWS provided)
    boto3 (AWS provided)
    os (AWS provided)

    NOTE: Those that are provided by AWS do not need to be installed via pip

  3. It’s design requires it to have access to an AWS S3 as well for downloading of the images during a given instance

AWS

Layers

  • python-libs: extra dependencies maintained here

Triggers

  • tweet-timer: bot will shoot a tweet every 30 min from 0700-0000 every day

File Structure

  • python: directory that has dependencies of libraries needed for the bot
  • README.org: README file with information on bot
  • twitter_bot.py: source code for twitter bot
  • screenshot.py: code for taking screenshot of the map

Maintenance

The bot should run for the year posting tweets. At the end of the year:

  • the CSV file maintained in S3 will need to be updated, if new blocks want to be shown. The CSV has the following format:
    • geoid,text,length
      • NOTE: Bot only accesses geoid and text. Length is to make sure it doesn’t go over Twitters character count.

Bash Commands for AWS

# ensure you are in the main dirctory that contains the code

# *** Updating Sorce Code for Twitter Bot ***
# zipping up source code for AWS
zip ../twitter_bot.zip twitter_bot.py
# uploading source code to AWS lambda service
aws lambda update-function-code --function-name its-twitter-bot --zip-file fileb://../twitter_bot.zip
# NOTE: both commands above insert the zip in the directory above the current directory
#       this is so no extra files are added to the github when zips are created

# *** Updating Libraries for Twitter Bot ***
# adding extra dependencies
pip install package --target python/lib/python3.8/site-packages/
# create new zip to be uploaded
zip -r ../python-libs.zip python
  • go to AWS>Lambda>Layes>python-libs>Create version
    • upload the new zip and choose python 3.8 and python 3.7 for runtimes
    • also update the description with new libraries added

Inserting Images

  • The bot is implemented to insert images, but may result in only text when “Twitter cards” are implemented on the website
  • If the cards get implemented the following lines of code are what should be removed
    # ... global variables defined at top of file
    KEY = 'tweets/'
    IMG_PATH = '/tmp/local.png'
    # ... within lambda_handler function
    print('Downloading PNG')
    s3.download_file(BUCKET,KEY+geoid+'.png',IMG_PATH)
    
    # ... within twitterBot function
    img = open(IMG_PATH,'rb')
    # . within try:
    response = twitter.upload_media(media=img)
    # replace .update_status
    twitter.update_status(status=text, media_ids=[response['media_id']])
    # with this line
    twitter.update_status(status=text)
        
  • images added to S3 must follow the format ‘GEOID.png’ where GEOID is the numerical ID of the block along with their information added to the CSV.

About

Simple Twitter Bot to post images

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages