데이터 엔지니어 이것저것

python) API를 활용한 티스토리 업로드 본문

개발언어/Python

python) API를 활용한 티스토리 업로드

pastime 2020. 9. 9. 23:12
728x90
import requests 
import json
from bs4 import BeautifulSoup

#access_token를 만들기 위한 방법 + app_id, client_secret 얻는방법 포함
#https://limsee.com/325 
access_token=''
app_id = ''
client_secret = ''


url = f'https://www.tistory.com/oauth/access_token?client_id={app_id}&client_secret={client_secret}&redirect_uri=https://pastime2532.tistory.com/&code={code}&grant_type=authorization_code'


def json_parsing(response_json):
    json_text = json.dumps(response_json, indent=4, ensure_ascii=False)
    return json_text

url = 'https://www.tistory.com/apis/blog/info'
data = {'access_token': access_token, 'output': 'json'}
res = requests.get(url, params=data)
print(res.url)

json_text = json_parsing(res.json())        
print(json_text)


def blog_list(blog_name, page):
    url = 'https://www.tistory.com/apis/post/list'
    '''
    GET https://www.tistory.com/apis/post/list?
        access_token={access-token}
        &output={output-type}
        &blogName={blog-name}
        &page={page-number}
    blogName: Blog 이름
    page: 불러올 페이지 번호입니다. 1부터 시작    
    '''
    data = {'access_token': access_token, 'output': 'json', 'blogName': blog_name, 'page': page}
    res = requests.get(url, params=data)
    print(res.url)
    if res.status_code == 200:
        json_text = json_parsing(res.json())
        print(json_text)
        write_json_file('blog_list_' + blog_name + '_' + str(page) + '.json', json_text)
    else:        
        json_text = json_parsing(res.json())
        print(json_text)
blog_list('pastime2532', 1)



def blog_write(blog_name, category_id, title, content, tag):
    url = 'https://www.tistory.com/apis/post/write'
    visibility = 3    
    published = ''
    slogan = ''
    acceptComment = 1 
    password = ''
    '''
    blogName: Blog Name (필수)
    title: 글 제목 (필수)
    content: 글 내용
    visibility: 발행상태 (0: 비공개 - 기본값, 1: 보호, 3: 발행)
    category: 카테고리 아이디 (기본값: 0)
    published: 발행시간 (TIMESTAMP 이며 미래의 시간을 넣을 경우 예약. 기본값: 현재시간)
    slogan: 문자 주소
    tag: 태그 (',' 로 구분)
    acceptComment: 댓글 허용 (0, 1 - 기본값)
    password: 보호글 비밀번호
    '''
    data = {'access_token': access_token, 'output': 'json', 'blogName': blog_name, 'title': title, 
        'content': content, 'visibility': visibility, 'category': category_id, 'published': published, 
        'slogan': slogan, 'tag': tag, 'acceptComment': acceptComment, 'password': password}
    res = requests.post(url, data=data)
    print(res.url)
    json_text = json_parsing(res.json())
    print(json_text)
        
        
blog_write('pastime2532', 1, 'api를 사용한 업로드', 'api를 활용하여 게시글 업로드하기', 'API, PYTHON')


#출처 : https://github.com/chandong83
728x90

'개발언어 > Python' 카테고리의 다른 글

request로 File 보내기  (0) 2020.12.20
윈도우 알림 기능  (0) 2020.12.19
계약에 의한 디자인 (Design by Contract)  (0) 2020.11.08
파이썬 클린코드 (밑줄 _ 의 사용)  (0) 2020.11.02
슬랙 api  (0) 2020.11.02