Ejemplo n.º 1
0
 def __init__(self,
              consumer_key=None,
              consumer_secret=None,
              oauth_token=None,
              oauth_token_secret=None):
     if consumer_key is None or consumer_secret is None or oauth_token is None or oauth_token_secret is None:
         self.t = Tumblpy()
     else:
         self.t = Tumblpy(consumer_key, consumer_secret, oauth_token,
                          oauth_token_secret)
Ejemplo n.º 2
0
def get_tumblpy():
    keyfile = 'keys.json'

    keys = {}
    if P.exists(keyfile):
        print 'Using saved OAuth keys. If there is an error, remove %s' % keyfile
        keys = json.load(open(keyfile))
    else:
        print 'Must complete OAuth steps first'

    save_keys = lambda: json.dump(keys, open(keyfile, 'w'))

    if not keys.get("OAUTH_CONSUMER_KEY"):
        print("Register an app at https://www.tumblr.com/oauth/apps .")
        print("Then input the given keys here.")
        keys['OAUTH_CONSUMER_KEY'] = raw_input("OAuth consumer key: ")
        keys['OAUTH_SECRET_KEY'] = raw_input("OAuth secret key: ")
        save_keys()

    if not keys.get("OAUTH_TOKEN"):
        t = Tumblpy(keys['OAUTH_CONSUMER_KEY'], keys['OAUTH_SECRET_KEY'])

        auth_props = t.get_authentication_tokens(
            callback_url='http://example.com/')
        auth_url = auth_props['auth_url']

        keys['OAUTH_TOKEN_SECRET'] = auth_props['oauth_token_secret']

        print 'Visit this URL: %s' % auth_url
        print 'Paste the redirect URL here:'
        redirect_url = raw_input('Redirect URL: ')
        res = parse_qs(redirect_url.split("?", 1)[1])
        keys['OAUTH_TOKEN'] = res['oauth_token'][0].split("#")[0]
        keys['OAUTH_VERIFIER'] = res['oauth_verifier'][0].split("#")[0]
        save_keys()

    if not keys.get('FINAL_OAUTH_TOKEN'):
        t = Tumblpy(keys['OAUTH_CONSUMER_KEY'], keys['OAUTH_SECRET_KEY'],
                    keys['OAUTH_TOKEN'], keys['OAUTH_TOKEN_SECRET'])

        authorized_tokens = t.get_authorized_tokens(keys['OAUTH_VERIFIER'])

        keys['FINAL_OAUTH_TOKEN'] = authorized_tokens['oauth_token']
        keys['FINAL_OAUTH_SECRET'] = authorized_tokens['oauth_token_secret']
        save_keys()

        print 'OAuth complete!'

    return Tumblpy(keys['OAUTH_CONSUMER_KEY'], keys['OAUTH_SECRET_KEY'],
                   keys['FINAL_OAUTH_TOKEN'], keys['FINAL_OAUTH_SECRET'])
Ejemplo n.º 3
0
def post():
    YOUR_CONSUMER_KEY = ''
    YOUR_CONSUMER_SECRET = ''
    OAUTH_TOKEN = ''
    OAUTH_TOKEN_SECRET = ''

    # Get the final tokens from the database or wherever you have them stored

    t = Tumblpy(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, OAUTH_TOKEN,
                OAUTH_TOKEN_SECRET)

    # Print out the user info, let's get the first blog url...
    blog_url = t.post('user/info')
    blog_url = blog_url['user']['blogs'][0]['url']
    print blog_url

    # Assume you are using the blog_url and Tumblpy instance from the previous sections
    source = 'http://www.factslides.com/imgs/lion2.jpg'
    state = 'published'
    type = 'photo'
    caption = 'TESSSTT CAPP'
    tags = ' "yay", "bay" '

    post = t.post('post',
                  blog_url=blog_url,
                  params={
                      'type': type,
                      'state': state,
                      'caption': caption,
                      'source': source,
                      'tags': tags
                  })
    print post
    return post
