コード例 #1
0
    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
コード例 #2
0
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
コード例 #3
0
ファイル: tumblr.py プロジェクト: s884812/tumblr-downloader
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']
コード例 #4
0
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
コード例 #5
0
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)