當前位置: 首頁>>代碼示例>>Python>>正文


Python Reddit.updateToken方法代碼示例

本文整理匯總了Python中reddit.Reddit.updateToken方法的典型用法代碼示例。如果您正苦於以下問題:Python Reddit.updateToken方法的具體用法?Python Reddit.updateToken怎麽用?Python Reddit.updateToken使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在reddit.Reddit的用法示例。


在下文中一共展示了Reddit.updateToken方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: download_a

# 需要導入模塊: from reddit import Reddit [as 別名]
# 或者: from reddit.Reddit import updateToken [as 別名]
class Client:
    sock = None
    reddit = None
    
    def download_a(self, after=None):
        download_dir = os.path.join(tmpdir, 'client', 'staging')
        posts, nav = self.reddit.getListing('all', after)
        blob = { 'posts': posts, 'nav': nav }
        fp = open(os.path.join(download_dir, 'a_'+after), 'w')
        fp.write(json.dumps(blob))
        fp.close()
        return True

    def download_p(self, pid):
        download_dir = os.path.join(tmpdir, 'client', 'staging')
        self.reddit.updateToken()
        post, comments = self.reddit.getPost(pid)
        blob = { 'post': post, 'comments': comments }
        fp = open(os.path.join(download_dir, 'p_'+pid), 'w')
        fp.write(json.dumps(blob))
        fp.close()
        return True

    def download_u(self, user):
        download_dir = os.path.join(tmpdir, 'client', 'staging')
        url = 'http://www.reddit.com/user/'+user+'.json'
        filename = 'u_'+user
        data = self.download_get(url, os.path.join(download_dir, filename), True)
        if data == '':
            return False
        blob = json.loads(data)
        nav = parser.extract_listing_nav(blob)
        while nav['after'] is not None:
            newurl = url+'?after='+nav['after']
            filename = 'u_'+user+'_'+nav['after']
            data = self.download_get(newurl, os.path.join(download_dir, filename), True)
            if data == '':
                return False
            blob = json.loads(data)
            nav = parser.extract_listing_nav(blob)
        return True
    
    def download_req(self, req):
        # format of request:
        # | a | <pid>
        # | p | <pid>
        # | u | <username> | <after>
        res = True
        if req[0] == 'a':
            res = self.download_a(req[1])
        elif req[0] == 'p':
            res = self.download_p(req[1])
        elif req[0] == 'u':
            res = self.download_u(req[1])
        return res

    def download_data(self, reqlist):
        for req in reqlist:
            self.download_req(req)
            print '  -- '+str(req)
    
    def connect(self, host, port):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((host, port))
        self.sock = sock
        fp = sock.makefile('rb+')
        print 'Connected to '+host+':'+str(port)
        return fp
    
    def close(self):
        self.sock.close()
        self.sock = None
    
    def cleanup(self):
        download_dir = os.path.join(tmpdir, 'client', 'staging')
        for entry in os.listdir(download_dir):
            os.unlink(os.path.join(download_dir, entry))
        os.unlink(os.path.join(tmpdir, 'client', 'archive.tar.gz'))

    def targz(self):
        return shutil.make_archive(os.path.join(tmpdir, 'client', 'archive'), 'gztar', os.path.join(tmpdir, 'client', 'staging'))

    def run(self, host, port):
        self.reddit = Reddit(creds.key, creds.secret, creds.username, creds.password, creds.redirect_uri)
        self.reddit.updateToken()
        self.reddit.testAccess()
        sleeptime = 0
        while True:
            if sleeptime > 10:
                time.sleep(10)
            elif sleeptime > 1:
                time.sleep(1)
            # Connect to host:port, get the fp
            fp = self.connect(host, port)
        
            # Send hostname of client over initially
            hostname = socket.getfqdn()
            fp.write(hostname+'\n')
            fp.flush()
            if debug:
#.........這裏部分代碼省略.........
開發者ID:aleboz,項目名稱:reddit-crawler,代碼行數:103,代碼來源:crawler.py


注:本文中的reddit.Reddit.updateToken方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。