Skip to content

Commit

Permalink
Tidied up mastodon service module a bit
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed Aug 21, 2018
1 parent 7f1e07e commit 5054dc7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/pywws/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '18.8.0'
_release = '1564'
_commit = '5bfc528'
_release = '1565'
_commit = '7f1e07e'
27 changes: 10 additions & 17 deletions src/pywws/service/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,30 +155,24 @@

import codecs
from contextlib import contextmanager
from datetime import timedelta
import logging
import os
import sys

from mastodon import Mastodon

import pywws.localisation
import pywws.logger
import pywws.service
import pywws.storage

__docformat__ = "restructuredtext en"
service_name = os.path.splitext(os.path.basename(__file__))[0]
logger = logging.getLogger(__name__)


class ToService(pywws.service.FileService):
logger = logger
service_name = service_name
service_name = 'mastodon'

def __init__(self, context):
# check config
handle = context.params.get(service_name, 'handle', '')
handle = context.params.get(self.service_name, 'handle', '')
if not handle:
raise RuntimeError('No user handle specified in weather.ini')
# get default character encoding of template output
Expand All @@ -189,9 +183,9 @@ def __init__(self, context):

@contextmanager
def session(self):
handle = self.context.params.get(service_name, 'handle')
handle = self.context.params.get(self.service_name, 'handle')
api_base_url = handle.split('@')[-1]
access_token = self.context.params.get(service_name, 'access_token')
access_token = self.context.params.get(self.service_name, 'access_token')
if not access_token:
raise RuntimeError('Authentication data not found')
yield Mastodon(access_token=access_token, api_base_url=api_base_url)
Expand All @@ -214,21 +208,20 @@ def upload_file(self, session, filename):
return False, str(ex)
return True, 'OK'


def register(self):
import webbrowser

# get user handle
handle = self.context.params.get(service_name, 'handle')
handle = self.context.params.get(self.service_name, 'handle')
api_base_url = handle.split('@')[-1]
# get client data
client_id = self.context.params.get('mastodon', 'client_id')
client_secret = self.context.params.get('mastodon', 'client_secret')
client_id = self.context.params.get(self.service_name, 'client_id')
client_secret = self.context.params.get(self.service_name, 'client_secret')
if (not client_id) or (not client_secret):
client_id, client_secret = Mastodon.create_app(
'pywws', scopes=['write'], api_base_url=api_base_url)
self.context.params.set('mastodon', 'client_id', client_id)
self.context.params.set('mastodon', 'client_secret', client_secret)
self.context.params.set(self.service_name, 'client_id', client_id)
self.context.params.set(self.service_name, 'client_secret', client_secret)
# create api
api = Mastodon(client_id=client_id, client_secret=client_secret,
api_base_url=api_base_url)
Expand All @@ -245,7 +238,7 @@ def register(self):
code = code.strip()
# log in
access_token = api.log_in(code=code, scopes=['write'])
self.context.params.set('mastodon', 'access_token', access_token)
self.context.params.set(self.service_name, 'access_token', access_token)


if __name__ == "__main__":
Expand Down

0 comments on commit 5054dc7

Please sign in to comment.