-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmdb.py
200 lines (181 loc) · 7.07 KB
/
tmdb.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from tmdbv3api import TMDb
from tmdbv3api import Movie,TV
import os
import json
from werobot import WeRoBot
import requests
import argparse
tmdb = TMDb()
tmdb.language = 'zh-CN'
tmdb.api_key = os.environ.get('tmdb_api_key')
movie = Movie()
tv = TV()
def Client():
robot = WeRoBot()
robot.config["APP_ID"] = os.environ.get('wechat_app_id')
robot.config["APP_SECRET"] = os.environ.get('wechat_app_secret')
client = robot.client
token = client.grant_token()
return client, token
def search_tmdb(type,name):
poster_path_list = []
content_list=[]
print('search_tmdb',type,name)
i = 0
if type == '电影':
search = movie.search(name)
if search['total_results'] == 0:
imgurl = 'http://saqnq6fvz.hb-bkt.clouddn.com/014109558a3f8b000000b18f00adbf.jpg%401280w_1l_2o_100sh.jpg'
return imgurl
for res in search:
if i == 1:
break
print('tmdbid:',res.id)
print('语言:',res.original_language)
print('tmdb评分:',res.vote_average)
print('上映日期:',res.release_date)
print('内容介绍:',res.overview)
imgurl = 'https://image.tmdb.org/t/p/original'+ res.poster_path
poster_path_list.append(imgurl)
content= 'tmdbid:'+str(res.id) + '<br>' + '语言:'+str(res.original_language) + '<br>' + 'tmdb评分:'+ str(res.vote_average) + '<br>' + '上映日期:'+ str(res.release_date) + '<br>' + '内容介绍:'+'<br>'+ res.overview + '<br><br>'
content_list.append(content)
if name == res.title:
i = 1
else:
i = 0
imgurl = poster_path_list[0]
content = content_list[0]
tmdbid = res.id
elif type == '电视剧':
show = tv.search(name)
if show['total_results'] == 0:
imgurl = 'http://saqnq6fvz.hb-bkt.clouddn.com/014109558a3f8b000000b18f00adbf.jpg%401280w_1l_2o_100sh.jpg'
return imgurl
for result in show:
if i == 1:
break
# print(result.name)
print('tmdbid:',result.id)
print('语言:',result.origin_country[0])
print('tmdb评分:',result.vote_average)
print('上映日期:',result.first_air_date)
print('内容介绍:',result.overview)
imgurl = 'https://image.tmdb.org/t/p/original'+ result.poster_path
poster_path_list.append(imgurl)
content = content= 'tmdbid:'+str(result.id) + '<br>' + '语言:'+str(result.origin_country[0]) + '<br>' + 'tmdb评分:'+str(result.vote_average) + '<br>' + '上映日期:'+str(result.first_air_date) + '<br>' + '内容介绍:'+'<br>'+result.overview + '<br><br>'
content_list.append(content)
if name == result.name:
i = 1
else:
i = 0
imgurl = poster_path_list[0]
content = content_list[0]
tmdbid = result.id
else:
'没有搜索到'
return imgurl,content,tmdbid
def down_img(img_url):
response_img = requests.get(img_url)
with open('tmdb_img.jpg', 'wb') as f:
f.write(response_img.content)
def down_dropback_img(dropback_url):
for i in dropback_url:
response_img = requests.get(i)
with open(str(i.split('/')[-1]), 'wb') as f:
f.write(response_img.content)
def upload_image():
#上传图片到公众号素材库
media_json = client.upload_permanent_media("image",open('tmdb_img.jpg', "rb")) ##永久素材
media_id = media_json['media_id']
media_url = media_json['url']
print('upload_success')
return media_id ,media_url
def upload_dropback_image(dropback_url):
#上传图片到公众号素材库
media_url_list=[]
for i in dropback_url:
media_json = client.upload_permanent_media("image",open(str(i).split('/')[-1], "rb")) ##永久素材
media_url = media_json['url']
print('upload_success')
media_url_list.append(media_url)
return media_url_list
def upload_wechat_news(token,content,thumb,title):
#上传到草稿未发表
AUTHOR = '电影小助手'
articles = {
'articles':
[
{
"title": 'up推荐-'+title,
"thumb_media_id": thumb,
"author": AUTHOR,
"digest": '电影推荐',
"show_cover_pic": 0,
"content": content,
"need_open_comment":1,
}
# 若新增的是多图文素材,则此处应有几段articles结构,最多8段
]
}
headers={'Content-type': 'text/plain; charset=utf-8'}
datas = json.dumps(articles, ensure_ascii=False).encode('utf-8')
postUrl = "https://api.weixin.qq.com/cgi-bin/draft/add?access_token=%s" % token['access_token']
r = requests.post(postUrl, data=datas, headers=headers)
resp = json.loads(r.text)
return resp
def down_dropback(type,id):
dropback_list=[]
headers = {
'User-Agent': 'Apipost client Runtime/+https://www.apipost.cn/',
'Content-Type': 'application/json',
}
params = (
('api_key', tmdb.api_key),
)
if '电影' == type:
type = 'movie'
else:
type = 'tv'
response = requests.get('https://api.themoviedb.org/3/%s/%s/images'%(type,id), headers=headers, params=params)
response = json.loads(response.text)
for i in response['backdrops']:
url = 'https://image.tmdb.org/t/p/original'+i['file_path']
dropback_list.append(url)
return dropback_list
def publish(token,media_id):
headers={'Content-type': 'text/plain; charset=utf-8'}
postUrl = "https://api.weixin.qq.com/cgi-bin/freepublish/submit?access_token=%s" % token['access_token']
articles = {
'media_id':media_id,
}
datas = json.dumps(articles, ensure_ascii=False).encode('utf-8')
r = requests.post(postUrl, data=datas, headers=headers)
resp = json.loads(r.text)
return resp
if __name__ =='__main__':
parser = argparse.ArgumentParser()
parser.add_argument("title", help="名称")
parser.add_argument("type", help="影视类型")
parser.add_argument("publish",help="发布yes/no")
options = parser.parse_args()
title = options.title
type = options.type
client, token = Client()
dropback_content=""
imgurl,content,tmdb_id = search_tmdb(type,title)
down_img(imgurl)
filename = imgurl.split('/')[-1]
media_id ,media_url = upload_image()
dropback_url = down_dropback(type,tmdb_id)
down_dropback_img(dropback_url)
dropback_wechat_urllist= upload_dropback_image(dropback_url)
print(dropback_wechat_urllist)
for i in dropback_wechat_urllist:
dropback_content += "<img src='%s'"%str(i)+ "/>" +'<br><br>'
wechat_content ='名称:'+ title + '<br>'+"<img src='%s'"%str(media_url)+ "/>" +'<br>' + content + '<br>' + 'tmdb剧照:'+ dropback_content
news_id = upload_wechat_news(token,wechat_content,media_id,title)
if options.publish == 'yes':
publish(token,news_id['media_id'])
else:
pass
os.system('rm *.jpg')