Ejemplo n.º 4
0
def post_images(status):
    """
    画像を作品のタグと引用付きで Tumblr に Post する
    :param status: Tweepy の status オブジェクト
    :return: なし
    """
    BLOG_URL = "mangatime-kirara.tumblr.com"
    caption = status.full_text + "\n\n" + "Source: https://" + status.extended_entities[
        "media"][0]["display_url"]
    tags = "manga,manga time kirara,まんがタイムきらら," + extract_titles(status)
    params = {
        "type": "photo",
        "caption": caption,
        "tags": tags,
    }

    image_urls = []
    for i, entry in enumerate(status.extended_entities["media"]):
        image_urls.append(entry["media_url"] + ":orig")  # original size

    for i, url in enumerate(image_urls):
        params["data[{}]".format(i)] = urllib.request.urlopen(url)

    with open("tumblr_config.txt", "r") as f:
        ck, cs, token, token_secret = f.read().strip().split()

    t = Tumblpy(ck, cs, token, token_secret)
    t.post('post', blog_url=BLOG_URL, params=params)
Ejemplo n.º 5
0
def post(request):
    user = request.user
    # check access token. If no access token, redirect to Oauth page.
    try:
        token = TumblrToken.objects.get(user=user,
                                        apikey=settings.TUMBLR_CONSUMER_KEY)
    except TumblrToken.DoesNotExist:
        return HttpResponseRedirect(reverse("tumblr_auth"))

    final_oauth_token, final_oauth_token_secret = token.get_oauth_token()

    t = Tumblpy(app_key=settings.TUMBLR_CONSUMER_KEY,
                app_secret=settings.TUMBLR_SECRET_KEY,
                oauth_token=final_oauth_token,
                oauth_token_secret=final_oauth_token_secret)
    try:
        # get default blog url.
        blog_url = t.post('user/info')
        blog_url = blog_url['user']['blogs'][0]['url']

        # post blog
        post = t.post('post',
                      blog_url=blog_url,
                      params={
                          'title': 'A new blog',
                          'body': 'This is blog content.'
                      })
    except TumblpyAuthError:
        return HttpResponseRedirect(reverse("tumblr_auth"))

    return HttpResponse("Blog post successfully.")
Ejemplo n.º 6
0
    def postJson(self, blog_url, tags, params):
        # Get the final tokens from the database or wherever you have them stored
        t = Tumblpy(self.YOUR_CONSUMER_KEY, self.YOUR_CONSUMER_SECRET,
                    self.OAUTH_TOKEN, self.OAUTH_TOKEN_SECRET)

        # let's get the first blog url...
        blog_url = t.post('user/info')
        blog_url = blog_url['user']['blogs'][0]['url']

        print blog_url
        dict_params = json.loads(params)

        print 'Posts length: ' + str(len(dict_params))

        for i in range(0, len(dict_params)):
            try:
                source = dict_params[i]['post']['source']
                caption = dict_params[i]['post']['caption']
                params = {
                    'type': 'photo',
                    'state': 'published',
                    'caption': caption,
                    'source': source,
                    'data': '',
                    'tags': tags + ',' + caption
                }

                post = t.post('post', blog_url=blog_url, params=params)
                raw_input("Downloading....")
            except Exception as e:
                print '------------------------------------------'
                print(e)
                print source

        return post
Ejemplo n.º 7
0
	def getToken(self):
		t = Tumblpy(
			settings['tum']['consumer_key'],
			settings['tum']['consumer_secret'],
			settings['tum']['token'],
			settings['tum']['token_secret'],
			)
		return t
Ejemplo n.º 8
0
def load_config():
    config = utils.load_config('tumbl_config')
    consumer_key = config[':consumer_key'] 
    consumer_secret = config[':consumer_secret'] 
    token = config[':token'] 
    token_secret = config[':token_secret'] 
    default_n = config[':default_n']
    t = Tumblpy(consumer_key,consumer_secret, token, token_secret)
    blog_url = t.post('user/info')['user']['blogs'][0]['url']
    return t, default_n, blog_url
