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
|
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] creds = None if os.path.exists(TOKEN_PICKLE): with open(TOKEN_PICKLE, 'rb') as token: creds = pickle.load(token) 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) with open(TOKEN_PICKLE, 'wb') as token: pickle.dump(creds, token)
try: 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: 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 = "6532123509888947251" postBlog("title","content",'tttt,ttt2',blog_id)
|