본문 바로가기
category/python

chatGPT를 활용한 티스토리 썸네일 자동 업로드 with python(미완성)

by 자운대고라니 2023. 2. 21.
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import requests
import os
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
 
BLOG_NAME = "rok0188.tistory.com" # 블로그명 입력
ACCESS_TOKEN = "" # 발급 받은 ACCESS_TOKEN 입력
 
# 이미지에 넣을 글자와 폰트, 폰트크기, 배경색, 글자색을 입력합니다.
text = "title"
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"36)
font_color = (255255255)
background_color = (000)
 
# 이미지를 생성하는 함수입니다.
def generate_thumbnail(text):
    img = Image.new('RGB', (230300), color = background_color)
    d = ImageDraw.Draw(img)
    textwidth, textheight = d.textsize(text, font=font)
    x = (img.width - textwidth) / 2
    y = (img.height - textheight) / 2
    d.text((x, y), text, fill=font_color, font=font)
    return img
 
# 생성된 이미지를 저장하는 함수입니다.
def create_thumbnail(image, post_id):
    image_file = BytesIO()
    image.save(image_file, format='JPEG')
    image_bytes = image_file.getvalue()
    headers = {
        'Authorization': f'Bearer {ACCESS_TOKEN}',
        'Content-Type''image/jpeg'
    }
    response = requests.post(f"https://www.tistory.com/apis/post/attach?access_token={ACCESS_TOKEN}&blogName={BLOG_NAME}&postId={post_id}&useMarkdown=false", headers=headers, data=image_bytes)
    return response
 
def main():
    # 게시글 목록을 가져오는 API URL입니다.
    url = f"https://www.tistory.com/apis/post/list?access_token={ACCESS_TOKEN}&output=json&blogName={BLOG_NAME}&page=1&count=180"
 
    # API를 호출해서 게시글 목록을 가져옵니다.
    response = requests.get(url)
    posts = response.json()["tistory"]["item"]
 
    # 게시글 목록에서 하나씩 순회하면서 썸네일을 생성하고 업로드합니다.
    for post in posts['posts']:
        post_id = post["id"]
        text = post["title"]
        thumbnail_image = generate_thumbnail(text)
        create_thumbnail(thumbnail_image, post_id)
 
    print("모든 썸네일이 업로드되었습니다.")
 
if __name__ == "__main__":
    main()
cs

chatGPT를 활용해 썸네일 자동 업로드 코드를 만들었다.

생각보다 오류가 많고, 수정할 부분도 많아서 손 좀 봐줬다.

 

하지만 마지막 구문인 "모든 썸네일이 업로드되었습니다."가 나왔지만 변경되지 않은 것으로 확인했다.

무슨 문젠지 파악해야한다.

반응형

'category > python' 카테고리의 다른 글

visual studio code kivi 설치  (1) 2023.05.04
python web scraping - google image (PyQt)  (0) 2021.07.06
python web scraping  (0) 2021.02.27
python 모듈 만들기  (0) 2021.02.26
python slicing 방법  (0) 2021.02.21

댓글