Ejemplo n.º 9
0
    def post(self, blog_url, params):
        # Get the final tokens from the database or wherever you have them stored
        t = Tumblpy(self.YOUR_CONSUMER_KEY, self.YOUR_CONSUMER_SECRET,
                    self.OAUTH_TOKEN, self.OAUTH_TOKEN_SECRET)

        # let's get the first blog url...
        blog_url = t.post('user/info')
        blog_url = blog_url['user']['blogs'][0]['url']

        post = t.post('post', blog_url=blog_url, params=params)
        return post
Ejemplo n.º 10
0
def do_flush_key(key):
    spider_log.info("正在刷新Key ID:{}".format(key.id))
    t = Tumblpy(key.ConsumerKey, key.ConsumerSecret)
    auth_props = t.get_authentication_tokens()
    key.Token = auth_props.get("oauth_token")
    key.TokenSecret = auth_props.get("oauth_token_secret")
    spider_log.info("请打开下面的链接执行授权")
    spider_log.info(auth_props.get("auth_url"))
    key.UpdateTime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    t.client.close()
    session.commit()
    spider_log.info("刷新Key ID:{} 完成".format(key.id))
Ejemplo n.º 11
0
def authorize():
    print("func authorize")
    t = Tumblpy(CONSUMER_KEY, CONSUMER_SECRET)

    auth_props = t.get_authentication_tokens()
    auth_url = auth_props['auth_url']

    token = auth_props['oauth_token']
    token_secret = auth_props['oauth_token_secret']

    print("auth_url: ", auth_url)
    print("token: ", token)
    print("token_secret: ", token_secret)
Ejemplo n.º 12
0
def _post_to_tumblr():
    """
    Handles the POST to Tumblr.
    """
    def clean(string):
        """
        Formats a string all pretty.
        """
        return string.replace('-', ' ').replace("id ", "I'd ").replace("didnt", "didn't").replace('i ', 'I ')

    # Request is a global. Import it down here where we need it.
    from flask import request

    def strip_html(value):
        """
        Strips HTML from a string.
        """
        return re.compile(r'</?\S([^=]*=(\s*"[^"]*"|\s*\'[^\']*\'|\S*)|[^>])*?>', re.IGNORECASE).sub('', value)

    def strip_breaks(value):
        """
        Converts newlines, returns and other breaks to <br/>.
        """
        value = re.sub(r'\r\n|\r|\n', '\n', value)
        return value.replace('\n', '<br />')

    caption = u"<p class='intro'>Dear Mr. President,</p><p class='voted' data-vote-type='%s'>%s.</p><p class='message'>%s</p><p class='signature-name'>Signed,<br/>%s from %s</p><p class='footnote'>What do <em>you</em> want President Obama to remember in his second term? Share your message at <a href='http://inauguration2013.tumblr.com/'>NPR's Dear Mr. President</a>.</p>" % (
        request.form['voted'],
        clean(request.form['voted']),
        strip_breaks(strip_html(request.form['message'])),
        strip_html(request.form['signed_name']),
        strip_html(request.form['location'])
    )

    t = Tumblpy(
        app_key=app_config.TUMBLR_KEY,
        app_secret=os.environ['TUMBLR_APP_SECRET'],
        oauth_token=os.environ['TUMBLR_OAUTH_TOKEN'],
        oauth_token_secret=os.environ['TUMBLR_OAUTH_TOKEN_SECRET'])

    try:
        tumblr_post = t.post('post', blog_url=app_config.TUMBLR_URL, params={
            'type': 'photo',
            'caption': caption,
            'tags': u"%s" % request.form['voted'].replace('-', ''),
            'data': request.files['image']
        })
    except:
        return 'Sorry, we\'re probably over capacity. Please try again later.'

    return redirect(u"http://%s/%s#posts" % (app_config.TUMBLR_URL, tumblr_post['id']), code=301)
