-
-
Save deifos/40295607aca7340559064560f5181e91 to your computer and use it in GitHub Desktop.
Take pictures from a folder that has pictures and txt files with the same name holding the ALT text, tweet one picture every 60 minutes and moving the photo and .txt file to another folder
This file contains 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 os | |
import tweepy | |
import time | |
#setting up twitter API authentication | |
consumer_key = 'xxx' | |
consumer_secret = 'xxx' | |
access_token = 'xxx' | |
access_token_secret = 'xxx' | |
#authenticating with twitter API | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
#defining the folder path | |
folder_path = 'source_path' | |
#defining the destination folder path | |
destination_folder_path = 'destination_path' | |
#process every file in the folder | |
for file in os.listdir(folder_path): | |
#check if the file is a photo | |
if file.endswith(".jpg") or file.endswith(".png"): | |
#open the corresponding text file | |
txt_file = open(folder_path + file[:-3] + 'txt', 'r') | |
#read the text file | |
alt_text = txt_file.read() | |
#Get path of file | |
photo_path = folder_path + file | |
#1 upload photo to get the id so then the ALT text can be set | |
res = api.media_upload(photo_path) | |
_media_ids = [res.media_id_string] | |
#2 uplaod metadata for photo now that we have the media id | |
api.create_media_metadata(res.media_id_string, alt_text) | |
#3 post photo and message | |
api.update_status(media_ids=_media_ids, status='today is an awesome day') | |
# if you just want to post the photo with no ALT text skip step 1, 2, 3 and just do this line. | |
# api.update_with_media('today is an awesome day', photo_path) | |
#close the text file | |
txt_file.close() | |
#move photo and text file to destination folder | |
os.rename(photo_path, destination_folder_path + file) | |
os.rename(folder_path + file[:-3] + 'txt', destination_folder_path + file[:-3] + 'txt') | |
#wait for 1 hour | |
print('job done waiting to do again') | |
time.sleep(3600) | |
else: | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment