Created
April 4, 2020 20:37
-
-
Save blakegreendev/d15cde6323eb72d62884fc90c2e02263 to your computer and use it in GitHub Desktop.
twitter-bot-lambda
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
import boto3 | |
from twitter import OAuth, Twitter | |
import requests | |
from bs4 import BeautifulSoup | |
import random | |
def main(event, context): | |
####################### WEB SCRAPING ###################################### | |
r = requests.get('https://greengocloud.com/archives/') | |
p = [] | |
soup = BeautifulSoup(r.text, 'html.parser') | |
posts = soup.find_all(class_='archive-article-title') | |
for post in posts: | |
#print(post) | |
title = post.get_text() | |
#print(title) | |
link = post.get('href') | |
tl = title + ' #aws #greengocloud ' + 'https://greengocloud.com' + link | |
p.append(tl) | |
blog = random.choice(p) | |
print(blog) | |
##################### TWITTER ############################################ | |
CONSUMER_KEY_PARAM_NAME = '/{}/consumer_key'.format('tsgt_green') | |
CONSUMER_SECRET_PARAM_NAME = '/{}/consumer_secret'.format('tsgt_green') | |
OAUTH_TOKEN_PARAM_NAME = '/{}/access_token'.format('tsgt_green') | |
OAUTH_SECRET_PARAM_NAME = '/{}/access_token_secret'.format('tsgt_green') | |
SSM = boto3.client('ssm') | |
param_names=[ | |
CONSUMER_KEY_PARAM_NAME, | |
CONSUMER_SECRET_PARAM_NAME, | |
OAUTH_TOKEN_PARAM_NAME, | |
OAUTH_SECRET_PARAM_NAME | |
] | |
response = SSM.get_parameters( | |
Names=param_names, | |
WithDecryption=True | |
) | |
param_lookup = {param['Name']: param['Value'] for param in response['Parameters']} | |
oauth_token=param_lookup[OAUTH_TOKEN_PARAM_NAME] | |
oauth_secret=param_lookup[OAUTH_SECRET_PARAM_NAME] | |
t = Twitter( | |
auth=OAuth( | |
oauth_token, | |
oauth_secret, | |
consumer_key=param_lookup[CONSUMER_KEY_PARAM_NAME], | |
consumer_secret=param_lookup[CONSUMER_SECRET_PARAM_NAME], | |
) | |
) | |
t.statuses.update(status=blog) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment