import os
import httplib2
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run_flow
from googleapiclient import discovery
def authorize_credentials():
CLIENT_SECRET = 'client_secret.json'
SCOPE = 'https://www.googleapis.com/auth/blogger'
STORAGE = Storage('credentials.storage')
credentials = STORAGE.get()
if credentials is None or credentials.invalid:
flow = flow_from_clientsecrets(CLIENT_SECRET, scope=SCOPE)
http = httplib2.Http()
credentials = run_flow(flow, STORAGE, http=http)
return credentials
def get_blogger_service():
credentials = authorize_credentials()
http = credentials.authorize(httplib2.Http())
discovery_url = 'https://blogger.googleapis.com/$discovery/rest?version=v3'
service = discovery.build('blogger', 'v3', http=http, discoveryServiceUrl=discovery_url)
return service
def post_to_blogger(payload):
service = get_blogger_service()
post = service.posts()
insert = post.insert(blogId='7542194527810126825', body=payload).execute()
print("Done posting!")
return insert
def build_html_from_file(file_path):
with open(file_path, 'r') as file:
content = file.read()
html = f'{content}
'
return html
def main():
directory = '/path/to/text/files' # Update with the directory path where your text files are located
custom_metadata = "This is meta data"
file_names = [file for file in os.listdir(directory) if file.endswith('.txt')]
for file_name in file_names:
title = os.path.splitext(file_name)[0]
file_path = os.path.join(directory, file_name)
html_data = build_html_from_file(file_path)
payload = {
"content": html_data,
"title": title,
'labels': ['label1', 'label2'],
'customMetaData': custom_metadata
}
post_to_blogger(payload)
if __name__ == "__main__":
main()
Comments
Post a Comment