コード例 #1
0
ファイル: pdfgen.py プロジェクト: levin108/tweets2pdf
    def _process_status(self, status_entry = {}):

        tweets_table_row = []

        user = status_entry.getElementsByTagName('user')[0]
        userinfo = twitter.ttuser()
        userinfo.load_user_info(user)

        tweets_table_row.append('')

        statusinfo = twitter.ttstatus(self.link_color)
        try:
            statusinfo.load_status(status_entry)
        except:
            return

        if statusinfo.tweets_id > self.tweets_max_id:
            self.tweets_max_id = statusinfo.tweets_id

        if self.tweets_min_id == 0:
            self.tweets_min_id     = statusinfo.tweets_id
        else:
            if statusinfo.tweets_id < self.tweets_min_id:
                self.tweets_min_id = statusinfo.tweets_id

        print "tweets id is : %s" % statusinfo.tweets_id

        tweets_body = template.TWEETS_TEMPLATE % (
                            self.link_color,      \
                            userinfo.screen_name, \
                            userinfo.screen_name, \
                            userinfo.name + '  ', \
                            statusinfo.create_at, \
                            statusinfo.source,    \
                            statusinfo.status_text)
        print tweets_body

        try:
            tweets_paragraph = Paragraph(tweets_body, self.text_font)
            tweets_table_row.append(tweets_paragraph)
            tweets_table_row.append(userinfo)
            self.tweets_table_data.append(tweets_table_row)
        except:
            pass
コード例 #2
0
ファイル: pdfgen.py プロジェクト: levin108/tweets2pdf
    def generate_cover(self):

        # Fetching User Information
        self.main_panel.update_status(status_fetch_user_information)
        response = self.oauth_client.fetch_user_information(self.screen_name)
        if type(response) != types.StringType:
            try:
                if response.code == 404:
                    self.main_panel.show_done('@%s not exist' % self.screen_name)
                    return False
                else:
                    self.main_panel.show_done('Twitter API limited,please retry later.')
                    return False
            except:
                self.main_panel.show_done('Twitter API limited,please retry later.')
                return False

        doc = minidom.parseString(response).documentElement
        userinfo = twitter.ttuser()
        userinfo.load_user_info(doc)
        self.screen_name = userinfo.screen_name

        if self.tweets_count is None or self.tweets_count == 0:
            self.tweets_count = int(userinfo.status_count)

        if not self.has_cover:
            return True

        # generate information of the user
        table_data = []
        table_row  = []
        table_data.append([Spacer(0, 100)])
        profile_image_name = self.oauth_client.fetch_profile_image(
                screen_name = userinfo.screen_name,
                size = 'bigger',
                file_type = userinfo.image_type)
        try:
            profile_image = Image(profile_image_name, 73, 73)
            table_row.append(profile_image)
        except:
            table_row.append('')
        
        if userinfo.url == '#':
            desc = Paragraph(template.PROFILE_TEMPLATE_NO_URL % ( \
                userinfo.name + '  ', \
                userinfo.screen_name, \
                userinfo.location, \
                userinfo.description
                ), self.text_font)
        else:
            desc = Paragraph(template.PROFILE_TEMPLATE % ( \
                userinfo.name + '  ', \
                userinfo.screen_name, \
                userinfo.location, \
                userinfo.description, \
                userinfo.url, \
                self.link_color, \
                userinfo.url
                ), self.text_font)

        table_row.append(desc)
        table_data.append(table_row)

        cover_table = Table(table_data, colWidths = [80, self.cover_table_width - 120])
        self.elements.append(cover_table)

        table_data = []
        table_data.append([ 
                Paragraph('<b>Tweets  </b><font color="blue">%s</font>' % \
                              userinfo.status_count, self.text_font)
                ])

        status_node = doc.getElementsByTagName('status')
        if len(status_node) > 0:
            statusinfo = twitter.ttstatus(self.link_color)
            statusinfo.load_status(status_node[0])
            table_data.append([
                    Paragraph('<font color="grey"><b>%s: </b></font>%s' % \
                                  ( statusinfo.create_at, statusinfo.status_text), self.text_font)
                    ])
            table_data.append([ Spacer(0, 10)])

        table_data.append([
            Paragraph('<b>Followers </b><font color="blue">%s</font>' % \
                          userinfo.followers_count, self.text_font)
            ])
        
        table_data = self._generate_grid(table_data, self.screen_name, 'follower')

        table_data.append([
            Paragraph('<b>Following </b><font color="blue">%s</font>' % \
                          userinfo.friends_count, self.text_font)
            ])

        #generate information of the following account
        table_data = self._generate_grid(table_data, self.screen_name, 'following')
        table_data.append([Spacer(0, 20)])

        cover_table = Table(table_data, colWidths = [self.cover_table_width])
        self.elements.append(cover_table)
        return True
