Skip to content

Commit

Permalink
feat: Video and Description support
Browse files Browse the repository at this point in the history
  • Loading branch information
zeriyoshi committed Aug 22, 2023
1 parent ed1163d commit 67de16b
Showing 1 changed file with 58 additions and 14 deletions.
72 changes: 58 additions & 14 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,51 @@
import tweepy
import json
from datetime import datetime
from html.parser import HTMLParser
from googleapiclient.http import MediaIoBaseDownload
from googleapiclient.discovery import build
from google.oauth2 import service_account

# ----------------------------------------------------------------
# Message
# ----------------------------------------------------------------
class MessageManager(HTMLParser):
def __init__(self, data: str|None):
super().__init__()
self.entering_tag = ''
self.tweet = None
self.alt = None
if data is not None:
self.feed(data)

def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
self.entering_tag = tag

def handle_endtag(self, tag: str) -> None:
self.entering_tag = None

def handle_data(self, data: str) -> None:
match self.entering_tag:
case 'tweet':
self.tweet = data
case 'alt':
self.alt = data

def generate(self) -> tuple[str, str | None]:
if self.tweet is not None:
return [self.tweet, self.alt]

now = datetime.now(pytz.timezone('Asia/Tokyo'))
message = os.environ.get('MESSAGE_FOOTER', '#邪神ちゃん今日の1枚 をどうぞ。')
if 4 <= now.hour < 11:
message = os.environ.get('MESSAGE_HEADER_MORNING', 'フォロワーの皆さま、おはようございます! ') + message
elif 11 <= now.hour < 15:
message = os.environ.get('MESSAGE_HEADER_NOON', 'フォロワーの皆さま、ランチタイムです! ') + message
else :
message = os.environ.get('MESSAGE_HEADER_NIGHT', 'フォロワーの皆さま、今日も1日おつかれさまでした。お休み前に ') + message

return [message, self.alt]

# ----------------------------------------------------------------
# Google Drive
# ----------------------------------------------------------------
Expand Down Expand Up @@ -43,9 +84,15 @@
while finished is False:
_, finished = downloader.next_chunk()

# Get a metadata from Google Drive
metadata = drive.files().get(fileId = id, fields = 'name, mimeType, description').execute()

# ----------------------------------------------------------------
# Twitter
# X (formerly Twitter)
# ----------------------------------------------------------------
message, alt = MessageManager(metadata.get('description', None)).generate()

# Setup tweepy
consumer_key = os.environ['TWITTER_CONSUMER_KEY']
consumer_secret = os.environ['TWITTER_CONSUMER_SECRET']
access_token = os.environ['TWITTER_ACCESS_TOKEN']
Expand All @@ -57,25 +104,22 @@
# Create OAuth 1.1 API object for Media Upload
api_1_1 = tweepy.API(oauth1)

# Create OAuth 2.0 Client object for Tweet
# Create OAuth 2.0 Client object for Post
client_2_0 = tweepy.Client(
consumer_key = consumer_key,
consumer_secret = consumer_secret,
access_token = access_token,
access_token_secret = access_token_secret
)

# Upload picture and Tweet
media = api_1_1.media_upload('temporary')

# Generate text
now = datetime.now(pytz.timezone('Asia/Tokyo'))
message = '#邪神ちゃん今日の1枚 をどうぞ。'
if 4 <= now.hour < 11:
message = "フォロワーの皆さま、おはようございます! " + message
elif 11 <= now.hour < 15:
message = "フォロワーの皆さま、ランチタイムです! " + message
else :
message = "フォロワーの皆さま、今日も1日おつかれさまでした。おやすみ前に " + message
# Upload media
mime = metadata.get('mimeType', 'image/')
if mime.startswith('image/'):
media = api_1_1.simple_upload('temporary')
if alt is not None:
api_1_1.create_media_metadata(media.media_id, alt)
else:
media = api_1_1.chunked_upload('temporary', file_type = mime)

# Post
client_2_0.create_tweet(text = message, media_ids = [media.media_id])

0 comments on commit 67de16b

Please sign in to comment.