def main(): print 'start at %s' % time.asctime() users = db.select("users") print 'current users count %s ' % len(users) for user in users: # print 'user %s ' % user.token # print 'user %s ' % user.secret access_token = OAuthToken(user.token, user.secret) if not user.trunk_key: continue t = Trunkly(user.trunk_key) sinat = Sinat(sinaConsumer, access_token=access_token) statuses = sinat.statuses__user_timeline('GET') for status in statuses: weibo = status['text'] if status.has_key('retweeted_status'): weibo = '%s //@%s: %s' % (weibo , status['retweeted_status']['user']['name'], status['retweeted_status']['text']) # print 'status %s' % status['text'] urls = p.findall(weibo) for url in urls: print 'url is %s ' % url title = None trunk = None try: html = lxml.html.parse(url) title = html.find(".//title").text url = html.getroot().base_url print 'title is %s' % title print 'base url is %s ' % url try: try: trunk = t.get_link(parameters={'url': url}) print 'url Already exists!!!' continue except: print 'error' pass if title and not trunk: print 'post url to trunk.ly' t.post_link(parameters={'url': url, 'title': title, 'tags' : '', 'note' : weibo, 'text' : weibo}) except: print 'post to trunk error. url %s title %s' % (url, title) except: print 'url %s fetch error' % (url) print '---------------- end ---------------------'
def POST(self): if not session.get('logged' ,False): web.seeother("/") data = web.input() username = data.username password = data.password logger.debug('username %s ' % username) if username and password: try: logger.debug('request api_key ') t = Trunkly() apikey = t.get_api_key(username=username,password=password) logger.debug('api_key is %s' % apikey) db.update('users',vars=dict(passport=session.uid, provider=session.provider), where='passport = $passport and provider = $provider ',trunk_key=apikey['api_key']) session.trunk_key = apikey['api_key'] except: logger.debug("%s" % Exception) return render.trunk(username=username , error="Trunk.ly认证失败.") web.seeother("/home")
#!/usr/bin/env python # encoding: utf-8 from trunkly import Trunkly import hashlib TRUNK_KEY = "your api key" if __name__ == "__main__": t = Trunkly(TRUNK_KEY) # print 'check /api/v1/user/' # data = t.user() # # print 'name %s' % data['username'] # for k, v in data.iteritems(): # print '%s = %s' % (k,v ) # # print 'check /api/v1/user/id/<uid>/' # #a user object for specified user ID # user = t.user_id__uid(parameters={'uid':data['uid']},method='GET') # for k, v in user.iteritems(): # print '%s = %s' % (k,v ) # # print 'check /api/v1/user/followers/' # followings = t.get_user_followers() # for k, v in followings.iteritems(): # print '%s = %s' % (k,v ) # # print 'check /api/v1/link/' # link = t.post_link(parameters={ # 'url': 'http://blog.roynotes.com', # 'title': 'roy的博客'