Ejemplo n.º 13
0
 def tumblr__init(self, data_=None):
     print('initTumblr')
     with open('tumblr_credentials.json', 'r') as f:
         self.tumblr_key = JLoad(f)
     self.tumblr = Tumblpy(self.tumblr_key['consumer_key'],
                           self.tumblr_key['consumer_secret'],
                           self.tumblr_key['oauth_token'],
                           self.tumblr_key['oauth_token_secret'],
                           proxies={
                               "http": self.proxies,
                               "https": self.proxies
                           })
     self.__putGui('tumblr', 'statusBar', {'text': '获取图片列表'})
     self.__tumblr__getImgList()
     return self.tumblr__getDashboards()
Ejemplo n.º 14
0
def auth(request):
    t = Tumblpy(app_key=settings.TUMBLR_CONSUMER_KEY,
                app_secret=settings.TUMBLR_SECRET_KEY)
    auth_props = t.get_authentication_tokens(
        callback_url="http://tumblrdeneme.pythonanywhere.com/callback/")
    auth_url = auth_props['auth_url']

    oauth_token = auth_props['oauth_token']
    oauth_token_secret = auth_props['oauth_token_secret']

    request.session["oauth_token"] = oauth_token
    request.session["oauth_token_secret"] = oauth_token_secret

    return redirect("http://www.tumblr.com/oauth/authorize?oauth_token=%s" %
                    oauth_token)
Ejemplo n.º 15
0
def write_mr_president_test_posts():
    """
    Writes test posts to our test blog as defined by app_config.py
    """

    # This is how many posts will be written.
    TOTAL_NUMBER = 15

    def clean(string):
        """
        Formats a string all pretty.
        """
        return string.replace('-', ' ').replace("id ", "I'd ").replace("didnt", "didn't").replace('i ', 'I ')

    t = Tumblpy(
            app_key=app_config.TUMBLR_KEY,
            app_secret=os.environ['TUMBLR_APP_SECRET'],
            oauth_token=os.environ['TUMBLR_OAUTH_TOKEN'],
            oauth_token_secret=os.environ['TUMBLR_OAUTH_TOKEN_SECRET'])

    tags = ['ivotedforyou', 'idrathernotsayhowivoted', 'ididntvoteforyou', 'ididntvote']

    images = ['data/images/1.png', 'data/images/2.png', 'data/images/3.png', 'data/images/4.png']

    n = 0
    while n < TOTAL_NUMBER:
        tag = choice(tags)
        caption = u"""<p class='intro'>Dear Mr. President,</p>
        <p class='voted' data-vote-type='%s'>%s.</p>
        <p class='message'>This is a test post.</p>
        <p class='signature-name'>Signed,<br/>Test from Test, Test</p>""" % (
            tag,
            clean(tag),
        )

        filename = choice(images)

        with open(filename, 'rb') as f:
            tumblr_post = t.post('post', blog_url=app_config.TUMBLR_URL, params={
                'type': 'photo',
                'caption': caption,
                'tags': tag,
                'data': f
            })

        print n, tumblr_post['id']

        n += 1
Ejemplo n.º 16
0
def callback(request):
    t = Tumblpy(app_key=settings.TUMBLR_CONSUMER_KEY,
                app_secret=settings.TUMBLR_SECRET_KEY,
                oauth_token=request.session["oauth_token"],
                oauth_token_secret=request.session["oauth_token_secret"])

    oauth_verifier = request.GET.get('oauth_verifier')
    authorized_tokens = t.get_authorized_tokens(oauth_verifier)

    # save token locally.
    token, created = TumblrToken.objects.get_or_create(
        user=request.user, apikey=settings.TUMBLR_CONSUMER_KEY)
    token.access_token = authorized_tokens['oauth_token']
    token.access_token_secret = authorized_tokens['oauth_token_secret']
    token.save()
    return HttpResponse("Tumblr Oauth Passed.")
