def get_user_api(username): user = settings.TUMBLR_USERS[username] if user.has_key('email') and user.has_key('password'): return Api(user['tumblr_user'] + ".tumblr.com", user['email'], user['password']) else: return False
def testWrite(self): api = Api(BLOG, USER, PASSWORD) newpost = api.write_regular('title', 'body') post = api.read(newpost['id']) assert newpost['id'] == post['id'] newpost = api.write_link('http://www.google.com') post = api.read(newpost['id']) assert newpost['id'] == post['id'] newpost = api.write_quote('it was the best of times...') post = api.read(newpost['id']) assert newpost['id'] == post['id'] newpost = api.write_conversation('me: wow\nyou: double wow!') post = api.read(newpost['id']) assert newpost['id'] == post['id'] newpost = api.write_video('http://www.youtube.com/watch?v=60og9gwKh1o') post = api.read(newpost['id']) assert newpost['id'] == post['id'] newpost = api.write_photo( 'http://www.google.com/intl/en_ALL/images/logo.gif') post = api.read(newpost['id']) assert newpost['id'] == post['id']
def testBadAuthenticate(self): api = Api(BLOG, USER, 'badpassword') try: api.auth_check() assert False # should never get here except TumblrAuthError, e: pass
def testRequiredArgs(self): api = Api(BLOG, USER, PASSWORD) self.assertRaises(TumblrError, api.write_regular) self.assertRaises(TumblrError, api.write_quote) self.assertRaises(TumblrError, api.write_photo) self.assertRaises(TumblrError, api.write_photo, source='foo', data='bar') self.assertRaises(TumblrError, api.write_conversation) self.assertRaises(TumblrError, api.write_link) self.assertRaises(TumblrError, api.write_video)
def testFixNames(self): api = Api(BLOG) before = {} before['test_one'] = 1 before['test_two'] = 1 after = api._fixnames(before) assert not 'test_one' in after assert 'test-one' in after assert 'test-two' in after assert not 'test_two' in after
def __init__(self, name, save='./photos', db='./tumblr_photo_down.db'): self.name = name self.save = save self.db = db self.api = Api(name + '.tumblr.com') self.pp = pprint.PrettyPrinter(indent=4) self._count = 0 self._coursor = 1 self.posts = None self.conn = None self._init_db() #self.pp.pprint(self.api); if os.path.isdir(save) is False: os.mkdir(save)
def testRead(self): api = Api(BLOG) freq = {} posts = api.read() total = 0 for post in posts: total += 1 type = post['type'] try: freq[type] += 1 except: freq[type] = 1 assert total > 0 for type in freq: assert self.countType(api, type) == freq[type]
def update_backchannel(): """ Update data for the backchannel from Tumblr """ TUMBLR_FILENAME = 'www/tumblr.json' TUMBLR_BLOG_ID = 'nprbackchannel' TUMBLR_MAX_POSTS = 10 api = Api(TUMBLR_BLOG_ID) posts = list(api.read(max=TUMBLR_MAX_POSTS)) posts.reverse() with open(TUMBLR_FILENAME, 'w') as f: f.write(json.dumps(posts)) if 'settings' in env: conn = boto.connect_s3() bucket = conn.get_bucket(env.s3_bucket) key = Key(bucket) key.key = TUMBLR_FILENAME key.set_contents_from_filename( TUMBLR_FILENAME, policy='public-read', headers={ 'Cache-Control': 'max-age=5 no-cache no-store must-revalidate' }) if env.alt_s3_bucket: conn = boto.connect_s3() bucket = conn.get_bucket(env.alt_s3_bucket) key = Key(bucket) key.key = TUMBLR_FILENAME key.set_contents_from_filename( TUMBLR_FILENAME, policy='public-read', headers={ 'Cache-Control': 'max-age=5 no-cache no-store must-revalidate' })
def write_mr_president_json(): """ Writes the JSON for Dear Mr. President to www. """ # # First, handle stuff from the V1 API. This is fetching the posts by tag. # print "V1: Starting." TUMBLR_FILENAME = 'www/live-data/misterpresident.json' TUMBLR_MAX_POSTS = 10000 MAX_PER_CATEGORY = 100 api = Api(app_config.TUMBLR_BLOG_ID) print "V1: API call made." posts = list(api.read(max=TUMBLR_MAX_POSTS)) print "V1: Fetched %s posts." % len(posts) print "V1: Starting to render." output = { 'idrathernotsayhowivoted': [], 'ivotedforyou': [], 'ididntvoteforyou': [], 'ididntvote': [], 'mostpopular': [] } for post in posts: simple_post = { 'id': post['id'], 'url': post['url'], 'text': post['photo-caption'], 'photo_url': post['photo-url-100'], 'photo_url_250': post['photo-url-250'], 'photo_url_500': post['photo-url-500'], 'photo_url_1280': post['photo-url-1280'], 'timestamp': post['unix-timestamp'] } for tag in post['tags']: try: if len(output[tag]) <= MAX_PER_CATEGORY: output[tag].append(simple_post) except KeyError: pass print "V1: Rendering finished." # # Now, fetch the most popular posts using the V2 API. # print "V2: Starting." # Set constants base_url = 'http://api.tumblr.com/v2/blog/inauguration2013.tumblr.com/posts/photo' key_param = '?api_key=Cxp2JzyA03QxmQixf7Fee0oIYaFtBTTHKzRA0AveHlh094bwDH' limit_param = '&limit=20' limit = 20 new_limit = limit post_list = [] # Figure out the total number of posts. r = requests.get(base_url + key_param) total_count = int(json.loads(r.content)['response']['total_posts']) print "V2: %s total posts available." % total_count # Do the pagination math. pages_count = (total_count / limit) pages_remainder = (total_count % limit) if pages_remainder > 0: pages_count += 1 pages = range(0, pages_count) print "V2: %s pages required." % len(pages) # Start requesting pages. # Note: Maximum of 20 posts per page. print "V2: Requesting pages." for page in pages: # Update all of the pagination shenanigans. start_number = new_limit - limit end_number = new_limit if end_number > total_count: end_number = total_count new_limit = new_limit + limit page_param = '&offset=%s' % start_number page_url = base_url + key_param + limit_param + page_param # Actually fetch the page URL. r = requests.get(page_url) posts = json.loads(r.content) for post in posts['response']['posts']: try: note_count = post['note_count'] post_list.append(post) except KeyError: pass # Sort the results first. print "V2: Finished requesting pages." print "V2: Sorting list." post_list = sorted(post_list, key=lambda post: post['note_count'], reverse=True) # Render the sorted list, but slice to just 24 objects per bb. print "V2: Rendering posts from sorted list." for post in post_list[0:24]: default_photo_url = post['photos'][0]['original_size']['url'] simple_post = { 'id': post['id'], 'url': post['post_url'], 'text': post['caption'], 'timestamp': post['timestamp'], 'note_count': post['note_count'], 'photo_url': default_photo_url, 'photo_url_250': default_photo_url, 'photo_url_500': default_photo_url, 'photo_url_1280': default_photo_url } # Handle the new photo assignment. for photo in post['photos'][0]['alt_sizes']: if int(photo['width']) == 100: simple_post['photo-url-100'] = photo['url'] if int(photo['width']) == 250: simple_post['photo_url_250'] = photo['url'] if int(photo['width']) == 500: simple_post['photo_url_500'] = photo['url'] if int(photo['width']) == 1280: simple_post['photo_url_1280'] = photo['url'] output['mostpopular'].append(simple_post) # Ensure the proper sort on our output list. print "V2: Ordering output." output['mostpopular'] = sorted(output['mostpopular'], key=lambda post: post['note_count'], reverse=True) # Write the JSON file. print "All: Producing JSON file at %s." % TUMBLR_FILENAME json_output = json.dumps(output) with open(TUMBLR_FILENAME, 'w') as f: f.write(json_output) print "All: JSON file written." if app_config.DEPLOYMENT_TARGET: with gzip.open(TUMBLR_FILENAME + '.gz', 'wb') as f: f.write(json_output) for bucket in app_config.S3_BUCKETS: conn = boto.connect_s3() bucket = conn.get_bucket(bucket) key = boto.s3.key.Key(bucket) key.key = '%s/live-data/misterpresident.json' % app_config.DEPLOYED_NAME key.set_contents_from_filename( TUMBLR_FILENAME + '.gz', policy='public-read', headers={ 'Cache-Control': 'max-age=5 no-cache no-store must-revalidate', 'Content-Encoding': 'gzip' } ) os.remove(TUMBLR_FILENAME + '.gz')
def testAuthenticate(self): api = Api(BLOG, USER, PASSWORD) api.auth_check()
def populate_models(tumblr_user, user): ''' Takes a tumblr username (string), and a User model. Populates the tumblr models with data from 'tumblr_user'.tumblr.com, and associates the entries with 'user'. ''' tumbls = Api(tumblr_user + ".tumblr.com") for tumbl in tumbls.read(): # Common to all models id = tumbl['id'] pub_date = datetime.datetime.strptime(tumbl['date-gmt'], '%Y-%m-%d %H:%M:%S %Z') # 'Regular' objects. if tumbl['type'] == "regular": if tumbl['regular-title']: title = tumbl['regular-title'] else: title = "" body = tumbl['regular-body'] m = Regular(id=id, pub_date=pub_date, user=user, title=title, body=body) # 'Photo' objects. elif tumbl['type'] == "photo": source = tumbl['photo-url-250'] if tumbl['photo-caption']: caption = tumbl['photo-caption'] else: caption = "" m = Photo(id=id, pub_date=pub_date, user=user, source=source, caption=caption) # 'Quote' objects. elif tumbl['type'] == "quote": quote = tumbl['quote-text'] if tumbl['quote-source']: source = tumbl['quote-source'] else: source = "" m = Quote(id=id, pub_date=pub_date, user=user, quote=quote, source=source) # 'Link' objects. elif tumbl['type'] == "link": if tumbl['link-text']: name = tumbl['link-text'] else: name = "" url = tumbl['link-url'] if tumbl['link-description']: description = tumbl['link-description'] else: description = "" m = Link(id=id, pub_date=pub_date, user=user, name=name, url=url, description=description) # 'Conversation' objects. elif tumbl['type'] == "conversation": if tumbl['conversation-title']: title = tumbl['conversation-title'] else: title = "" m = Conversation(id=id, pub_date=pub_date, user=user, title=title, conversation_text=tumbl['conversation-text']) m.save() # 'Video' objects. elif tumbl['type'] == "video": embed = tumbl['video-player'] if tumbl['video-caption']: caption = tumbl['video-caption'] else: caption = "" m = Video(id=id, pub_date=pub_date, user=user, embed=embed, caption=caption) # 'Audio' objects. elif tumbl['type'] == "audio": embed = tumbl['audio-player'] if tumbl['audio-caption']: caption = tumbl['audio-caption'] else: caption = "" m = Audio(id=id, pub_date=pub_date, user=user, embed=embed, caption=caption) # TODO: Raise error. else: print "ERROR!", tumbl return '' m.save()