[tumblr] add support for OAuth authentication (#65)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2017 Mike Fährmann
|
||||
# Copyright 2017-2018 Mike Fährmann
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License version 2 as
|
||||
@@ -9,8 +9,8 @@
|
||||
"""Utility classes to setup OAuth and link a users account to gallery-dl"""
|
||||
|
||||
from .common import Extractor, Message
|
||||
from . import deviantart, flickr, reddit
|
||||
from .. import util
|
||||
from . import deviantart, flickr, reddit, tumblr
|
||||
from .. import text, util
|
||||
import os
|
||||
import urllib.parse
|
||||
|
||||
@@ -71,6 +71,32 @@ class OAuthBase(Extractor):
|
||||
print(url, end="\n\n", flush=True)
|
||||
return self.recv()
|
||||
|
||||
def _oauth1_authorization_flow(
|
||||
self, request_token_url, authorize_url, access_token_url):
|
||||
"""Perform the OAuth 1.0a authorization flow"""
|
||||
del self.session.params["oauth_token"]
|
||||
|
||||
# Get a Request Token
|
||||
params = {"oauth_callback": self.redirect_uri}
|
||||
data = self.session.get(request_token_url, params=params).text
|
||||
|
||||
data = text.parse_query(data)
|
||||
self.session.params["oauth_token"] = token = data["oauth_token"]
|
||||
self.session.token_secret = data["oauth_token_secret"]
|
||||
|
||||
# Get the User's Authorization
|
||||
params = {"oauth_token": token, "perms": "read"}
|
||||
data = self.open(authorize_url, params)
|
||||
|
||||
# Exchange the Request Token for an Access Token
|
||||
data = self.session.get(access_token_url, params=data).text
|
||||
|
||||
data = text.parse_query(data)
|
||||
self.send(OAUTH1_MSG_TEMPLATE.format(
|
||||
category=self.subcategory,
|
||||
token=data["oauth_token"],
|
||||
token_secret=data["oauth_token_secret"]))
|
||||
|
||||
def _oauth2_authorization_code_grant(
|
||||
self, client_id, client_secret, auth_url, token_url, scope):
|
||||
"""Perform an OAuth2 authorization code grant"""
|
||||
@@ -120,23 +146,6 @@ class OAuthBase(Extractor):
|
||||
))
|
||||
|
||||
|
||||
class OAuthReddit(OAuthBase):
|
||||
subcategory = "reddit"
|
||||
pattern = ["oauth:reddit$"]
|
||||
|
||||
def items(self):
|
||||
yield Message.Version, 1
|
||||
|
||||
self.session.headers["User-Agent"] = reddit.RedditAPI.USER_AGENT
|
||||
self._oauth2_authorization_code_grant(
|
||||
reddit.RedditAPI.CLIENT_ID,
|
||||
"",
|
||||
"https://www.reddit.com/api/v1/authorize",
|
||||
"https://www.reddit.com/api/v1/access_token",
|
||||
"read",
|
||||
)
|
||||
|
||||
|
||||
class OAuthDeviantart(OAuthBase):
|
||||
subcategory = "deviantart"
|
||||
pattern = ["oauth:deviantart$"]
|
||||
@@ -162,35 +171,79 @@ class OAuthFlickr(OAuthBase):
|
||||
OAuthBase.__init__(self, match)
|
||||
self.session = util.OAuthSession(
|
||||
self.session,
|
||||
flickr.FlickrAPI.API_KEY, flickr.FlickrAPI.API_SECRET
|
||||
flickr.FlickrAPI.API_KEY,
|
||||
flickr.FlickrAPI.API_SECRET,
|
||||
)
|
||||
del self.session.params["oauth_token"]
|
||||
|
||||
def items(self):
|
||||
yield Message.Version, 1
|
||||
|
||||
# Get a Request Token
|
||||
url = "https://www.flickr.com/services/oauth/request_token"
|
||||
params = {"oauth_callback": self.redirect_uri}
|
||||
data = self.session.get(url, params=params).text
|
||||
self._oauth1_authorization_flow(
|
||||
"https://www.flickr.com/services/oauth/request_token",
|
||||
"https://www.flickr.com/services/oauth/authorize",
|
||||
"https://www.flickr.com/services/oauth/access_token",
|
||||
)
|
||||
|
||||
data = urllib.parse.parse_qs(data)
|
||||
self.session.params["oauth_token"] = token = data["oauth_token"][0]
|
||||
self.session.token_secret = data["oauth_token_secret"][0]
|
||||
|
||||
# Get the User's Authorization
|
||||
url = "https://www.flickr.com/services/oauth/authorize"
|
||||
params = {"oauth_token": token, "perms": "read"}
|
||||
data = self.open(url, params)
|
||||
class OAuthReddit(OAuthBase):
|
||||
subcategory = "reddit"
|
||||
pattern = ["oauth:reddit$"]
|
||||
|
||||
# Exchange the Request Token for an Access Token
|
||||
url = "https://www.flickr.com/services/oauth/access_token"
|
||||
data = self.session.get(url, params=data).text
|
||||
def items(self):
|
||||
yield Message.Version, 1
|
||||
|
||||
data = urllib.parse.parse_qs(data)
|
||||
self.send(FLICKR_MSG_TEMPLATE.format(
|
||||
token=data["oauth_token"][0],
|
||||
token_secret=data["oauth_token_secret"][0]))
|
||||
self.session.headers["User-Agent"] = reddit.RedditAPI.USER_AGENT
|
||||
self._oauth2_authorization_code_grant(
|
||||
reddit.RedditAPI.CLIENT_ID,
|
||||
"",
|
||||
"https://www.reddit.com/api/v1/authorize",
|
||||
"https://www.reddit.com/api/v1/access_token",
|
||||
"read",
|
||||
)
|
||||
|
||||
|
||||
class OAuthTumblr(OAuthBase):
|
||||
subcategory = "tumblr"
|
||||
pattern = ["oauth:tumblr$"]
|
||||
|
||||
def __init__(self, match):
|
||||
OAuthBase.__init__(self, match)
|
||||
self.session = util.OAuthSession(
|
||||
self.session,
|
||||
tumblr.TumblrAPI.API_KEY,
|
||||
tumblr.TumblrAPI.API_SECRET,
|
||||
)
|
||||
|
||||
def items(self):
|
||||
yield Message.Version, 1
|
||||
|
||||
self._oauth1_authorization_flow(
|
||||
"https://www.tumblr.com/oauth/request_token",
|
||||
"https://www.tumblr.com/oauth/authorize",
|
||||
"https://www.tumblr.com/oauth/access_token",
|
||||
)
|
||||
|
||||
|
||||
OAUTH1_MSG_TEMPLATE = """
|
||||
Your Access Token and Access Token Secret are
|
||||
|
||||
{token}
|
||||
{token_secret}
|
||||
|
||||
Put these values into your configuration file as
|
||||
'extractor.{category}.access-token' and
|
||||
'extractor.{category}.access-token-secret'.
|
||||
|
||||
Example:
|
||||
{{
|
||||
"extractor": {{
|
||||
"{category}": {{
|
||||
"access-token": "{token}",
|
||||
"access-token-secret": "{token_secret}"
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
|
||||
|
||||
OAUTH2_MSG_TEMPLATE = """
|
||||
@@ -210,23 +263,3 @@ Example:
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
|
||||
FLICKR_MSG_TEMPLATE = """
|
||||
Your Access Token and Access Token Secret are
|
||||
|
||||
{token}
|
||||
{token_secret}
|
||||
|
||||
Put these values into your configuration file as
|
||||
'extractor.flickr.access-token' and 'extractor.flickr.access-token-secret'.
|
||||
|
||||
Example:
|
||||
{{
|
||||
"extractor": {{
|
||||
"flickr": {{
|
||||
"access-token": "{token}",
|
||||
"access-token-secret": "{token_secret}"
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user