Ejemplo n.º 17
0
def dump_tumblr_json():
    t = Tumblpy(
            app_key=app_config.TUMBLR_KEY,
            app_secret=os.environ['TUMBLR_APP_SECRET'],
            oauth_token=os.environ['TUMBLR_OAUTH_TOKEN'],
            oauth_token_secret=os.environ['TUMBLR_OAUTH_TOKEN_SECRET'])

    limit = 10
    pages = range(0, 20)

    for page in pages:
        offset = page * limit
        posts = t.get('posts', blog_url=app_config.TUMBLR_URL, params={'limit': limit, 'offset': offset})

        with open('data/backups/tumblr_prod_%s.json' % page, 'w') as f:
            f.write(json.dumps(posts))
Ejemplo n.º 18
0
 def __init__(
     self,
     consumer_key,
     consumer_secret,
     oauth_token,
     oauth_secret,
     subreddit,
     site_link,
 ):
     self.client = Tumblpy(consumer_key, consumer_secret, oauth_token,
                           oauth_secret)
     self.blog_url = self.client.post('user/info')
     self.blog_url = self.blog_url['user']['blogs'][0]['url']
     self.agent = 'PyEng version: ' + str(randrange(1, 200, 1))
     self.r = praw.Reddit(user_agent=self.agent)
     self.subreddit = self.r.get_subreddit(subreddit)
     self.site_link = site_link
Ejemplo n.º 19
0
 def __init__(self, interval=3600, test=False):
     if not test:
         self.interval = interval
         credl = []
         with open("credentials/credentials", "r") as f:
             credl = f.read().split("\n")
         self.t = Tumblpy(
             credl[0],
             credl[1],
             credl[2],
             credl[3]
         )
         thread = threading.Thread(target=self.run, args=())
         thread.daemon = True
         thread.start()
     else:
         exit()
Ejemplo n.º 20
0
 def do():
     try:
         t = Tumblpy(self.key.ConsumerKey, self.key.ConsumerSecret)
         resp = t.get('posts/video',
                      blog_url=self.blog.url,
                      params={"offset": self.offset})
         # 视频不存在会导致 url字段为空
         posts = resp.get('posts')
         video_posts_handler(posts, self.blog)
         t.client.close()
     except TumblpyRateLimitError:
         spider_log.info("Key调用次数达到上限,本线程退出")
         return
     except TumblpyError as e:
         if e.error_code == 404:
             mark_dead_blog(self.blog)
     finally:
         session.remove()
Ejemplo n.º 21
0
 def do():
     try:
         t = Tumblpy(self.key.ConsumerKey, self.key.ConsumerSecret)
         # t.client.headers = {'Connection': 'close'}
         resp = t.get('posts/photo',
                      blog_url=self.blog.url,
                      params={"offset": self.offset})
         posts = resp.get('posts')
         post_handler(posts, self.blog)
         t.client.close()
     except TumblpyRateLimitError:
         spider_log.info("Key调用次数达到上限,本线程退出")
         return
     except TumblpyError as e:
         if e.error_code == 404:
             mark_dead_blog(self.blog)
     finally:
         session.remove()
