Skip to content
Permalink
Browse files

Now blogger is an available service

  • Loading branch information...
shawnmjones committed Apr 30, 2019
1 parent f7873d7 commit f43aa2c44bde77dcff666ccc9cb7a9e3ca9a8e07
Showing with 109 additions and 8 deletions.
  1. +4 −0 bin/raintale_cmd
  2. +7 −3 raintale/storygenerators.py
  3. +93 −3 raintale/storytellers.py
  4. +5 −2 setup.py
@@ -124,6 +124,7 @@ def gather_credentials(storyteller, credentials_file):
"output_filename": storyteller
}
else:

with open(credentials_file) as f:
credentials_json = load(f, Loader=Loader)

@@ -198,6 +199,9 @@ def tell_story(storyteller, storyformat, story_data, mementoembed_api,
storyformat = "rawhtml_{}".format(storyformat)
storyteller = "rawhtml"

if storyteller == "blogger":
storyformat = "rawhtml_{}".format(storyformat)

sg = storygenerators[storyformat](mementoembed_api)
st = storytellers[storyteller](sg, story_data, credentials)
st.tell_story()
@@ -12,12 +12,12 @@ def __init__(self, mementoembed_api):
if mementoembed_api.endswith('/'):
self.mementoembed_api = mementoembed_api[:-1]

def get_urielement_rawhtml(self):
def get_urielement_rawhtml(self, urim):
raise NotImplementedError(
"StoryGenerator class is not meant to be called directly. "
"Create a child class to use StoryGenerator functionality.")

def get_urielement_data(self):
def get_urielement_data(self, urim):
raise NotImplementedError(
"StoryGenerator class is not meant to be called directly. "
"Create a child class to use StoryGenerator functionality.")
@@ -32,7 +32,11 @@ def get_urielement_rawhtml(self, urim):
self.mementoembed_api, urim
)

r = requests.get(api_endpoint)
headers = {
"Prefer": "using_remote_javascript=no"
}

r = requests.get(api_endpoint, headers=headers)

if r.status_code == 200:
element_data = r.text
@@ -1,7 +1,9 @@
import logging
import twitter
import requests

import sys
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow

module_logger = logging.getLogger('raintale.storytellers')

@@ -200,6 +202,13 @@ def tell_story(self):
urim, element_data['errordata']
))

else:

module_logger.warn(
"skipping unsupported story element type of {}".format(
element['type']
))

except KeyError:

module_logger.exception(
@@ -212,12 +221,93 @@ def tell_story(self):
"cannot post tweet for story element {}, skipping".format(element)
)

class BloggerStoryTeller(StoryTeller):

def tell_story(self):

module_logger.debug("telling story using BloggerStoryTeller")

client_config = {
"installed": {
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"redirect_uris": [],
"client_id": self.credentials['clientid'],
"client_secret": self.credentials['clientsecret']
}
}

flow = InstalledAppFlow.from_client_config(
client_config,
scopes=['https://www.googleapis.com/auth/blogger'])

credentials = flow.run_console()

blogger_service = build('blogger', 'v3', credentials=credentials)

story_elements = self.get_story_elements()

blog_post_content = '<p><strong>Story By: </strong>{}</p><p>Collection URL: <a href="{}">{}</a></p>'.format(
self.story_data['generated_by'],
self.story_data['collection_url'], self.story_data['collection_url']
)

story_elements = self.get_story_elements()

for element in story_elements:

module_logger.debug("examining story element {}".format(element))

try:

if element['type'] == 'link':

urim = element['value']

module_logger.debug(
"getting story data for URI-M {}".format(urim)
)

raw_element_html = self.storygenerator.get_urielement_rawhtml(urim)

blog_post_content += "<p>\n{}\n</p>\n".format(raw_element_html)

else:
module_logger.warn(
"skipping unsupported story element type of {}".format(
element['type']
))

except KeyError:

module_logger.exception(
"cannot process story element data of {}, skipping".format(element)
)

module_logger.info("posting story to blog ID {}".format(self.credentials['blogid']))

request_body = {
"kind": "blogger#post",
"title": self.story_data['title'],
"content": blog_post_content
}

r = blogger_service.posts().insert(
blogId=self.credentials['blogid'],
body=request_body
).execute()

module_logger.info("blog post should be available at {}".format(r['url']))



storytellers = {
"rawhtml": RawHTMLStoryTeller,
"twitter": TwitterStoryTeller
"twitter": TwitterStoryTeller,
"blogger": BloggerStoryTeller
}

storytelling_services = [
"twitter"
"twitter",
"blogger"
]
@@ -29,9 +29,12 @@
'bin/raintale_cmd',
],
install_requires=[
'requests',
'google-api-python-client',
'google_auth_oauthlib',
'oauth2client',
'pyyaml',
'python-twitter'
'python-twitter',
'requests'
],
test_suite="tests",
zip_safe=True,

0 comments on commit f43aa2c

Please sign in to comment.
You can’t perform that action at this time.