forked from wjswlgus123/py_tistory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tistory.py
163 lines (161 loc) · 5.77 KB
/
Tistory.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import requests
import re
import json
class Tistory():
def __init__(self, m_client_id, m_redirect_uri, m_user_id, m_password, m_blogname):
self.m_client_id = m_client_id
self.m_redirect_uri = m_redirect_uri
self.m_user_id = m_user_id
self.m_password = m_password
self.m_blogname = m_blogname
self.m_access_token = self.getAccessToken()
pass
def getAccessToken(self,):
print('Start getAccessToken')
# TODO: To get Access token
URL_0 = 'https://www.tistory.com/auth/login'
URL_1 = 'https://www.tistory.com/oauth/authorize'
loginParams = {
'redirectUrl':self.m_redirect_uri,
'loginId':self.m_user_id,
'password':self.m_password,
'fp' : 'mymackbook',
}
tokenParams = {
'client_id':self.m_client_id,
'redirect_uri':self.m_redirect_uri,
'response_type':'token'
}
headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'
}
rs = requests.session()
r0 = rs.get(URL_1, params=tokenParams)
#print(r0.url)
r1 = rs.post(URL_0, headers=headers, data=loginParams)
r2 = rs.get(URL_1, params=tokenParams)
#print(r2.url)
if r0.url == r2.url:
print('please confirm the email and try again')
return None
else:
match = re.match('(.*?)access_token=(?P<access_token>.*?)&state=', r2.url)
gd = match.groupdict()
access_token = gd['access_token']
print('access_token: '+access_token)
return access_token
def getList(self,m_page_num):
print("Start getList")
# TODO: 글 목록
url = "https://www.tistory.com/apis/post/list?access_token="+self.m_access_token+"&output=json"+"&blogName="+self.m_blogname+"&page="+str(m_page_num)
#print(url)
rd = requests.get(url)
#print(rd.text)
try:
item = json.loads(rd.text)
# print(item["tistory"]["status"])
# print(item["tistory"]["item"])
# print(item)
return item
except:
print("Fail")
return False
def getPublishedPosts(self):
m_published_posts = []
print("Start getPublishedPosts")
i =1
while i:
#print("I'm in while")
item = self.getList(i)
if str(item).find("posts") != -1:
posts = item["tistory"]["item"]["posts"]
#print("Start Print Posts")
#print(posts)
for post in posts:
m_visibility = str(post.get("visibility"))
if m_visibility == "20":
#print("published")
#print(post)
m_published_posts.append(post)
else:
return m_published_posts
i = i+1
def getRead():
# TODO: 글 읽기
return
def writePost(self, m_title, m_content, m_category, m_tag):
print("Start writePost")
# TODO: 글쓰기
# blogName: Blog Name (필수)
# title: 글 제목 (필수)
# content: 글 내용
# visibility: 발행상태 (0: 비공개 - 기본값, 1: 보호, 3: 발행)
# category: 카테고리 아이디 (기본값: 0)
# published: 발행시간 (TIMESTAMP 이며 미래의 시간을 넣을 경우 예약. 기본값: 현재시간)
# slogan: 문자 주소
# tag: 태그 (',' 로 구분)
# acceptComment: 댓글 허용 (0, 1 - 기본값)
# password: 보호글 비밀번호
params = {
'blogName' : self.m_blogname,
'title' : m_title,
'content' : m_content,
'tag' : m_tag,
'category' : m_category,
'visibility' : '0',
#'published' : '',
#'slogan' : '',
#'acceptComment' : '1',
#'password' : '',
'access_token' : self.getAccessToken(),
'output' : 'json'
}
data = json.dumps(params)
headers = {'Content-Type': 'application/json; charset=utf-8'}
rd = requests.post('https://www.tistory.com/apis/post/write', data=data, headers=headers)
if rd.status_code == 200:
return True
else:
print(rd.status_code)
print(rd.text)
item = json.loads(rd.text)
print(rd.status_code)
print(item["tistory"]["error_message"])
return False
def modifyPost():
# TODO: 글수정
return
def attach(self, m_imgname):
# TODO: 파일첨부
print("Start attach")
m_filepath = './img/'+m_imgname
files = {'uploadedfile': open(m_filepath, 'rb')}
params = {'access_token': self.getAccessToken(), 'targetUrl':self.m_blogname, 'output':'json'}
rd = requests.post('https://www.tistory.com/apis/post/attach', params=params, files=files)
try:
item = json.loads(rd.text)
print(item["tistory"]["replacer"])
print(item["tistory"]["url"])
os.remove(m_filepath)
return(item["tistory"]["replacer"])
except:
print("Success")
return item["tistory"]["replacer"]
def getCategoryList():
# TODO: 카테고리 리스트
return
def getNewestComment():
# TODO: 최근 댓글 목록 가져오기
return
def getCommnetList():
# TODO: 댓글 목록
return
def writeCommnet():
# TODO: 댓글 작성
return
def modifyCommnet():
# TODO: 댓글 수정
return
def delComment():
# TODO: 댓글 삭제
return