Ejemplo n.º 22
0
def write_test_posts():
    """
    Writes test posts to our test blog as defined by app_config.py
    """

    # This is how many posts will be written.
    TOTAL_NUMBER = 9

    secrets = app_config.get_secrets()

    t = Tumblpy(app_key=secrets['TUMBLR_APP_KEY'],
                app_secret=secrets['TUMBLR_APP_SECRET'],
                oauth_token=secrets['TUMBLR_OAUTH_TOKEN'],
                oauth_token_secret=secrets['TUMBLR_OAUTH_TOKEN_SECRET'])

    tags = ['featured', '']

    images = [
        'http://media.npr.org/assets/img/2013/04/24/habitablezones_custom-fa87578c6e6a97788b92a0ecac956b9098607aa6-s40.jpg',
        'http://media.npr.org/assets/img/2013/04/24/ocpack-32260770b4090f86ddeb7502175a631d50c3b4a1-s51.jpg',
        'http://media.npr.org/assets/img/2013/04/24/dalrymple-c-karoki-lewis-4c9bd790639c870d51c670cbecbca4b802b82b1a-s51.jpg',
        'http://media.npr.org/assets/img/2013/04/24/ap111231019469-46289d097a45801ed2ca3464da14b13be40e5adb-s51.jpg'
    ]

    n = 0
    while n < TOTAL_NUMBER:
        image = choice(images)
        tag = choice(tags)
        caption = u"<p class='intro'>Introduction,</p><p class='message'>This is a test post.</p><p class='signature-name'>Sincerely,<br/>Test from Test, Test</p>"
        tumblr_post = t.post('post',
                             blog_url=app_config.TUMBLR_URL,
                             params={
                                 'type': 'photo',
                                 'caption': caption,
                                 'tags': tag,
                                 'source': image
                             })

        print n, tumblr_post['id']

        n += 1
Ejemplo n.º 23
0
    def __init__(self, frame):
        super(TumblrCtrl, self).__init__()
        self.frame = frame
        self.popup = None
        # self.imgView = self.frame.get_root().find_first('#ul')
        self.cfg = {
            "alt_sizes": -3,
            "dashboard_param": {
                "limit": 20,
                "offset": 0
            },
            "posts_param": {
                "limit": 20,
                "offset": 0
            },
            "proxies": {}
        }
        with open('data.json', 'r') as f:
            self.cfg.update(json.load(f))
        with open('tumblr_credentials.json', 'r') as f:
            self.tumblr_key = json.load(f)
        self.proxies = self.cfg['proxies']
        # self.offset = self.cfg['dashboard_param']['limit']
        self.current_folder = os.getcwd()
        self.target_folder = os.path.join(self.current_folder, 'imgTemp')
        if not os.path.isdir(self.target_folder):
            os.mkdir(self.target_folder)
        self.download_folder = os.path.join(self.current_folder, 'download')
        if not os.path.isdir(self.download_folder):
            os.mkdir(self.download_folder)

        # 创建一个线程池
        self.tpool = TPool(max_workers=20)
        # 创建一个进程池
        self.ppool = PPool(max_workers=2)
        # self.queue = Queue.Queue()
        self.tumblr = Tumblpy(self.tumblr_key['consumer_key'],
                              self.tumblr_key['consumer_secret'],
                              self.tumblr_key['oauth_token'],
                              self.tumblr_key['oauth_token_secret'],
                              proxies=self.proxies)
Ejemplo n.º 24
0
 def do():
     spider_log.info("开始获取博客信息!")
     blogs = load_all_blog()
     spider_log.info("加载Blog列表完成!")
     for blog in blogs:
         try:
             t = Tumblpy(self.key.ConsumerKey, self.key.ConsumerSecret)
             resp = t.get('info', blog_url=urlparse(blog.url).netloc)
             b = resp.get("blog")
             t.client.close()
             blog.name = b.get("name")
             blog.url = b.get("url")
             blog.posts = b.get("posts")
             spider_log.info("BlogId:{} 已更新".format(blog.id))
         except TumblpyRateLimitError:
             spider_log.info("Key达到上限,本线程退出")
         except TumblpyError as e:
             if e.error_code == 404:
                 mark_dead_blog(blog)
         finally:
             session.commit()
     session.remove()
Ejemplo n.º 25
0
from tumblpy import Tumblpy
from makeGifs import makeGif, check_config

config = ConfigParser.ConfigParser()
config.read("config.cfg")
config.sections()
slugs = check_config("config.cfg")[3]

CONSUMER_KEY = config.get("tumblr", "consumer_key")
CONSUMER_SECRET = config.get("tumblr", "consumer_secret")
OAUTH_TOKEN = config.get("tumblr", "oauth_token")
OAUTH_TOKEN_SECRET = config.get("tumblr", "oauth_token_secret")