コード例 #3
0
ファイル: pdfgen.py プロジェクト: levin108/tweets2pdf
    def _generate_grid(self, table_data = {}, screen_name = {}, grid_type = 'follower'):

        cursor         = '-1'
        COUNT_PER_LINE = 18
        LINE_COUNT     = 3
        count          = 0
        grid_ok        = False

        threadlist = []
        userlist = []
        self.cover_portrait_count = COUNT_PER_LINE * LINE_COUNT
        self.main_panel.update_progress('0/54', 0)
        self.main_panel.update_status(status_fetch_followers if grid_type == 'follower' else status_fetch_friends)
        #generate profile image grid
        while True:
            if grid_type == 'follower':
                # Fetching Follower...
                response = self.oauth_client.fetch_followers(screen_name, cursor = cursor)
                if type(response) != types.StringType:
                    continue
            else:
                # Fetching Friends...
                response = self.oauth_client.fetch_friends(screen_name, cursor = cursor)
                if type(response) != types.StringType:
                    continue

            doc = minidom.parseString(response).documentElement
            for user in doc.getElementsByTagName('user'):
                userinfo = twitter.ttuser()
                userinfo.load_user_info(user)
                userlist.append(userinfo)
                count += 1
                if count == self.cover_portrait_count:
                    grid_ok = True
                    break
            self.main_panel.update_progress('%s/54' % count, float(count)/float(54))
            if count < self.cover_portrait_count:
                self.cover_portrait_count = count
            if grid_ok:
                break
            next_cursor = doc.getElementsByTagName('next_cursor')[0]
            cursor = next_cursor.childNodes[0].data
            if cursor == '0':
                break
        
        thread_count = len(userlist)

        if thread_count == 0:
            return []

        count_per_thread = len(userlist) / thread_count

        self.main_panel.update_status(status_fetch_followers_profile_image \
                if grid_type == 'follower' else status_fetch_friends_profile_image)
        self.main_panel.update_progress('0/%d' % count, 0)
        global mutex
        global portrait_already_got
        mutex = threading.Lock()
        portrait_already_got = 0

        for i in range(0, thread_count):
            threadlist.append(
                    threading.Thread(target = fetch_image_thread, \
                            args = [[
                                 userlist[i], \
                                 self.oauth_client, \
                                 self.cover_portrait_count, \
                                 self.main_panel
                                 ]]))
        
        for thread in threadlist:
            thread.start()

        for thread in threadlist:
            thread.join()

        follower_table_data = []
        follower_table_row = []

        line = 0
        for userinfo in userlist:
            profile_image = self.oauth_client.fetch_profile_image(
                    screen_name = userinfo.screen_name,
                    file_type = userinfo.image_type)
            desc = Image(profile_image, 20, 20)

            if line % COUNT_PER_LINE == 0 and line != 0:
                follower_table_data.append(follower_table_row)
                follower_table_row = []
                line = 0
            follower_table_row.append(desc)
            line += 1

        if len(follower_table_row) != 0:
            follower_table_data.append(follower_table_row)

        if count > 0:
            follower_table = Table(follower_table_data, [22,])
            table_data.append([follower_table])
        return table_data