def deletePosts(params, ids): c,cs = tokens.loadConsumer() tapi = api.API(c, cs, tokens.loadAccessToken) #we must sign our requests - access token is required for id in ids: #for every post id = id.strip() try: tapi.deletePost(params.blog, id) #deleting post except api.NotFound as E: #no such post (or blog) if not params.quiet: print >>stderr, '#{id}: post not found'.format(id=id, blog=params.blog) failure = True except api.Unauthorized as E: #we don't have access token if not params.quiet: print >>stderr, '#{id}: access denied to blog {blog}'.format(id=id, blog=params.blog) failure = True except api.Error as E: #I just don't know what went wrong! if not params.quiet: print >>stderr, '#{id}: {msg}'.format(id=id, msg=E.message) failure = True else: #Everything is fine failure = False if failure and (params.outcome=='fail'): #if something bad happened and we must fail exit(1) # then we fail. elif (not failure or (params.outcome=='ignore')): print >>stdout, id
def retagPosts(params, ids): c,cs = tokens.loadConsumer() tapi = api.API(c, cs, tokens.loadAccessToken) #we must sign our requests - access token is required for id in ids: #for every post id = id.strip() try: post = tapi.retrievePost(params.blog, id) actions = expandTags(params.actions, post) post['tags'] = changeTags(actions, post['tags']) tapi.updatePost(params.blog, post) except api.NotFound as E: #no such post (or blog) if not params.quiet: print >>stderr, '#{id}: post not found'.format(id=id, blog=params.blog) failure = True except api.Unauthorized as E: #we don't have access token if not params.quiet: print >>stderr, '#{id}: access denied to blog {blog}'.format(id=id, blog=params.blog) failure = True except api.Error as E: #I just don't know what went wrong! if not params.quiet: print >>stderr, '#{id}: {msg}'.format(id=id, msg=E.message) failure = True else: #Everything is fine failure = False if not failure or (params.error=='ignore'): print >>stdout, params.format(post) elif params.error=='fail': exit(1)
def retrievePosts(params): '''Retrieves posts via Tumblr API, utilizing some filtering conditions in the process.''' c,cs = tokens.loadConsumer() tapi = api.API(c, cs, tokens.loadAccessToken) posttype = params.type if params.type!=u'all' else u'' req_params = { #pre-retrieval filter } if params.id is not None: #post id (if specified) req_params['id'] = params.id if params.tags: #tag for tag,rule in dict(params.tags).iteritems(): #checking tags list (duplicate tags removed) if rule=='+': #if there is a required tag... req_params['tag'] = tag #set this tag as search criteria break #Tumblr lets us set only one required tag as search criteria. Other tag restrictions will be enforced later. #portion size - how many posts will be retrieved per query if params.slice is None: if params.count is not None: req_params['limit'] = str(params.count) else: req_params['limit'] = str(params.slice) if params.plaintext: #use plain text versions req_params['filter'] = 'text' flt = PostFilter(params) #setting up post-retrieval filter offset = params.offset #initial offset end = offset+1 #initial end value try: while offset<end: #as long as there are more posts to retrieve req_params['offset'] = str(offset) #new offset data = tapi.listPosts(params.blog, req_params, posttype) #decoded API response total = data['total_posts'] #updating total post count part = data['posts'] #post list offset += len(part) #how many posts have we retrieved? limit = total if params.count is None else params.count end = min(total, params.offset+limit) #determining new post count try: for post in part: #returning filtered posts one by one if flt(post): yield post except NoMoreMatchesExpected: break # # except api.NotFound as E: if (params.id is not None): raise api.NotFound("post #{id}".format(id=params.id)) else: raise
def editPosts(params, ids): c,cs = tokens.loadConsumer() tapi = api.API(c, cs, tokens.loadAccessToken) #we must sign our requests - access token is required processPost = performRegexp if params.useregexp else performReplacement #determining method for id in ids: #for every post id = id.strip() post = { u'id':id } #stub. In case of post retrieval failure we will have the ID. try: post = tapi.retrievePost(params.blog, id) #retrieving post changed = processPost(params.body, post, args.FieldMap.body) #changing body if params.title: if (post['type']=='answer'): #we can't change question raise api.Error('Can\'t edit question text.') else: #change title (or equivalent part) changed_title = processPost(params.title, post, args.FieldMap.title) changed = changed or changed_title tapi.updatePost(params.blog, post) #submitting post back except api.NotFound as E: #no such post (or blog) if not params.quiet: print >>stderr, '#{id}: post not found'.format(id=id, blog=params.blog) failure = True except api.Unauthorized as E: #we don't have access token if not params.quiet: print >>stderr, '#{id}: access denied to blog {blog}'.format(id=id, blog=params.blog) failure = True except api.Error as E: #I just don't know what went wrong! if not params.quiet: print >>stderr, '#{id}: {msg}'.format(id=id, msg=E.message) failure = True else: #Everything is fine failure = False if failure and (params.outcome=='fail'): #if something bad happened and we must fail exit(1) # then we fail. elif (not failure or (params.outcome=='ignore')) and \ (not params.editonly or changed): print >>stdout, params.format(post)