0%

python发送谷歌blogger

QQ群:397745473

python发送谷歌blogger

python发送谷歌blogger

碰了很多坑,这里记录一下

1
2
3
4
5
6
7
8
9
10
11
12
参考文章
# Blogger自動投稿 https://qiita.com/ronk/items/44e8912fd9bc41816286
这里面有完整代码,有几个需要注意的地方
在发送先需要先进行一次autov2.0 的授权

按下面步骤操作
1. 执行代码
2. 出现一个网址
3. 在浏览器中打开
4. 授权后会跳到另一个localhost的页面,取地址再把开一个窗口用curl提交一次就可以了

下面是测试发送的完整代码
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
# coding: utf-8
#import feedparser
import requests
import os
import pickle
from extractcontent3 import ExtractContent

from googleapiclient.discovery import build
from googleapiclient.discovery import HttpError
from googleapiclient.discovery import MediaFileUpload

from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

extractor = ExtractContent()
# オプション値を指定する
opt = {"threshold":50}
extractor.set_option(opt)

TOKEN_PICKLE = 'token.pickle'
CLIENT_SECRETS_FILE = "client_secret.json"

TRANS_REQUEST_SCOPE = "https://www.googleapis.com/auth/cloud-translation"
TRANS_API_SERVICE_NAME = "translate"
TRANS_API_VERSION = "v3"
TTS_REQUEST_SCOPE = "https://www.googleapis.com/auth/cloud-platform"
TTS_API_SERVICE_NAME = "texttospeech"
TTS_API_VERSION = "v1"
BLOGGER_UPLOAD_SCOPE = "https://www.googleapis.com/auth/blogger"
BLOGGER_API_SERVICE_NAME = "blogger"
BLOGGER_API_VERSION = "v3"
YOUTUBE_UPLOAD_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

def get_authenticated_service(api,version):
#scope = [BLOGGER_UPLOAD_SCOPE,YOUTUBE_UPLOAD_SCOPE,TTS_REQUEST_SCOPE,TRANS_REQUEST_SCOPE]
scope = [BLOGGER_UPLOAD_SCOPE]
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(TOKEN_PICKLE):
#print ('exists')
with open(TOKEN_PICKLE, 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
CLIENT_SECRETS_FILE, scope)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(TOKEN_PICKLE, 'wb') as token:
pickle.dump(creds, token)

try:
# simulate calling a method that's not defined
auth = build(api, version, credentials=creds)
except Exception as e:
print(e)
return None

return auth

def postBlog(title ,description, tag,blog_id ):
blogger = get_authenticated_service(BLOGGER_API_SERVICE_NAME,BLOGGER_API_VERSION)
jsondata = {
"kind": "blogger#post",
"blog": {
"id": blog_id,
},
"title": title,
"content": description,
"labels" : [tag],
}

try:
# simulate calling a method that's not defined
posts = blogger.posts()
insert = posts.insert(blogId=blog_id, isDraft=False, body=jsondata)
posts_doc = insert.execute()
except Exception as e:
print(e)
return None
return posts_doc["url"]

def getBody(link):
try :
res = requests.get(link)
extractor.analyse(res.text)
text, title = extractor.as_text()
postBlog(title,text,'TECHNOLOGY')
except Exception as e :
print(e)
#blog_id = "7314085719050314779"
blog_id = "6532123509888947251"
postBlog("title","content",'tttt,ttt2',blog_id)
#getBody('https://www.blogger.com/blog/posts/7314085719050314779?hl=ja&tab=jj')

QQ群:397745473

欢迎关注我的其它发布渠道