def scrape(self, all=True, max_depth=10): # No maximum depth if scraping all posts max_depth = float('inf') if all else max_depth # Create the tumblpy agent agent = Tumblpy(TUMBLR_KEYS['consumer'], TUMBLR_KEYS['secret']) offset = 0 posts = [] # Pull 20 posts a max number of times equal to max_depth while offset < max_depth: # Get 20 posts new_posts = agent.get('posts', self.url, params={'offset': offset * 20, 'limit': 20, 'notes_info': True}) new_posts = new_posts['posts'] # No posts found; stop scraping if not new_posts: break for post in new_posts: # if any of the new posts is from before last scraping, stop time = tz.make_aware(dt.fromtimestamp(post['timestamp'])) if time < self.last_scraped: offset = max_depth break posts += new_posts offset += 1 self.last_scraped = tz.now() # Create photos from posts for post in posts: photos = scraping.models.photos.Photo.from_tumblr_api(post, self) for photo_data in photos: photo = photo_data['photo'] raw_tags = photo_data['raw tags'] photo.save() photo.tags_from_ary(raw_tags)
def requestToken(): # Go to http://www.tumblr.com/oauth/apps and click your # app to find out your dynamic callback url t = Tumblpy(app_key = APP_KEY, app_secret = APP_SECRET, callback_url = 'http://' + BLOG_URL) return auth_props = t.get_authentication_tokens() auth_url = auth_props['auth_url'] oauth_token = auth_props['oauth_token'] oauth_token_secret = auth_props['oauth_token_secret'] print "token: %s" % oauth_token print "token_secret: %s" % oauth_token_secret print "connect to tumblr via %s" % auth_url print "once connected obtain the value in the URL with the tag oauth_verifier" t = Tumblpy(app_key = APP_KEY, app_secret = APP_SECRET, oauth_token = oauth_token, oauth_token_secret = oauth_token_secret) oauth_verifier = raw_input("inserta el oauth verifier: ") authorized_tokens = t.get_authorized_tokens(oauth_verifier) final_oauth_token = authorized_tokens['oauth_token'] final_oauth_token_secret = authorized_tokens['oauth_token_secret'] print "token: %s" % final_oauth_token print "token_secret: %s" % final_oauth_token_secret
def do_post(path): print "Posting..." t = Tumblpy("","", "","") tbuff = textview.get_buffer() article_text = "" if isWeather.get_active(): article_text = get_date_desc() + weatherProvider.get_weather() article_text = article_text + tbuff.get_text(tbuff.get_start_iter(), tbuff.get_end_iter()) blog_url = t.post('user/info') blog_url = blog_url['user']['blogs'][1]['url'] if path.get_text() !="No image": photo = open(path.get_text(), 'rb') ephoto = open(path.get_text(), 'rb') tags = "catumblr , "+ platform.node() etags = exifread.process_file(ephoto) if etags.has_key('Image Model'): tags = "catumblr , "+ platform.node() + ", " + str(etags['Image Model']) p_params = {'type':'photo', 'caption': article_text, 'data': photo, 'tags':tags} ephoto.close() else: tags = "catumblr , "+ platform.node() time_caption = strftime("%Y-%m-%d %H:%M:%S", gmtime()) p_params = {'type':'text', 'body': article_text, 'caption': time_caption, 'tags':tags} post = t.post('post', blog_url=blog_url, params=p_params) print post # returns id if posted successfully
def authorize(): # Authorization Step 1 t = Tumblpy(app_key = app_key, app_secret = app_secret) auth_props = t.get_authentication_tokens() auth_url = auth_props['auth_url'] # temp tokens oauth_token = auth_props['oauth_token'] oauth_token_secret = auth_props['oauth_token_secret'] # Authorization Step 2 t = Tumblpy(app_key = app_key, app_secret = app_secret, oauth_token=oauth_token, oauth_token_secret=oauth_token_secret, callback_url='oob') # oauth_verifier = *Grab oauth verifier from URL* # At this point, follow instructions in commandline. print "Go to the link ", auth_url print "Click 'Allow' and when the page redirects, "\ "grab the value of oauth_verifier from the URL and enter it here." oauth_verifier = raw_input('oauth_verifier:') authorized_tokens = t.get_access_token(oauth_verifier) # Final access tokens oauth_token = authorized_tokens['oauth_token'] oauth_token_secret = authorized_tokens['oauth_token_secret'] return oauth_token, oauth_token_secret
def main(): configFile = argv[1] config = SafeConfigParser() config.read(configFile) consumer_key = config.get('consumer', 'key') consumer_secret = config.get('consumer', 'secret') tumblr = Tumblpy(consumer_key, consumer_secret) auth_props = tumblr.get_authentication_tokens() print("Go to the following link in your browser:") print(auth_props['auth_url']) print('') oauth_verifier = 'n' while oauth_verifier.lower() == 'n': oauth_verifier = raw_input('What is the PIN?: ') tumblr = Tumblpy(consumer_key, consumer_secret, auth_props['oauth_token'], auth_props['oauth_token_secret']) authorized_tokens = tumblr.get_access_token(oauth_verifier) config.set('oauth', 'key', authorized_tokens['oauth_token']) config.set('oauth', 'secret', authorized_tokens['oauth_token_secret']) print('Saving keys to config file %s' % configFile) with open(configFile, 'w') as fp: config.write(fp)
def postInTumblr(self,title,date,body): t = Tumblpy(app_key = APP_KEY, app_secret = APP_SECRET, oauth_token = OAUTH_TOKEN, oauth_token_secret = OAUTH_TOKEN_SECRET) # Assume you are using the blog_url and Tumblpy instance from the previous sections photo = open('/Users/xmanoel/Documents/Development/posterous-verter/gotasdechuva.jpg', 'rb') post = t.post('post', blog_url=BLOG_URL, params={'type':'text', 'state':STATE, 'title':title, 'body':body, 'date':date, 'tags':TAGS}) print post # returns id if posted successfully
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
def demoPost(): t = Tumblpy(app_key = APP_KEY, app_secret = APP_SECRET, oauth_token = OAUTH_TOKEN, oauth_token_secret = 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'] # Assume you are using the blog_url and Tumblpy instance from the previous sections post = t.post('post', blog_url=blog_url, params={'type':'text', 'state':'private', 'title':'titulo', 'body':'<p>This is a body</p>'}) print post # returns id if posted successfully
def setup(): global oauth_token global oauth_token_secret t = Tumblpy(app_key = my_app_key, app_secret = my_app_secret, callback_url = 'http://example.com/callback/') auth_props = t.get_authentication_tokens() auth_url = auth_props['auth_url'] print auth_url oauth_token = auth_props['oauth_token'] oauth_token_secret = auth_props['oauth_token_secret']
def post_to_tumblr(): # Collect them all CONSUMER_KEY = environ['tumblr_consumer_key'] CONSUMER_SECRET = environ['tumblr_consumer_secret'] OAUTH_TOKEN = environ['tumblr_auth_token'] OAUTH_TOKEN_SECRET = environ['tumblr_auth_token_secret'] # Authenticate dat t = Tumblpy(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) # Load the photo and post to Tumblr photo = open('pic.jpg', 'rb') post = t.post('post', blog_url='http://meyow-me.tumblr.com', params={'type':'photo', 'caption': 'Test Caption', 'data': photo})
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)
def getPosts(): t = Tumblpy(app_key = APP_KEY, app_secret = APP_SECRET, oauth_token = OAUTH_TOKEN, oauth_token_secret = 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 "url: %s" % blog_url # Assume you are using the blog_url and Tumblpy instance from the previous section posts = t.get('posts', blog_url=blog_url) for post in posts['posts']: print "%s %s" % (post['date'],post['post_url'])
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
def main(): setup() t = Tumblpy(app_key = my_app_key, app_secret = my_app_secret, oauth_token=oauth_token, oauth_token_secret=oauth_token_secret) blog='staff.tumblr.com' #followers = t.get('posts', blog_url=blog) #print followers posts = t.get('tagged',blog_url=None, params={'tag':'asoiaf', 'before': 1363820400}) print len(posts) a = 0 for p in posts: print (p['post_url'], p['timestamp'], p['type']) '''
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))
def deletePosts(tag): t = Tumblpy(app_key = APP_KEY, app_secret = APP_SECRET, oauth_token = OAUTH_TOKEN, oauth_token_secret = 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 "url: %s" % blog_url # Assume you are using the blog_url and Tumblpy instance from the previous section posts = t.get('posts', blog_url=blog_url, params={'tag':tag}) for post in posts['posts']: print "%s %s" % (post['date'],post['post_url']) id = post['id'] post = t.post('post/delete', blog_url=blog_url, params={'id':id}) print "post deleted!!"
def hidePosts(): t = Tumblpy(app_key = APP_KEY, app_secret = APP_SECRET, oauth_token = OAUTH_TOKEN, oauth_token_secret = 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 "url: %s" % blog_url # Assume you are using the blog_url and Tumblpy instance from the previous section posts = t.get('posts', blog_url=blog_url, params={'tag':"JustMigrate"}) for post in posts['posts']: print "%s %s" % (post['date'],post['post_url']) id = post['id'] post = t.post('edit', blog_url=blog_url, params={'id':id,'state':'private'}) print "ahora está en estado %s" % post['state']
def dump_tumblr_json(): t = Tumblpy( app_key=app_config.TUMBLR_KEY, app_secret=os.environ["%s_TUMBLR_APP_SECRET" % app_config.CONFIG_NAME], oauth_token=os.environ["%s_TUMBLR_OAUTH_TOKEN" % app_config.CONFIG_NAME], oauth_token_secret=os.environ["%s_TUMBLR_OAUTH_TOKEN_SECRET" % app_config.CONFIG_NAME], ) 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))
def get_connection_values(response, **kwargs): if not response: return None t = Tumblpy(app_key=config["consumer_key"], app_secret=config["consumer_secret"], oauth_token=response['oauth_token'],oauth_token_secret=response['oauth_token_secret']) blog_url = t.post('user/info') return dict( provider_id=config['id'], provider_user_id=str(blog_url["user"]["name"]), access_token=response['oauth_token'], secret=response['oauth_token_secret'], #display_name='%s %s' %(profile.first_name, profile.last_name) , profile_url="N/A", image_url= "" )
def GET(self): data = web.input(oauth_verifier=None) if data.oauth_verifier: tumblr = Tumblpy(app_key=KEYS['tumblr']['key'], app_secret=KEYS['tumblr']['secret'], oauth_token=session.tumblr_oauth_token, oauth_token_secret=session.tumblr_oauth_token_secret) auth = tumblr.get_authorized_tokens(data.oauth_verifier) return j({'success': True, 'auth': auth}) else: tumblr = Tumblpy(app_key=KEYS['tumblr']['key'], app_secret=KEYS['tumblr']['secret'], callback_url='https://secure.lumilux.org/admin/tumblrauth') auth = tumblr.get_authentication_tokens() auth_url = auth['auth_url'] session.tumblr_oauth_token = auth['oauth_token'] session.tumblr_oauth_token_secret = auth['oauth_token_secret'] raise web.SeeOther(auth_url)
def from_api(cls, name): # Create tumblpy agent agent = Tumblpy(TUMBLR_KEYS['consumer'], TUMBLR_KEYS['secret']) try: # Get blog info info = agent.get('info', name)['blog'] # Get avatar avatar = agent.get('avatar', name, params={'size': 512}) except TumblpyError: raise TumblpyError('Could not connect to {}'.format(name + '.tumblr.com')) # Create TumblrBlog instance = cls() # Assign fields instance.url = info['url'] instance.name = info['title'] instance.description = info['description'] instance.avatar_url = avatar['url'] # Return without saving to db return instance
def get_url(self, string): # Check if it matches a tumblr url pattern tumblr_url_regex = '(?P<url>(http\:\/\/)?[A-Za-z0-9\-]+\.tumblr\.com).*' tumblr_url_match = re.fullmatch(tumblr_url_regex, string) if tumblr_url_match: return {'type': TumblrBlog, 'url': tumblr_url_match} # Check if it matches a tumblr name pattern tumblr_name_regex = '[A-Za-z0-9\-]+' tumblr_name_match = re.fullmatch(tumblr_name_regex, string).string # Check if a tumblr blog with that name exists if tumblr_name_match: tumblr_agent = Tumblpy(TUMBLR['consumer'], TUMBLR['secret']) try: tumblr_agent.get('info', tumblr_name_match) # tumblpy didn't throw an exception, so blog exists return {'type': TumblrBlog, 'url': 'http://' + tumblr_name_match + '.tumblr.com/'} except TumblpyError: # tumblpy did throw an exception, so blog doesn't exist. pass
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
def f_post(skrizz, trigger): """Post to the bots tumblr account""" post_content = trigger.group(2) t = Tumblpy(consumer_key, consumer_secret, access_token, access_token_secret) blog_info = t.post('user/info') blog_url = blog_info['user']['blogs'][0]['url'] blog_title = str(blog_info['user']['blogs'][0]['title']) blog_desc = str(blog_info['user']['blogs'][0]['description']) blog_posts = str(blog_info['user']['blogs'][0]['posts']) if post_content.startswith('info'): skrizz.say('[TUMBLR] Title: ' + blog_title + ' | Address: ' + str(blog_url) + ' | Description: ' + blog_desc + ' | Posts: ' + blog_posts) return if post_content.startswith('that'): if trigger.sender not in skrizz.memory['last_seen_url']: skrizz.say('I haven\'t seen any URL\'s lately...') return post_url = skrizz.memory['last_seen_url'][trigger.sender] post_caption = post_content.replace("that","",1) if not post_caption and skrizz.memory['last_seen_url_title'][trigger.sender]: post_caption = skrizz.memory['last_seen_url_title'][trigger.sender] elif not post_caption: post_caption = "I was too lazy to come up with a clever caption." post_content = post_url + " " + post_caption post, meta = build_post(post_content) if not post: skrizz.say("Error: No URL Specified to post.") return try: t.post('post', blog_url=blog_url, params=post) except TumbplyError, e: skrizz.say(e.message) skrizz.say('Something super sad happened :(')
class Client(object): name = "tumblr" def __init__(self, config): self.client = Tumblpy(config['consumer_key'], config['consumer_secret'], config['token'], config['token_secret']) self.blog_id = config['blog_id'] def send(self, picture): "Post a post. `picture` is a `Result` object from `picdescbot.common`" post_text = TEMPLATE.format(description=picture.caption, source=picture.source_url) tags = DEFAULT_TAGS + filter_tags(picture.tags) params = {'caption': post_text, 'source': picture.url, 'tags': ','.join(tags)} params.update(DEFAULT_PARAMS) retries = 0 post = None while retries < 3 and post is None: if retries > 0: print('retrying...') try: post = self.client.post("post", blog_url=self.blog_id, params=params) except tumblpy.exceptions.TumblpyError as e: print("Error when sending tumblr post: %s" % e) retries += 1 if retries >= 3: raise else: time.sleep(5) return post['id']
def connect(self, app_key = None, app_secret = None, oauth_token = None, oauth_token_secret = None): return Tumblpy(app_key = app_key, app_secret = app_secret, oauth_token = oauth_token, oauth_token_secret = oauth_token_secret )
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 />') try: caption = "<p class='message'>%s</p><p class='signature-name'>Initialed,<br/>%s from %s</p><p class='footnote'>Dinner is hard. We want to know what's on your family's table, and why. Share yours at <a href='http://%s/'>NPR's Dinnertime Confessional</a>.</p>" % ( strip_breaks(strip_html(request.form['message'])), strip_html(request.form['signed_name']), strip_html(request.form['location']), app_config.TUMBLR_URL ) 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']) file_path = '/upload/%s/%s_%s' % ( 'family-meal', str(time.mktime(datetime.datetime.now().timetuple())).replace('.', ''), secure_filename(request.files['image'].filename.replace(' ', '-')) ) with open('/tmp%s' % file_path, 'w') as f: f.write(request.files['image'].read()) params = { "type": "photo", "caption": caption, "tags": "food,dinner,plate,confession,crunchtime,npr", "source": "http://%s%s" % (app_config.SERVERS[0], file_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' % tumblr_url) return redirect('%s#posts' % tumblr_url, code=301) except TumblpyError, e: logger.error('%s %s' % (e.error_code, e.msg)) return 'TUMBLR ERROR' return redirect('%s#posts' % tumblr_url, code=301)
from tumblpy import Tumblpy import yaml settings_fn = 'settings.yml' with open(settings_fn) as f: settings = yaml.load(f) t = Tumblpy(app_key=settings['tumblr']['consumer_key'], app_secret=settings['tumblr']['consumer_secret'], oauth_token=settings['tumblr']['oauth_token'], oauth_token_secret=settings['tumblr']['oauth_secret']) posts = t.get('posts', blog_url='okbot.tumblr.com') print posts
class TumblrCtrl(object): """docstring for TumblrCtrl""" 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) def myOnLoadDatas(self, uri): self.tpool.submit(self._downloadInRAM, "photo", uri) return True def loadPreviewImg(self, data): fileName = data['id'] + '_' + data['preview_size'].split("_")[-1] print(fileName) # return file_path = os.path.join(self.target_folder, fileName) if not os.path.isfile(file_path): self.tpool.submit(self._downloadPrev, "photo", data, file_path) else: self.popup.set_style_attribute("background-image", "url(" + file_path + ")") self.popup.set_attribute("imgid", data['id']) self.popup.set_attribute("original", data['original']) pass def loadImgList(self): '''获取图片列表 { "id": 0, "source_url": "", "original_size": "https://*_1280.jpg", "alt_sizes": "https://*_100.jpg" } ''' print('获取图片列表') future_tasks = [ self.ppool.submit(getDashboards, self.tumblr, self.cfg) ] for f in future_tasks: if f.running(): print('is running') for f in as_completed(future_tasks): try: if f.done(): self.cfg['dashboard_param']['offset'] += self.cfg[ 'dashboard_param']['limit'] for x in f.result(): fileName = x['id'] + '_' + x['alt_sizes'].split( "_")[-1] # print(fileName) file_path = os.path.join(self.target_folder, fileName) if not os.path.isfile(file_path): self.tpool.submit(self._download, "photo", x, file_path) else: html = htmlTemplate.format(x['id'], file_path, x['original_size'], x['preview_size']) self.frame.call_function('appendImgList', html) except Exception as e: f.cancel() return # li = sciter.Element.create("li") # li.set_attribute("id", x['id']) # # li.set_attribute("data-src", file_path) # ul.append(li) # li.set_style_attribute( "background-image", file_path ) def _getTumblrList(self): print('_getTumblrList') return self.getDashboard() # return self.getBloggers() def _downloadPrev(self, medium_type, data, file_path): if medium_type == "photo": print('_download photo') req = requests.get(data['preview_size'], proxies=self.proxies) with open(file_path, 'wb') as fh: for chunk in req.iter_content(chunk_size=1024): fh.write(chunk) self.popup.set_style_attribute("background-image", "url(" + file_path + ")") self.popup.set_attribute("imgid", data['id']) self.popup.set_attribute("original", data['original']) # html = htmlTemplate.format( x['id'], file_path, x['original_size'], x['preview_size'] ) # self.frame.call_function('appendImgList', html ) return def downloadOriginal(self, id, url): fileName = id + '_' + url.split("_")[-1] file_path = os.path.join(self.download_folder, fileName) if not os.path.isfile(file_path): self.tpool.submit(self._downloadOriginal, url, file_path) def _downloadOriginal(self, url, file_path): req = requests.get(url, proxies=self.proxies) with open(file_path, 'wb') as fh: for chunk in req.iter_content(chunk_size=1024): fh.write(chunk) def _download(self, medium_type, x, file_path): if medium_type == "photo": print('_download photo') req = requests.get(x['alt_sizes'], proxies=self.proxies) with open(file_path, 'wb') as fh: for chunk in req.iter_content(chunk_size=1024): fh.write(chunk) html = htmlTemplate.format(x['id'], file_path, x['original_size'], x['preview_size']) self.frame.call_function('appendImgList', html) # li = sciter.Element.create("li") # li.set_attribute("id", id) # # li.set_attribute("data-src", file_path) # ul.append(li) # li.set_style_attribute( "background-image", file_path ) return def _downloadInRAM(self, medium_type, uri): print("_downloadInRAM", uri) try: if medium_type == "photo": req = requests.get(uri, proxies=self.proxies) self.frame.data_ready(uri, req.content) except Exception as e: raise e def _mkMainDict(self, d): data = [] for v in d["posts"]: data.append({ 'id': gets(v, 'id', 0), 'link_url': gets(v, 'link_url', ''), 'original_size': gets(v, 'photos.0.original_size.url', ''), 'preview_size': gets( v, 'photos.0.alt_sizes.' + str(self.cfg['preview_size']) + '.url', ''), 'alt_sizes': gets( v, 'photos.0.alt_sizes.' + str(self.cfg['alt_sizes']) + '.url', '') }) return data def getDashboard(self): dashboard = self.tumblr.dashboard(self.cfg['dashboard_param']) if dashboard: self.cfg['dashboard_param']['offset'] += self.cfg[ 'dashboard_param']['limit'] return self._mkMainDict(dashboard) def getBloggers(self): '''取得博主的列表''' all_posts = self.tumblr.posts('kuvshinov-ilya.tumblr.com', None, self.cfg['posts_param']) if all_posts: self.cfg['posts_param']['offset'] += self.cfg['posts_param'][ 'limit'] return self._mkMainDict(all_posts)
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))
def __init__(self, config): self.client = Tumblpy(config['consumer_key'], config['consumer_secret'], config['token'], config['token_secret']) self.blog_id = config['blog_id']
from tumblpy import Tumblpy import time import Configuration config = Configuration.Configuration('config.ini') # Get the final tokens from the database or wherever you have them stored t = Tumblpy(config.ConsumerKey, config.ConsumerSecret, config.OauthToken, config.OauthTokenSecret) # 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'] posts = t.get('posts', blog_url=blog_url) class UserInfo: def __init__(self, name, blogURL): self.Name = name self.BlogURL = blogURL class AutoFollow: USERNAMEFILE = "followedUser.txt" # Writes followed user name into a file (followedUser.txt) def WriteNameToFile(self, username): userFile = open(self.USERNAMEFILE, 'a') userFile.write(username) userFile.write('\n') userFile.close()
def _post_to_tumblr(): """ Handles the POST to Tumblr. """ 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', do_mark_safe('<br/>')) # Request is a global. Import it down here where we need it. from flask import request # These should match the form fields. message = strip_html(request.form.get('message', None)) message = escape(message) message = strip_breaks(message) name = strip_html(request.form.get('signed_name', None)) email = strip_html(request.form.get('email', None)) context = { 'message': message, 'name': name, 'email': email, 'app_config': app_config } 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']) file_path = '/uploads/%s/%s_%s' % ( app_config.PROJECT_SLUG, str(time.mktime(datetime.datetime.now().timetuple())).replace('.', ''), secure_filename(request.files['image'].filename.replace(' ', '-')) ) with open('/var/www%s' % file_path, 'w') as f: f.write(request.files['image'].read()) params = { "type": "photo", "caption": caption, "tags": app_config.TUMBLR_TAGS, "source": "http://%s%s" % (app_config.SERVERS[0], file_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 %s) (times in EST)' % (tumblr_url, name, email)) return redirect(tumblr_url, code=301) except TumblpyError, e: logger.error('%s %s http://%s%s reader(%s %s) (times in EST)' % ( e.error_code, e.msg, app_config.SERVERS[0], file_path, name, email)) return 'TUMBLR ERROR'
from tumblpy import Tumblpy import urllib.request as urllib2 import json, codecs import time # Get the final tokens from the database or wherever you have them stored t = Tumblpy("aDnOPY0TMjGlvge1WXf9m7iYScGKwW39Lay7fVcPwOtyi0gI1j", "soipYkCzLZfFVgDTU9Sc1gujAPTtmaAQB3N10jWlo21r5ZVxgK", "g8KKgHnoZUnknbmnyCcghGZzMvIzH3VzdnXsMls0iDHbsWTeUF", "bgepJW6rZysdAivMGMvn9iRjVpcxw78JieY3KMJi0JMzv2lXta") # Print out the user info, let's get the first blog url... blog_url = t.post('user/info') blog_url = str(blog_url['user']['blogs'][0]['url']) #posts = t.get('posts', blog_url=blog_url) class UserInfo: def __init__(self, name, postURL, postID, reblogKey): self.Name = name self.PostURL = postURL self.PostID = postID self.ReblogKey = reblogKey class AutoLovePost: USERNAMEFILE = "likedUser.txt" # Writes followed user name into a file (followedUser.txt) def WriteNameToFile(self, username): userFile = open(self.USERNAMEFILE, 'a') userFile.write(username) userFile.write('\n') userFile.close()
#!/usr/bin/env python import csv import re from tumblpy import Tumblpy, TumblpyError import app_config 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']) with open('data/review.csv') as f: rows = list(csv.reader(f)) for row in rows: svg_url, status, tumblr_url = row if not tumblr_url: row.append('') row.append('') continue post_id = tumblr_url.split('/')[-1] try:
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', 'star_wars.gif', '-layers', 'optimize', '-colors', '64', '-loop', '0', 'star_wars.gif' ]) while (os.path.getsize('star_wars.gif') > 1048576):
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 />') try: caption = "<p class='message'>%s</p><p class='signature-name'>Initialed,<br/>%s from %s</p><p class='footnote'>Dinner is hard. We want to know what's on your family's table, and why. Share yours at <a href='http://%s/'>NPR's Dinnertime Confessional</a>.</p>" % ( strip_breaks(strip_html(request.form['message'])), strip_html(request.form['signed_name']), strip_html(request.form['location']), app_config.TUMBLR_URL) 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']) file_path = '/upload/%s/%s_%s' % ( 'family-meal', str(time.mktime( datetime.datetime.now().timetuple())).replace('.', ''), secure_filename(request.files['image'].filename.replace(' ', '-'))) with open('/tmp%s' % file_path, 'w') as f: f.write(request.files['image'].read()) params = { "type": "photo", "caption": caption, "tags": "food,dinner,plate,confession,crunchtime,npr", "source": "http://%s%s" % (app_config.SERVERS[0], file_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' % tumblr_url) return redirect('%s#posts' % tumblr_url, code=301) except TumblpyError, e: logger.error('%s %s' % (e.error_code, e.msg)) return 'TUMBLR ERROR' return redirect('%s#posts' % tumblr_url, code=301)
def get_tumblr_table(self): t = Tumblpy("sYKNnjJRqbxWWlg19sY8WYnZyQi6wURbilnE4k3vsyqX4vc4ER","n8mtWzKieR8qgTdwUWNhF3OYZVIsvMZXvVr9DKPlCGI6wE2VLV", "PyvcruFPx1YqhdAOkCWjCPWMBIYx3fUJaiFzjhxpkwUwps0VjC","Zjwmi2wYA83rtIdoL82BcWcj5sxm5QrI1MEnZX4DzFQHWydx1C") blog_url = t.post('user/info') blog_url = blog_url['user']['blogs'][1]['url'] posts = t.get('posts', blog_url=blog_url) posts_count = posts["total_posts"] #print posts table = gtk.Table(posts_count, 1, False) # set the spacing to 10 on x and 10 on y table.set_row_spacings(10) table.set_col_spacings(10) # pack the table into the scrolled window i = 0 for cur_post in posts["posts"]: buffer = "" cur_image_fac = catImageBox.catImageBox("http://www.linux.org.ru/tango/img/opensource-logo.png", 50, 50) if cur_post["type"] == "text": buffer = cur_post["body"] if cur_post["type"] == "photo": j = len(cur_post["photos"][0]["alt_sizes"]) -1 img_url = cur_post["photos"][0]["alt_sizes"][j]["url"] cur_image_fac = catImageBox.catImageBox(img_url, 75, 75) buffer = cur_post["caption"] s = MLStripper() s.feed(buffer) label = gtk.Label(s.get_data()) label.set_line_wrap(True) label.set_justify(gtk.JUSTIFY_LEFT) label.set_width_chars(30) label.show() #date box date_box = gtk.HBox(True, 1) date_icon = gtk.Image() date_icon.set_from_file("resources/cal.png") date_icon.show() cur_image = cur_image_fac.image cur_image.show() fdate = cur_post["date"] date_label = gtk.Label(fdate.split(" ")[0]) date_label.set_line_wrap(True) date_label.show() #date_box.pack_start(date_icon, True, True, 1) date_box.pack_start(cur_image, True, True, 1) date_box.pack_end(date_label, True, True, 1) date_box.show() #tag box tag_box = gtk.HBox(True, 1) tag_icon = gtk.Image() tag_icon.set_from_file("resources/tag.png") tag_icon.show() ftags = "" for cur_tag in cur_post["tags"]: ftags += cur_tag + " " tag_label = gtk.Label(ftags) tag_label.set_line_wrap(True) tag_label.show() tag_box.pack_start(tag_icon, True, True, 1) tag_box.pack_end(tag_label, True, True, 1) tag_box.show() separator = gtk.HSeparator() separator.show() box = gtk.VBox(True, 1) box.pack_start(date_box, True, True, 1) if cur_post["tags"].count > 0: box.pack_start(tag_box, True, True, 1) box.pack_start(label, True, True, 0) box.pack_end(separator, True, True, 0) box.show() table.attach(box, 1, 2, i, i+1) i = i+1 return table
gif = '%s/%s' % (GIF_DIR, tweet_gif) gif = gif.replace('\n', '') api.update_with_media(gif) # Post to Tumblr Too # Load tumblr credentials for this bot from config file BOTCRED_FILE = '%s/.twurlrc' % os.path.expanduser('~') with open(BOTCRED_FILE, 'r') as credfile: full_config = yaml.load(credfile) api_key = api_key = full_config['profiles']['twinpeaksdiarytumblr'].keys( )[0] bot_creds = full_config['profiles']['twinpeaksdiarytumblr'][api_key] from tumblpy import Tumblpy TUMBLR_CONSUMER_KEY = bot_creds['consumer_key'] TUMBLR_CONSUMER_SECRET = bot_creds['consumer_secret'] OAUTH_TOKEN = bot_creds['token'] OAUTH_TOKEN_SECRET = bot_creds['secret'] t = Tumblpy(TUMBLR_CONSUMER_KEY, TUMBLR_CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) blog_url = t.post('user/info') blog_url = blog_url['user']['blogs'][0]['url'] photo = open(gif, 'rb') post = t.post('post', blog_url=blog_url, params={ 'type': 'photo', 'data': photo })