from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from io import BytesIO
from datetime import datetime
import functools
from PIL import Image
import sys
# speedy log
print = functools.partial(print, flush=True)
def fanSarvice(accountName):
# file name
now = datetime.now()
nowDate = now.strftime('%Y%m%d_%H%M%S')
# selenium parametor
options = Options()
options.add_argument('--headless')
options.add_argument('--window-size=1080,1920')
# 環境に合わせてchromedriverとchrome.exeの位置を調整する
# Linux/Mac
# options.binary_location = '/usr/bin/google-chrome'
# driver = webdriver.Chrome('chromedriver', options=options)
# Windows
options.binary_location = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
driver = webdriver.Chrome(
options=options, executable_path="C:\\dev\\chromedriver.exe")
# profile
profile = f'https://twitter.com/{accountName}'
# popular reply
searchMore = f'https://twitter.com/search?q=to%3A{accountName}&src=typed_query'
# recent reply
recentSearch = f'https://twitter.com/search?q=to%3A{accountName}&src=typed_query&f=live'
# image
withImagesTweet = f'https://twitter.com/search?q=from%3A{accountName}%20filter%3Aimages&src=typed_query&f=live'
getSereenShotList = [profile, searchMore, recentSearch, withImagesTweet]
print(f'list is {getSereenShotList}')
# main
for i, j in enumerate(getSereenShotList):
print(i, j)
driver.get(j)
time.sleep(10)
im = Image.open(BytesIO(driver.get_screenshot_as_png()))
crpim = im.crop((120, 0, 740, 1920))
crpim.save(f'{nowDate}_{i}_{accountName}.png', quality=95)
driver.quit()
if __name__ == "__main__":
accountName = sys.argv[1]
fanSarvice(accountName)