def __init__(self, **kwargs): self.client = TumblrRestClient(**kwargs) self.dummy = { 'likes': {'liked_posts': []}, 'following': {'blogs': []}, 'posts': {'total_posts': 0, 'posts': []} }
def get_tumblr_posts(self, amount): client = TumblrRestClient( settings.TUMBLR_CONSUMER_KEY, settings.TUMBLR_CONSUMER_SECRET, settings.TUMBLR_OAUTH_TOKEN2, settings.TUMBLR_OAUTH_SECRET ) blog_name = "nadezhdanova-eng.tumblr.com" language = translation.get_language() if language == settings.LANGUAGE_CODE: blog_name = "nadezhdanova.tumblr.com" response = client.posts(blog_name, type="text", limit=amount, filter="text") tumblr_posts = response.get("posts", []) posts = [] for post in tumblr_posts: item = { "title": post.get("title", ""), "text": post.get("body", ""), "url": post.get("post_url", "") } posts.append(item) return posts
def __init__(self, conf): self.log = getLogger(__name__) self.conf = conf self.__info = None self.client = TumblrRestClient(self.conf.tumblr_consumer_key, self.conf.tumblr_consumer_secret, self.conf.tumblr_oauth_token, self.conf.tumblr_oauth_secret) self.log.debug('%s init done', __name__)
def get_client(appinfo, authinfo): client = TumblrRestClient( appinfo.consumer_key, appinfo.consumer_secret, authinfo.token, authinfo.secret ) user = client.info().get('user') logger.debug( 'connect to account %s with %d followings and %d likes', user.get('name'), user.get('following'), user.get('likes') ) return client
def pull_tumblr(post_id): client = TumblrRestClient( 'q3K5ba44O1DLAv39TDsp4fEMNY1pGAdXsiciIQQqZdAsXF54Zt', 'UnkAeG6I7PRHidVqikB3zcrL6DI94q0woJv5sIeUCj3B7ndCdJ', 'KT75Gar86W3elQAuU7VyiT9UUstfX6JAFGTvG01pJYKStqlAXN', 'ZggEOt83VSC2lzvN01SWmFJy98r13oeuWyciXxfxVqOwAvo81n' ) # print client.info() tumblr_post = client.posts('cfsacompetition.tumblr.com', type='text', id=post_id) return tumblr_post
def tumblrAuth(config, OAUTH_TUMBLR): consumer_key = config.get("tumblr", "consumer_key") consumer_secret = config.get("tumblr", "secret_key") hostname = config.get("tumblr", "blog") try: f = open(OAUTH_TUMBLR, "rb") oauth_token = pickle.load(f) f.close() except: print "Couldn't open %s, reloading..." % OAUTH_TUMBLR oauth_token = False if (not oauth_token): tumblr_oauth = TumblrOAuthClient(consumer_key, consumer_secret) authorize_url = tumblr_oauth.get_authorize_url() print "Visit: %s" % authorize_url oauth_verifier = raw_input('What is the oauth_verifier?') oauth_token = tumblr_oauth.get_access_token(oauth_verifier) print "Access key:", oauth_token.key print "Access Secret:", oauth_token.secret f = open(OAUTH_TUMBLR, "w") pickle.dump(oauth_token, f) f.close() f = open(TIMECACHE, "rb") lasttime = f.read(128) f.close() return TumblrRestClient(consumer_key, consumer_secret, oauth_token.key, oauth_token.secret)
def __init__(self, *args): self.api = TumblrRestClient(tumblrLogin["consumerkey"], tumblrLogin["consumersecret"], tumblrLogin["oauthtoken"], tumblrLogin["oauthtokensecret"]) self.clientInfo = self.api.info() self.blogs = self.clientInfo['user']['blogs'] for arg in args: if arg.find("http", 0, len(arg)) != -1: self.blog = self.findBlog(arg, True) else: self.blog = self.findBlog(arg, False) self.queue = [] self.batch = [] self.state = "draft" self.number = 0 self.name = ""
class TumblrClient: def __init__(self, **kwargs): self.client = TumblrRestClient(**kwargs) self.dummy = { 'likes': { 'liked_posts': [] }, 'following': { 'blogs': [] }, 'posts': { 'total_posts': 0, 'posts': [] } } def info(self): return self.client.info() def posts(self, **kwargs): retry = MAX_RETRY while retry: try: return self.client.posts(**kwargs) except RequestException: retry -= 1 return self.dummy['posts'] def following(self, **kwargs): retry = MAX_RETRY while retry: try: return self.client.following(**kwargs) except RequestException: retry -= 1 return self.dummy['following'] def likes(self, **kwargs): retry = MAX_RETRY while retry: try: return self.client.likes(**kwargs) except RequestException: retry -= 1 return self.dummy['likes']
def getFollowings(): _retVal = [] client = TumblrRestClient(API_KEY, SECRTY_KEY, TOKEN_KEY, TOKEN_SECRTY) _followings = client.following(limit=1) if "meta" in _followings: print "Server Return ERROR : %s,code :%s" % (_followings["msg"], _followings["status"]) return # 先确定总关注数 _total = _followings["total_blogs"] _flBlogArr = [] for offset in xrange(0, _total / 20 + 1): _followings = client.following(limit=20, offset=offset * 20) for ele in _followings["blogs"]: _flBlogArr.append(ele["uuid"]) _retVal = _flBlogArr return _retVal
def getFollowings(): _retVal = [] client = TumblrRestClient(API_KEY, SECRTY_KEY, TOKEN_KEY, TOKEN_SECRTY) _followings = client.following(limit=1) if "meta" in _followings: print "Server Return ERROR : %s,code :%s" % (_followings["msg"], _followings["status"]) return # 先确定总关注数 _total = _followings["total_blogs"] _flBlogArr = [] for offset in xrange(0, _total / 20 + 1): _followings = client.following(limit=20, offset=offset * 20) for ele in _followings["blogs"]: _flBlogArr.append(ele["uuid"]) _retVal = _flBlogArr return _retVal
def __init__(self, consumer_key: str, consumer_secret: str, oauth_token: str, oauth_secret: str): """ Summary: Initializes and instance of TumblrClient Args: consumer_key: a valid tumblr rest api application's consumer key consumer_secret: a valid tumblr rest api application's consumer secret oauth_token: a valid tumblr rest api application's oauth token oauth_secret: a valid tumblr rest api application's oauth_secret Returns: An instance of the TumblrClient class """ self.tumblr = TumblrRestClient( consumer_key = consumer_key, consumer_secret = consumer_secret, oauth_token = oauth_token, oauth_secret = oauth_secret )
def __init__(self, token=None, **kwargs): """ :param token: :param kwargs: """ super(ServiceTumblr, self).__init__(token, **kwargs) self.AUTH_URL = 'https://www.tumblr.com/oauth/authorize' self.ACC_TOKEN = 'https://www.tumblr.com/oauth/access_token' self.REQ_TOKEN = 'https://www.tumblr.com/oauth/request_token' self.consumer_key = settings.TH_TUMBLR_KEY['consumer_key'] self.consumer_secret = settings.TH_TUMBLR_KEY['consumer_secret'] self.token = token self.service = 'ServiceTumblr' self.oauth = 'oauth1' if self.token is not None: token_key, token_secret = self.token.split('#TH#') self.tumblr = TumblrRestClient(self.consumer_key, self.consumer_secret, token_key, token_secret)
def getResource(filter='photo', resKey="photos"): client = TumblrRestClient(API_KEY) _thArr = [] for url in TURL: print "start Spider-URL:%s" % url _thread = Thread(target=getData, args=[client, url, filter, resKey]) _thread.start() _thArr.append(_thread) for th in _thArr: th.join() print "all Spider finish ..."
def __init__(self, token=None, **kwargs): """ :param token: :param kwargs: """ super(ServiceTumblr, self).__init__(token, **kwargs) self.AUTH_URL = 'https://www.tumblr.com/oauth/authorize' self.ACC_TOKEN = 'https://www.tumblr.com/oauth/access_token' self.REQ_TOKEN = 'https://www.tumblr.com/oauth/request_token' self.consumer_key = settings.TH_TUMBLR_KEY['consumer_key'] self.consumer_secret = settings.TH_TUMBLR_KEY['consumer_secret'] self.token = token self.service = 'ServiceTumblr' self.oauth = 'oauth1' if self.token is not None: token_key, token_secret = self.token.split('#TH#') self.tumblr = TumblrRestClient(self.consumer_key, self.consumer_secret, token_key, token_secret)
#!/usr/bin/env python # -*- coding: utf-8 -*- from pytumblr import TumblrRestClient from objectpath import Tree import urwid client = TumblrRestClient( consumer_key="iWpkjxQeBaFBpIvHTB0RbP7G5ozicNZ5FQtiMkkoKFkiJ4Cfjb", consumer_secret="JfmKt6EJWOPdNs155kBrVC7AC8YO3V9RVJt73PcuU85E7buGsq", oauth_token="ZoegVG4B9pTAFm3lI86bSueW8KVlr0PKOkxNsh4CqmwIMnLRcE", oauth_secret="AkuR2qM8QNvEm62dQ8JbldxYicQJMvpxdOhfBQPALgsBWLrc7G", ) tree = Tree(client.dashboard(type="text")) post = u"""\ -------- Username: {blog_name} Date: {date} Post type: {type} :: {title} :: {body} Tags: {tags} Notes: {note_count} URL: {post_url} """
from pytumblr import TumblrRestClient import json from bs4 import BeautifulSoup from random import choice client = TumblrRestClient('9g1lRa75IJ7HLbnyqMCaXsSsnvyz8uUsa7ZLzyGCipFciA23PM') def tumblrSelection(category): storyposts = posts(tag=category) #storyposts = [{'body': '<p><a href="https://en.wikipedia.org/wiki/Universe">https://en.wikipedia.org/wiki/Universe</a></p><p>No, literally.</p><p>As Douglas Adams said, the universe is big. Really big</p>', 'short_url': 'https://tmblr.co/ZJc03i22P_y9r', 'tags': ['passion'], 'title': 'Astronomical Numbers', 'plain': 'https://en.wikipedia.org/wiki/UniverseNo, literally.As Douglas Adams said, the universe is big. Really big', 'date': '2016-02-26 10:38:48 GMT', 'img_url': None, 'link_url': '<a href="https://en.wikipedia.org/wiki/Universe">https://en.wikipedia.org/wiki/Universe</a>', 'post_url': 'http://itabn.tumblr.com/post/140022366837/astronomical-numbers'}, {'body': '<p><a href="https://en.wikipedia.org/wiki/Number">https://en.wikipedia.org/wiki/Number</a></p><p>It starts with counting: 1, 2, 3 are what the maths guys call the Natural Numbers. From there it gets more and more UN-natural, all the way to imaginary and beyond, to some very weird structures that still get called numbers.</p>', 'short_url': 'https://tmblr.co/ZJc03i22P_oKs', 'tags': ['passion'], 'title': 'You Call That a Number?', 'plain': 'https://en.wikipedia.org/wiki/NumberIt starts with counting: 1, 2, 3 are what the maths guys call the Natural Numbers. From there it gets more and more UN-natural, all the way to imaginary and beyond, to some very weird structures that still get called numbers.', 'date': '2016-02-26 10:37:01 GMT', 'img_url': None, 'link_url': '<a href="https://en.wikipedia.org/wiki/Number">https://en.wikipedia.org/wiki/Number</a>', 'post_url': 'http://itabn.tumblr.com/post/140022326582/you-call-that-a-number'}] stories = {} if storyposts: featureStory = storyposts[0] else: featureStory = None #todo handle zero stories feature_candidates = [] for post in storyposts: if post["featured"]: feature_candidates.append(post) featureStory = choice(feature_candidates) stories["featured"] = featureStory if featureStory: storyposts.remove(featureStory) stories["other"] = storyposts return stories def posts(**kwargs):
Username: {blog_name} Date: {date} Post type: {type} :: {title} :: {body} Tags: {tags} Notes: {note_count} URL: {post_url} """ for i in query.execute("$.posts"): print(post.format(**i)) client = TumblrRestClient( consumer_key='iWpkjxQeBaFBpIvHTB0RbP7G5ozicNZ5FQtiMkkoKFkiJ4Cfjb', consumer_secret='JfmKt6EJWOPdNs155kBrVC7AC8YO3V9RVJt73PcuU85E7buGsq', oauth_token='ZoegVG4B9pTAFm3lI86bSueW8KVlr0PKOkxNsh4CqmwIMnLRcE', oauth_secret='AkuR2qM8QNvEm62dQ8JbldxYicQJMvpxdOhfBQPALgsBWLrc7G', ) dashboard = client.dashboard(type="text") query = Query(dashboard) user_info = client.info() print("--- Welcome to the InstaCommander prototype. ---\n") print("-"*15, "\nStart using the `client` object\n", "\n", "username:"******"\n", "url:", user_info['user']['blogs'][0]['url']) show_dashboard()
class ServiceTumblr(ServicesMgr): """ Service Tumblr """ def __init__(self, token=None, **kwargs): """ :param token: :param kwargs: """ super(ServiceTumblr, self).__init__(token, **kwargs) self.AUTH_URL = 'https://www.tumblr.com/oauth/authorize' self.ACC_TOKEN = 'https://www.tumblr.com/oauth/access_token' self.REQ_TOKEN = 'https://www.tumblr.com/oauth/request_token' self.consumer_key = settings.TH_TUMBLR_KEY['consumer_key'] self.consumer_secret = settings.TH_TUMBLR_KEY['consumer_secret'] self.token = token self.service = 'ServiceTumblr' self.oauth = 'oauth1' if self.token is not None: token_key, token_secret = self.token.split('#TH#') self.tumblr = TumblrRestClient(self.consumer_key, self.consumer_secret, token_key, token_secret) def read_data(self, **kwargs): """ get the data from the service as the pocket service does not have any date in its API linked to the note, add the triggered date to the dict data thus the service will be triggered when data will be found :param kwargs: contain keyword args : trigger_id at least :type kwargs: dict :rtype: list """ trigger_id = kwargs.get('trigger_id') data = list() cache.set('th_tumblr_' + str(trigger_id), data) def save_data(self, trigger_id, **data): """ let's save the data :param trigger_id: trigger ID from which to save data :param data: the data to check to be used and save :type trigger_id: int :type data: dict :return: the status of the save statement :rtype: boolean """ from th_tumblr.models import Tumblr title, content = super(ServiceTumblr, self).save_data(trigger_id, **data) # get the data of this trigger trigger = Tumblr.objects.get(trigger_id=trigger_id) # we suppose we use a tag property for this service status = self.tumblr.create_text(blogname=trigger.blogname, title=title, body=content, state='published', tags=trigger.tag) return status def auth(self, request): """ let's auth the user to the Service :param request: request object :return: callback url :rtype: string that contains the url to redirect after auth """ request_token = super(ServiceTumblr, self).auth(request) callback_url = self.callback_url(request) # URL to redirect user to, to authorize your app auth_url_str = '{auth_url}?oauth_token={token}' auth_url_str += '&oauth_callback={callback_url}' auth_url = auth_url_str.format(auth_url=self.AUTH_URL, token=request_token['oauth_token'], callback_url=callback_url) return auth_url
from pytumblr import TumblrRestClient from xml.dom import minidom from os.path import join import time import os if __name__ == "__main__": DATA_DIR = "data" TAGS = ["#selfie", "selfie"] keyDoc = minidom.parse('keys.xml') keys = {} for i in keyDoc.getElementsByTagName('string'): keys[i.attributes['name'].value] = i.childNodes[0].data mTumblr = TumblrRestClient(keys['consumer_key'], keys['consumer_secret'], keys['oauth_token'], keys['oauth_token_secret']) for directory in [ d for d in os.listdir(DATA_DIR) if os.path.isdir(join(DATA_DIR, d)) ]: for filename in [ f for f in os.listdir(join(DATA_DIR, directory)) if f.startswith("memememeselfie") and f.endswith(".jpg") ]: fullPath = join(DATA_DIR, directory, filename) print "POSTING %s" % fullPath mTumblr.create_photo(directory, state="published", tags=TAGS, data=fullPath) time.sleep(1)
import string import time from datetime import datetime from urlparse import parse_qs from django.conf import settings from django.shortcuts import render from instagram.client import InstagramAPI from apps.worker.models import GoogleImages from pytumblr import TumblrRestClient # creating client for tumbler tumblr_client = TumblrRestClient(consumer_key=settings.TUMBLR_KEY, consumer_secret=settings.TUMBLR_SECRET) # creating client for instagram inst_client = InstagramAPI(client_id=settings.INSTAGRAM_ID) def make_token(L = 10): return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(L)) def index(request): return render(request, 'index.html', {}) def google(request):
# Get Consumer key and secret from Tumblr_Keys Keys = eval(open("Tumblr_Keys", "r").read()) # Generate Oauth tokens. Taken from interactive_console.py in pytumblr yaml_path = os.path.expanduser("~") + "/.tumblr" if not os.path.exists(yaml_path): tokens = new_oauth(yaml_path, Keys["consumer_key"], Keys["consumer_secret"]) else: yaml_file = open(yaml_path, "r") tokens = yaml.safe_load(yaml_file) yaml_file.close() client = TumblrRestClient( tokens["consumer_key"], tokens["consumer_secret"], tokens["oauth_token"], tokens["oauth_token_secret"] ) # print client.info() # Read Captions, List of Files fout = open("Captions.csv", "r") data = fout.readlines() fout.close() ImageList = [] if not SortByTime: for i in data[1:]: ImagePath = os.path.join(Image_Folder, i.split(FileColumnSeparator)[0]) Caption = i.split(FileColumnSeparator)[1] + " <br> " + exif.get_exif_string(ImagePath) Tags = i.rstrip().split(FileColumnSeparator)[2].split(FileTagsSeparator)
def __init__(self, oauth_token, oauth_token_secret, blogname): TumblrRestClient.__init__(self, TumblrAPI.CLIENT_KEY, TumblrAPI.CLIENT_SECRET, oauth_token, oauth_token_secret) self.blogname = blogname or f"{self.info()['user']['name']}.tumblr.com"
from pytumblr import TumblrRestClient from picamera import PiCamera from datetime import datetime from time import sleep from auth import ( consumer_key, consumer_secret, oauth_token, oauth_secret ) client = TumblrRestClient( consumer_key, consumer_secret, oauth_token, oauth_secret ) info = client.info() try: username = info["user"]["name"] except KeyError: raise RuntimeError("Could not connect to Tumblr. Check auth keys.") PHOTOS_DIR = '/home/pi/photos' def take_picture(): with PiCamera() as camera: timestamp = datetime.now().isoformat() photo_path = '%s/%s.jpg' % (PHOTOS_DIR, timestamp)
#Tumblr API import os from mimetypes import guess_type from __init__ import tumblrLogin from tumblpy import Tumblpy import tumblr_photoset_upload api = Tumblpy(tumblrLogin["consumerkey"] , tumblrLogin["consumersecret"], tumblrLogin["oauthtoken"], tumblrLogin["oauthtokensecret"]) from pytumblr import TumblrRestClient api = TumblrRestClient(tumblrLogin["consumerkey"] , tumblrLogin["consumersecret"], tumblrLogin["oauthtoken"], tumblrLogin["oauthtokensecret"]) blog_url = api.info() print(blog_url) blogName = blog_url['user']['blogs'][0]['name'] def posts(directory, caption, tags, state): print ("Posting to: " + blog_url) photo = open(directory, 'rb') params = {'type':'photo', 'caption': caption, 'data': [photo, photo], 'state': state, 'tags': tags} post = api.post('post', blog_url=blog_url, params=params) print ("Post ID: " + str(post['id'])) # returns id if posted successfully #Creates a photo post using a source URL api.create_photo(blogName, state="draft", tags=["testing", "ok"],
class Blog(object): def __init__(self, conf): self.log = getLogger(__name__) self.conf = conf self.__info = None self.client = TumblrRestClient(self.conf.tumblr_consumer_key, self.conf.tumblr_consumer_secret, self.conf.tumblr_oauth_token, self.conf.tumblr_oauth_secret) self.log.debug('%s init done', __name__) @property def info(self): if self.__info is None: self.log.debug('request blog info for "%s"', self.conf.tumblr_blog_name) self.__info = [ blog for blog in self.client.info()['user']['blogs'] if blog['name'] == self.conf.tumblr_blog_name ][-1] return self.__info def log_post(self, post, text): self.log.info('\n '.join([ 'tumblr post ->', text, '\n', pformat(post.get('posts')), ])) def post_photo(self, source, *, title, caption, public, tags=[]): post = self.client.create_photo( self.info['name'], caption=caption, format='markdown', slug=title, source=source, state=('published' if public else 'draft'), tags=tags) self.log_post(post, 'photo') return self.client.posts(self.info['name'], id=post['id']).get('posts', [None])[0] def post_video(self, embed, *, title, caption, public, tags=[]): post = self.client.create_video( self.info['name'], caption=caption, embed=embed, format='markdown', slug=title, state=('published' if public else 'draft'), tags=tags) self.log_post(post, 'video') return self.client.posts(self.info['name'], id=post['id']).get('posts', [None])[0] def pull_photos(self): for offset in range(0, self.info['total_posts'], 20): for post in self.client.posts(self.info['name'], type='photo', offset=offset, limit=20)['posts']: yield post
# -*- coding: utf-8 -*- from pytumblr import TumblrRestClient from xml.dom import minidom from os.path import join import time import os if __name__ == "__main__": DATA_DIR = "data" TAGS = ["#selfie", "selfie"] keyDoc = minidom.parse("keys.xml") keys = {} for i in keyDoc.getElementsByTagName("string"): keys[i.attributes["name"].value] = i.childNodes[0].data mTumblr = TumblrRestClient( keys["consumer_key"], keys["consumer_secret"], keys["oauth_token"], keys["oauth_token_secret"] ) for directory in [d for d in os.listdir(DATA_DIR) if os.path.isdir(join(DATA_DIR, d))]: for filename in [ f for f in os.listdir(join(DATA_DIR, directory)) if f.startswith("memememeselfie") and f.endswith(".jpg") ]: fullPath = join(DATA_DIR, directory, filename) print "POSTING %s" % fullPath mTumblr.create_photo(directory, state="published", tags=TAGS, data=fullPath) time.sleep(1) os.remove(fullPath)
class TumblrClient(AbstractSocialClient): """ Summary: The TumblrClient class wraps an instance of the pytumblr.TumblrRestClient class. This class has built in support for common tasks like pagination and keyword matching to extract relevant data points for the Tumblr platform. """ def __init__(self, consumer_key: str, consumer_secret: str, oauth_token: str, oauth_secret: str): """ Summary: Initializes and instance of TumblrClient Args: consumer_key: a valid tumblr rest api application's consumer key consumer_secret: a valid tumblr rest api application's consumer secret oauth_token: a valid tumblr rest api application's oauth token oauth_secret: a valid tumblr rest api application's oauth_secret Returns: An instance of the TumblrClient class """ self.tumblr = TumblrRestClient( consumer_key = consumer_key, consumer_secret = consumer_secret, oauth_token = oauth_token, oauth_secret = oauth_secret ) def get_page(self, sourceName: str) -> (list, list): """ Summary: Gets the first page of results and a link to the next page of results for a top level page attribute of facebook. Args: sourceName: the name of the social media page to be searched for data Returns: dataPage: a list of individual data points taken from the api response nextPageLink: a list with info needed to link to the next page of data """ rawData = self.tumblr.posts(sourceName, limit=50, offset=0, notes_info=True) dataPage = rawData['posts'] nextPageLink = [sourceName, 50] return dataPage, nextPageLink def update_page(self, nextPageLink: list) -> (list, list): """ Summary: Updates the current data page to provide new data in the executing loop (search). Gets a list of data results and a link to the next page of data. Args: nextPageLink: a link to the next page of data results Returns: dataPage: a list of individual data points taken from the api response nextPageLink: a list with info needed to link to the next page of data """ rawData = self.tumblr.posts(nextPageLink[0], limit=50, offset=nextPageLink[1], notes_info=True) dataPage = rawData['posts'] nextPageLink = [nextPageLink[0], nextPageLink[1] + 50] return dataPage, nextPageLink def match(self, searchTerm: str, datum: dict) -> bool: """ Summary: Gathers any secondary information that is relevant to the social data point and updates the data point with that information. Args: datum: the data point to be updated with secondary information Returns: datum: the data point updated with secondary information """ searchTerm = searchTerm.lower() validFilter = datum.keys() jsonAttributes = ["summary"] validAttributes = [attribute for attribute in jsonAttributes if attribute in validFilter] for attribute in validAttributes: if searchTerm in datum[attribute].lower(): return True return False pass def parse(self, datum: dict) -> dict: """ Summary: Used to parse api response and add any additional data that is relevant to the response. Args: datum: the datapoint to be parsed and updated Returns: datum: the parsed data dictionary with secondary information added """ datum = { "type": datum["type"], "blog_name": datum["blog_name"], "id": datum["id"], "date": datum["date"], "post_url": datum["post_url"], "summary": datum["summary"], "note_count": datum["note_count"], "notes": datum["notes"] } return datum def get_secondary_information(self, datum: dict) -> dict: """ Summary: Gathers any secondary information that is relevant to the social data point and updates the data point with that information. Args: datum: the data point to be updated with secondary information Returns: datum: the data point updated with secondary information """ pass def search(self, searchTerm: str, sources: list, limit: int) -> list: return search(client=self, searchTerm=searchTerm, sources=sources, limit=limit)
class ServiceTumblr(ServicesMgr): """ Service Tumblr """ def __init__(self, token=None, **kwargs): """ :param token: :param kwargs: """ super(ServiceTumblr, self).__init__(token, **kwargs) self.AUTH_URL = 'https://www.tumblr.com/oauth/authorize' self.ACC_TOKEN = 'https://www.tumblr.com/oauth/access_token' self.REQ_TOKEN = 'https://www.tumblr.com/oauth/request_token' self.consumer_key = settings.TH_TUMBLR_KEY['consumer_key'] self.consumer_secret = settings.TH_TUMBLR_KEY['consumer_secret'] self.token = token self.service = 'ServiceTumblr' self.oauth = 'oauth1' if self.token is not None: token_key, token_secret = self.token.split('#TH#') self.tumblr = TumblrRestClient(self.consumer_key, self.consumer_secret, token_key, token_secret) def read_data(self, **kwargs): """ get the data from the service as the pocket service does not have any date in its API linked to the note, add the triggered date to the dict data thus the service will be triggered when data will be found :param kwargs: contain keyword args : trigger_id at least :type kwargs: dict :rtype: list """ trigger_id = kwargs.get('trigger_id') data = list() kwargs['model_name'] = 'Tumblr' kwargs['app_label'] = 'th_tumblr' super(ServiceTumblr, self).read_data(**kwargs) cache.set('th_tumblr_' + str(trigger_id), data) return data def save_data(self, trigger_id, **data): """ let's save the data :param trigger_id: trigger ID from which to save data :param data: the data to check to be used and save :type trigger_id: int :type data: dict :return: the status of the save statement :rtype: boolean """ from th_tumblr.models import Tumblr title, content = super(ServiceTumblr, self).save_data(trigger_id, **data) # get the data of this trigger trigger = Tumblr.objects.get(trigger_id=trigger_id) # we suppose we use a tag property for this service status = self.tumblr.create_text(blogname=trigger.blogname, title=title, body=content, state='published', tags=trigger.tag) return status def auth(self, request): """ let's auth the user to the Service :param request: request object :return: callback url :rtype: string that contains the url to redirect after auth """ request_token = super(ServiceTumblr, self).auth(request) callback_url = self.callback_url(request) # URL to redirect user to, to authorize your app auth_url_str = '{auth_url}?oauth_token={token}' auth_url_str += '&oauth_callback={callback_url}' auth_url = auth_url_str.format(auth_url=self.AUTH_URL, token=request_token['oauth_token'], callback_url=callback_url) return auth_url
class Queues(): #Either blog name or url def __init__(self, *args): self.api = TumblrRestClient(tumblrLogin["consumerkey"], tumblrLogin["consumersecret"], tumblrLogin["oauthtoken"], tumblrLogin["oauthtokensecret"]) self.clientInfo = self.api.info() self.blogs = self.clientInfo['user']['blogs'] for arg in args: if arg.find("http", 0, len(arg)) != -1: self.blog = self.findBlog(arg, True) else: self.blog = self.findBlog(arg, False) self.queue = [] self.batch = [] self.state = "draft" self.number = 0 self.name = "" def findBlog(self, arg, url): blogNumber = 0 read = True while read == True: try: if url: if self.blogs[blogNumber]['url'] == arg: return self.blogs[blogNumber]['name'] else: if self.blogs[blogNumber]['name'] == arg: return self.blogs[blogNumber]['name'] blogNumber = blogNumber + 1 except IndexError: read = False print("Blog not found, check your URL or name") #Add CSV to queue def csvQueue(self, csvFile): with open(csvFile, newline='', encoding='utf-8') as csvfile: reader = reader(csvfile, delimiter=',', quotechar='|') for row in reader: if row == "": print("Skipping Empty Row") else: self.addImages(row[0]) self.numberImages = self.numberImages + 1 def addImages(self, overwrite=False, *args, **kwargs): for arg in args: if isinstance(arg, pixivImage): if overwrite: self.queue = [arg] else: self.queue.append(arg) for kwarg in kwargs.keys(): self.csvQueue(kwargs[kwarg]) #Adds caption text, or keyworded text to be added when posted def link(self, text, link): return "<a href={}>{}</a>".format(link, text) #Takes dictionary or csv as {"trigger":"Tag to be added when triggered"} def tagTriggers( self, triggers, csv=False, overwrite=False, batch=None, ): tempTags = [] if batch == None: batch = self.queue if csv: with open(triggers, newline='', encoding='utf-8') as csvfile: csvReader = reader(csvfile, delimiter=',', quotechar='|') triggers = {} for row in csvReader: triggers[row[0]] = row[1] for image in batch: image.importIllustJSON() totaltags = image.tags for tag in image.userTags: totaltags.append(tag) for trigger in triggers.keys(): for tag in totaltags: if trigger == tag: tempTags.append(triggers[tag]) if overwrite: image.setCustomTags(tempTags) else: for tag in image.userTags: tempTags.append(tag) image.setCustomTags(tempTags) #Adds default caption def addCaption(self): for image in self.queue: image.setCaption("{} by {}".format( self.link(image.title, image.URL), self.link(image.name, image.user_URL))) def formatCaptions(self, text, *args): caption_format = args for image in self.queue: try: print(text) print(caption_format) print(text.format(caption_format)) image.setCaption(text.format(caption_format)) except AttributeError: try: self.getArtist(pixivImage) image.setCaption(text.format(caption_format)) except: print("Invalid arguments for caption") #Manually artist information for pixivImage def getArtistWebsite(self, pixivImage): website = pixivImage.webpage if website.find("twittee") != -1: return "https://twitter.com/intent/follow?user_id=" + pixivImage.twitter_id elif website.find("tumblr") != -1: username = self.api.blog_info(website)['name'] return "https://www.tumblr.com/follow/" + username else: pass def postList(self, images, blogName, state): rowcurrent = 0 rowsum = len(images) for image in images: rowcurrent = rowcurrent + 1 try: imageCaption = image.caption except AttributeError: imageCaption = "" print("({}/{}) Downloading: {} by {}".format( rowcurrent, rowsum, image.title, image.name)) if image.directories == []: image.download() print("Posting to " + blogName + " as " + state) print("Caption: " + imageCaption) print("Tags: " + str(image.userTags)) self.api.create_photo(blogName, state=state, format="markdown", tags=image.userTags, caption=str(image.caption), data=image.directories) print("") if rowcurrent == rowsum: print("Done!") def postAll(self, state): self.postList(self.queue, self.blog, state)
def delete_post(self, post_id): TumblrRestClient.delete_post(self, self.blogname, post_id)