t = Tumblpy(
    CONSUMER_KEY,
    CONSUMER_SECRET,
    OAUTH_TOKEN,
    OAUTH_TOKEN_SECRET,
)

while True:
    # you can set many more options, check the makeGif-function
    quote = makeGif(random.choice(slug), frames=20)
    quote = ' '.join(quote)

    # reduce amount of colors, because tumblr sucks
    subprocess.call([
        'convert', 'barbarella.gif', '-layers', 'optimize', '-colors', '64',
        '-loop', '0', 'barbarella.gif'
    ])

    while (os.path.getsize('barbarella.gif') > 1048576):
Ejemplo n.º 26
0
def tumblr_connect():
    return Tumblpy(settings.TUMBLR_OAUTH_KEY, settings.TUMBLR_SECRET_KEY)
Ejemplo n.º 27
0

#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"],
def Tumblpy_from_dict(data):
    """Create a Tumblpy object from the authentication constants stored in a dictionary."""
    return Tumblpy(data['consumer_key'], data['consumer_secret'],
                   data['token_key'], data['token_secret'])
Ejemplo n.º 29
0
        context['title'] = 'CairoSVG is unhappy.'
        context['message'] = e.output
        return render_template('500.html', **context)

    context = {
        'name': name,
        'location': location,
    }

    caption = render_template('caption.html', **context)

    secrets = app_config.get_secrets()

    t = Tumblpy(
        app_key=secrets['TUMBLR_APP_KEY'],
        app_secret=secrets['TUMBLR_APP_SECRET'],
        oauth_token=secrets['TUMBLR_OAUTH_TOKEN'],
        oauth_token_secret=secrets['TUMBLR_OAUTH_TOKEN_SECRET'])

    params = {
        "type": "photo",
        "caption": caption,
        "tags": app_config.TUMBLR_TAGS,
        "source": "http://%s%s" % (app_config.SERVERS[0], png_path)
    }

    try:
        tumblr_post = t.post('post', blog_url=app_config.TUMBLR_URL, params=params)
        tumblr_url = u"http://%s/%s" % (app_config.TUMBLR_URL, tumblr_post['id'])
        logger.info('200 %s reader(%s) (times in EST)' % (tumblr_url, name))
Ejemplo n.º 30
0
def download_images_from_tumblr(tag,
                                max_count=10,
                                before_timestamp=None,
                                saved_path=None,
                                is_face_detect=None,
                                is_animeface=None):
    print("download_image_from_tumblr/tag:", tag, ", before_timestamp:",
          before_timestamp)

    dl_count = 0
    fc_count = 0
    last_timestamp = 0

    t = Tumblpy(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

    params = {'tag': tag}
    if before_timestamp is not None:
        params.update({"timestamp": before_timestamp})

    output_path = saved_path
    if saved_path is None:
        output_path = "./dl_tumblr/" + tag

    while dl_count < max_count:

        if last_timestamp > 0:
            params.update({"before": last_timestamp})

        tags = None
        try:
            tags = t.get('tagged', params=params)
        except Exception as e:
            print("error: ", str(e))
        # print("tags: ", tags)

        if tags == None or len(tags) == 0:
            print("end: tags is void")
            break

        for i, tag in enumerate(tags):

            if i == None or tag == None:
                break

            # print("index: ", i, ", tag: ", tag)
            last_timestamp = tag["timestamp"]

            if "photos" in tag:
                photos = tag["photos"]
                for j, photo in enumerate(photos):
                    # print("index: ", j, ", tag: ", photo)
                    image_url = photo["original_size"]["url"]
                    # print("i:", i, "j:", j, ", image_url:", image_url)
                    file_path = download_image(image_url, output_path)
                    if file_path:
                        dl_count += 1
                        if is_face_detect is not None:
                            fc_count += save_detected_faces(
                                image_path=file_path,
                                is_animeface=is_animeface)
            else:
                break

    return dl_count, fc_count, last_timestamp