mirror of
https://github.com/5vl/AutomatedCats.git
synced 2025-05-24 02:07:00 +00:00
Create get_cat_and_post.py
This commit is contained in:
parent
b5438207bf
commit
c9129690f1
42
get_cat_and_post.py
Normal file
42
get_cat_and_post.py
Normal file
@ -0,0 +1,42 @@
|
||||
from pipedream.script_helpers import (steps, export)
|
||||
|
||||
import os
|
||||
import requests
|
||||
import tweepy
|
||||
import re
|
||||
|
||||
def upload_media():
|
||||
tweepy_auth = tweepy.OAuth1UserHandler(
|
||||
"API_KEY",
|
||||
"APY_SECRET",
|
||||
"CONSUMER_KEY",
|
||||
"CONSUMER_SECRET",
|
||||
)
|
||||
|
||||
tweepy_api = tweepy.API(tweepy_auth)
|
||||
url = "https://api.thecatapi.com/v1/images/search?mime_types=jpg,png&api_key=API_KEY"
|
||||
cats = requests.request("GET", url).json()
|
||||
cat_pic = cats[0]["url"]
|
||||
img_data = requests.get(cat_pic).content
|
||||
with open("/tmp/catpic.jpg", "wb") as handler:
|
||||
handler.write(img_data)
|
||||
post = tweepy_api.simple_upload("/tmp/catpic.jpg")
|
||||
text = str(post)
|
||||
media_id = re.search("media_id=(.+?),", text).group(1)
|
||||
payload = {"media": {"media_ids": ["{}".format(media_id)]}, "text": "Here's a new cat! #cats #CatsOfTwitter"}
|
||||
os.remove("/tmp/catpic.jpg")
|
||||
return payload
|
||||
|
||||
|
||||
def post_tweet(payload):
|
||||
return requests.request(
|
||||
"POST",
|
||||
"https://api.twitter.com/2/tweets",
|
||||
json=payload,
|
||||
headers={
|
||||
"Authorization": "Bearer {}".format(steps["get_access_token"]["$return_value"]),
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
)
|
||||
|
||||
post_tweet(upload_media())
|
Loading…
x
Reference in New Issue
Block a user