当前位置: 首页>>代码示例>>Python>>正文


Python Reddit.testAccess方法代码示例

本文整理汇总了Python中reddit.Reddit.testAccess方法的典型用法代码示例。如果您正苦于以下问题:Python Reddit.testAccess方法的具体用法?Python Reddit.testAccess怎么用?Python Reddit.testAccess使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在reddit.Reddit的用法示例。


在下文中一共展示了Reddit.testAccess方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: download_a

# 需要导入模块: from reddit import Reddit [as 别名]
# 或者: from reddit.Reddit import testAccess [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.testAccess方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。