Exemplo n.º 1
0
    def update_buffer(self):
        self.buffer.clear()

        maxx = self.window.getmaxyx()[1]
        c = 1

        longest_metadata_string_len = 0
        for n in self.timeline:
            if "direct" in self.timeline_type:
                user_string = "%s -> %s" % (n["sender"]["screen_name"],
                                            n["recipient"]["screen_name"])
                source_msg = ""
            else:
                user_string = "%s" % (n["user"]["screen_name"])
                raw_source_msg = "from %s" % (n["source"])
                source_msg = self.html_regex.sub("", raw_source_msg)
            if "in_reply_to_status_id" in n and n[
                    "in_reply_to_status_id"] is not None:
                if not config.config["show_source"]:
                    user_string += " +"
                else:
                    source_msg += " [+]"
            if "retweeted_status" in n:
                user_string = "%s [%s's RD]" % (
                    n["retweeted_status"]["user"]["screen_name"],
                    n["user"]["screen_name"])
                if "in_reply_to_status_id" in n["retweeted_status"]:
                    if not config.config["show_source"]:
                        user_string += " +"
                    else:
                        source_msg += " [+]"
            datetime_notice = helpers.notice_datetime(n)
            time_msg = helpers.format_time(helpers.time_since(datetime_notice),
                                           short_form=True)
            metadata_string = time_msg + " " + user_string
            if config.config["show_source"]:
                metadata_string += " " + source_msg
            if len(metadata_string) > longest_metadata_string_len:
                longest_metadata_string_len = len(metadata_string)

        for n in self.timeline:
            from_user = None
            to_user = None
            repeating_user = None
            if "direct" in self.timeline_type:
                from_user = n["sender"]["screen_name"]
                to_user = n["recipient"]["screen_name"]
                source_msg = ""
            else:
                if "retweeted_status" in n:
                    repeating_user = n["user"]["screen_name"]
                    n = n["retweeted_status"]
                from_user = n["user"]["screen_name"]
                raw_source_msg = "from %s" % (n["source"])
                source_msg = self.html_regex.sub("", raw_source_msg)
                repeat_msg = ""
                if n["in_reply_to_status_id"] is not None:
                    source_msg += " [+]"
            datetime_notice = helpers.notice_datetime(n)

            time_msg = helpers.format_time(helpers.time_since(datetime_notice),
                                           short_form=True)

            for user in [
                    user for user in [from_user, to_user, repeating_user]
                    if user is not None
            ]:
                if not user in config.session_store.user_cache:
                    config.session_store.user_cache[user] = random.choice(
                        identicurse.base_colours.items())[1]

            if "ic__paused_on" in n and c != 1:
                self.buffer.append([("-",
                                     identicurse.colour_fields["pause_line"])])
                self.buffer.append([("", identicurse.colour_fields["none"])])

            # Build the line
            line = []

            if c < 10:
                cout = " " + str(c)
            else:
                cout = str(c)
            line.append((cout, identicurse.colour_fields["notice_count"]))

            if (c - 1) == self.chosen_one:
                line.append((' * ', identicurse.colour_fields["selector"]))
            else:
                line.append((' ' * 3, identicurse.colour_fields["selector"]))

            if config.config['compact_notices']:
                line.append((time_msg, identicurse.colour_fields["time"]))
                line.append((" ", identicurse.colour_fields["none"]))

            if config.config['user_rainbow']:
                line.append(
                    (from_user, config.session_store.user_cache[from_user]))
            else:
                line.append((from_user, identicurse.colour_fields["username"]))
            user_length = len(from_user)

            if to_user is not None:
                line.append((" -> ", identicurse.colour_fields["none"]))
                if config.config['user_rainbow']:
                    line.append(
                        (to_user, config.session_store.user_cache[to_user]))
                else:
                    line.append(
                        (to_user, identicurse.colour_fields["username"]))
                user_length += (len(" -> ") + len(to_user))

            if repeating_user is not None:
                if config.config["compact_notices"]:
                    line.append((" [", identicurse.colour_fields["none"]))
                else:
                    line.append(
                        (" [ repeat by ", identicurse.colour_fields["none"]))

                if config.config['user_rainbow']:
                    line.append(
                        (repeating_user,
                         config.session_store.user_cache[repeating_user]))
                else:
                    line.append((repeating_user,
                                 identicurse.colour_fields["username"]))

                if config.config["compact_notices"]:
                    line.append(("'s RD]", identicurse.colour_fields["none"]))
                    user_length += (len(" [") + len(repeating_user) +
                                    len("'s RD]"))
                else:
                    line.append((" ]", identicurse.colour_fields["none"]))
                    user_length += (len(" [ repeat by ") +
                                    len(repeating_user) + len(" ]"))

            if not config.config['compact_notices']:
                if config.config["show_source"]:
                    line.append(
                        (' ' *
                         (maxx -
                          ((len(source_msg) + len(time_msg) + user_length +
                            (6 + len(cout))))),
                         identicurse.colour_fields["none"]))
                else:
                    line.append((' ' * (maxx - ((len(time_msg) + user_length +
                                                 (5 + len(cout))))),
                                 identicurse.colour_fields["none"]))
                line.append((time_msg, identicurse.colour_fields["time"]))
                if config.config["show_source"]:
                    line.append((' ', identicurse.colour_fields["none"]))
                    line.append(
                        (source_msg, identicurse.colour_fields["source"]))
                self.buffer.append(line)
                line = []
            else:
                detail_char = ""
                if (not config.config["show_source"]):
                    if "in_reply_to_status_id" in n and n[
                            "in_reply_to_status_id"] is not None:
                        detail_char = "+"
                    elif "retweeted_status" in n:
                        detail_char = "~"
                    line.append((" %s" % (detail_char),
                                 identicurse.colour_fields["source"]))
                if config.config["show_source"]:
                    line.append((" " + source_msg,
                                 identicurse.colour_fields["source"]))
                    line.append((" " * (
                        (longest_metadata_string_len -
                         (user_length + len(time_msg) + len(source_msg) + 2))),
                                 identicurse.colour_fields["none"]))
                else:
                    if detail_char == "":
                        line.append((" ", identicurse.colour_fields["none"]))
                    line.append((" " * ((longest_metadata_string_len -
                                         (user_length + len(time_msg) + 1))),
                                 identicurse.colour_fields["none"]))
                line.append((" | ", identicurse.colour_fields["none"]))

            try:
                notice_entities = helpers.split_entities(n['text'])
                for entity in notice_entities:
                    if len(entity['text']) > 0:
                        if entity['type'] in ['user', 'group', 'tag']:
                            entity_text_no_symbol = entity['text'][1:]
                            cache = getattr(config.session_store,
                                            '%s_cache' % (entity['type']))
                            if not entity_text_no_symbol in cache:
                                cache[entity_text_no_symbol] = random.choice(
                                    identicurse.base_colours.items())[1]
                            if config.config['%s_rainbow' % (entity['type'])]:
                                line.append((entity['text'],
                                             cache[entity_text_no_symbol]))
                            else:
                                if entity['type'] == "user":
                                    line.append((
                                        entity['text'],
                                        identicurse.colour_fields["username"]))
                                else:
                                    line.append((entity['text'],
                                                 identicurse.colour_fields[
                                                     entity['type']]))
                        else:
                            line.append((entity['text'],
                                         identicurse.colour_fields["notice"]))

                self.buffer.append(line)

            except UnicodeDecodeError:
                self.buffer.append([
                    ("Caution: Terminal too shit to display this notice.",
                     identicurse.colour_fields["warning"])
                ])

            if config.config["show_notice_links"]:
                line = []
                base_url = helpers.base_url_regex.findall(
                    self.conn.api_path)[0][0]
                if self.timeline_type in ["direct", "sentdirect"]:
                    notice_link = "%s/message/%s" % (base_url, str(n["id"]))
                else:
                    notice_link = "%s/notice/%s" % (base_url, str(n["id"]))
                line.append(("<%s>" % (notice_link),
                             identicurse.colour_fields["notice_link"]))
                self.buffer.append(line)

            if not config.config['compact_notices']:
                self.buffer.append([])

            c += 1
Exemplo n.º 2
0
    def update_buffer(self):
        self.buffer.clear()

        maxx = self.window.getmaxyx()[1]
        c = 1

        longest_metadata_string_len = 0
        for n in self.timeline:
            if "direct" in self.timeline_type:
                user_string = "%s -> %s" % (n["sender"]["screen_name"], n["recipient"]["screen_name"])
                source_msg = ""
            else:
                user_string = "%s" % (n["user"]["screen_name"])
                raw_source_msg = "from %s" % (n["source"])
                source_msg = self.html_regex.sub("", raw_source_msg)
            if "in_reply_to_status_id" in n and n["in_reply_to_status_id"] is not None:
                if not config.config["show_source"]:
                    user_string += " +"
                else:
                    source_msg += " [+]"
            if "retweeted_status" in n:
                user_string = "%s [%s's RD]" % (n["retweeted_status"]["user"]["screen_name"], n["user"]["screen_name"])
                if "in_reply_to_status_id" in n["retweeted_status"]:
                    if not config.config["show_source"]:
                        user_string += " +"
                    else:
                        source_msg += " [+]"
            datetime_notice = helpers.notice_datetime(n)
            time_msg = helpers.format_time(helpers.time_since(datetime_notice), short_form=True)
            metadata_string = time_msg + " " + user_string
            if config.config["show_source"]:
                metadata_string += " " + source_msg
            if len(metadata_string) > longest_metadata_string_len:
                longest_metadata_string_len = len(metadata_string)

        for n in self.timeline:
            from_user = None
            to_user = None
            repeating_user = None
            if "direct" in self.timeline_type:
                from_user = n["sender"]["screen_name"]
                to_user = n["recipient"]["screen_name"]
                source_msg = ""
            else:
                if "retweeted_status" in n:
                    repeating_user = n["user"]["screen_name"]
                    n = n["retweeted_status"]
                from_user = n["user"]["screen_name"]
                raw_source_msg = "from %s" % (n["source"])
                source_msg = self.html_regex.sub("", raw_source_msg)
                repeat_msg = ""
                if n["in_reply_to_status_id"] is not None:
                    source_msg += " [+]"
            datetime_notice = helpers.notice_datetime(n)

            time_msg = helpers.format_time(helpers.time_since(datetime_notice), short_form=True)

            for user in [user for user in [from_user, to_user, repeating_user] if user is not None]:
                if not user in config.session_store.user_cache:
                    config.session_store.user_cache[user] = random.choice(identicurse.base_colours.items())[1]

            if "ic__paused_on" in n and c != 1:
                self.buffer.append([("-", identicurse.colour_fields["pause_line"])])
                self.buffer.append([("", identicurse.colour_fields["none"])])

            # Build the line
            line = []

            if c < 10:
                cout = " " + str(c)
            else:
                cout = str(c)
            line.append((cout, identicurse.colour_fields["notice_count"]))

            if (c - 1) == self.chosen_one:
                line.append((" * ", identicurse.colour_fields["selector"]))
            else:
                line.append((" " * 3, identicurse.colour_fields["selector"]))

            if config.config["compact_notices"]:
                line.append((time_msg, identicurse.colour_fields["time"]))
                line.append((" ", identicurse.colour_fields["none"]))

            if config.config["user_rainbow"]:
                line.append((from_user, config.session_store.user_cache[from_user]))
            else:
                line.append((from_user, identicurse.colour_fields["username"]))
            user_length = len(from_user)

            if to_user is not None:
                line.append((" -> ", identicurse.colour_fields["none"]))
                if config.config["user_rainbow"]:
                    line.append((to_user, config.session_store.user_cache[to_user]))
                else:
                    line.append((to_user, identicurse.colour_fields["username"]))
                user_length += len(" -> ") + len(to_user)

            if repeating_user is not None:
                if config.config["compact_notices"]:
                    line.append((" [", identicurse.colour_fields["none"]))
                else:
                    line.append((" [ repeat by ", identicurse.colour_fields["none"]))

                if config.config["user_rainbow"]:
                    line.append((repeating_user, config.session_store.user_cache[repeating_user]))
                else:
                    line.append((repeating_user, identicurse.colour_fields["username"]))

                if config.config["compact_notices"]:
                    line.append(("'s RD]", identicurse.colour_fields["none"]))
                    user_length += len(" [") + len(repeating_user) + len("'s RD]")
                else:
                    line.append((" ]", identicurse.colour_fields["none"]))
                    user_length += len(" [ repeat by ") + len(repeating_user) + len(" ]")

            if not config.config["compact_notices"]:
                if config.config["show_source"]:
                    line.append(
                        (
                            " " * (maxx - ((len(source_msg) + len(time_msg) + user_length + (6 + len(cout))))),
                            identicurse.colour_fields["none"],
                        )
                    )
                else:
                    line.append(
                        (
                            " " * (maxx - ((len(time_msg) + user_length + (5 + len(cout))))),
                            identicurse.colour_fields["none"],
                        )
                    )
                line.append((time_msg, identicurse.colour_fields["time"]))
                if config.config["show_source"]:
                    line.append((" ", identicurse.colour_fields["none"]))
                    line.append((source_msg, identicurse.colour_fields["source"]))
                self.buffer.append(line)
                line = []
            else:
                detail_char = ""
                if not config.config["show_source"]:
                    if "in_reply_to_status_id" in n and n["in_reply_to_status_id"] is not None:
                        detail_char = "+"
                    elif "retweeted_status" in n:
                        detail_char = "~"
                    line.append((" %s" % (detail_char), identicurse.colour_fields["source"]))
                if config.config["show_source"]:
                    line.append((" " + source_msg, identicurse.colour_fields["source"]))
                    line.append(
                        (
                            " " * ((longest_metadata_string_len - (user_length + len(time_msg) + len(source_msg) + 2))),
                            identicurse.colour_fields["none"],
                        )
                    )
                else:
                    if detail_char == "":
                        line.append((" ", identicurse.colour_fields["none"]))
                    line.append(
                        (
                            " " * ((longest_metadata_string_len - (user_length + len(time_msg) + 1))),
                            identicurse.colour_fields["none"],
                        )
                    )
                line.append((" | ", identicurse.colour_fields["none"]))

            try:
                notice_entities = helpers.split_entities(n["text"])
                for entity in notice_entities:
                    if len(entity["text"]) > 0:
                        if entity["type"] in ["user", "group", "tag"]:
                            entity_text_no_symbol = entity["text"][1:]
                            cache = getattr(config.session_store, "%s_cache" % (entity["type"]))
                            if not entity_text_no_symbol in cache:
                                cache[entity_text_no_symbol] = random.choice(identicurse.base_colours.items())[1]
                            if config.config["%s_rainbow" % (entity["type"])]:
                                line.append((entity["text"], cache[entity_text_no_symbol]))
                            else:
                                if entity["type"] == "user":
                                    line.append((entity["text"], identicurse.colour_fields["username"]))
                                else:
                                    line.append((entity["text"], identicurse.colour_fields[entity["type"]]))
                        else:
                            line.append((entity["text"], identicurse.colour_fields["notice"]))

                self.buffer.append(line)

            except UnicodeDecodeError:
                self.buffer.append(
                    [("Caution: Terminal too shit to display this notice.", identicurse.colour_fields["warning"])]
                )

            if config.config["show_notice_links"]:
                line = []
                base_url = helpers.base_url_regex.findall(self.conn.api_path)[0][0]
                if self.timeline_type in ["direct", "sentdirect"]:
                    notice_link = "%s/message/%s" % (base_url, str(n["id"]))
                else:
                    notice_link = "%s/notice/%s" % (base_url, str(n["id"]))
                line.append(("<%s>" % (notice_link), identicurse.colour_fields["notice_link"]))
                self.buffer.append(line)

            if not config.config["compact_notices"]:
                self.buffer.append([])

            c += 1
Exemplo n.º 3
0
    def update(self):
        self.update_name()

        if self.paused:
            self.update_buffer()
            return

        get_count = config.config['notice_limit']

        if self.prev_page != self.page:
            self.timeline = []

        last_id = 0
        if len(self.timeline) > 0:
            for notice in self.timeline:
                if notice[
                        "ic__from_web"]:  # don't consider inserted posts latest
                    last_id = notice['id']
                    break

        if self.timeline_type == "home":
            raw_timeline = self.conn.statuses_home_timeline(count=get_count,
                                                            page=self.page,
                                                            since_id=last_id)
        elif self.timeline_type == "mentions":
            raw_timeline = self.conn.statuses_mentions(count=get_count,
                                                       page=self.page,
                                                       since_id=last_id)
        elif self.timeline_type == "direct":
            raw_timeline = self.conn.direct_messages(count=get_count,
                                                     page=self.page,
                                                     since_id=last_id)
        elif self.timeline_type == "user":
            raw_timeline = self.conn.statuses_user_timeline(
                user_id=self.type_params['user_id'],
                screen_name=self.type_params['screen_name'],
                count=get_count,
                page=self.page,
                since_id=last_id)
        elif self.timeline_type == "group":
            raw_timeline = self.conn.statusnet_groups_timeline(
                group_id=self.type_params['group_id'],
                nickname=self.type_params['nickname'],
                count=get_count,
                page=self.page,
                since_id=last_id)
        elif self.timeline_type == "tag":
            raw_timeline = self.conn.statusnet_tags_timeline(
                tag=self.type_params['tag'],
                count=get_count,
                page=self.page,
                since_id=last_id)
        elif self.timeline_type == "sentdirect":
            raw_timeline = self.conn.direct_messages_sent(count=get_count,
                                                          page=self.page,
                                                          since_id=last_id)
        elif self.timeline_type == "public":
            raw_timeline = self.conn.statuses_public_timeline()
        elif self.timeline_type == "favourites":
            raw_timeline = self.conn.favorites(page=self.page,
                                               since_id=last_id)
        elif self.timeline_type == "search":
            raw_timeline = self.conn.search(self.type_params['query'],
                                            page=self.page,
                                            standardise=True,
                                            since_id=last_id)
        elif self.timeline_type == "context":
            raw_timeline = []
            if last_id == 0:  # don't run this if we've already filled the timeline
                next_id = self.type_params['notice_id']
                while next_id is not None:
                    notice = self.conn.statuses_show(id=next_id)
                    raw_timeline.append(notice)
                    if "retweeted_status" in notice:
                        next_id = notice['retweeted_status']['id']
                    else:
                        next_id = notice['in_reply_to_status_id']

        self.prev_page = self.page

        temp_timeline = []
        old_ids = [n['id'] for n in self.timeline]

        for notice in raw_timeline:
            notice["ic__raw_datetime"] = helpers.notice_datetime(notice)
            notice["ic__from_web"] = True
            passes_filters = True
            if notice['id'] in old_ids:
                passes_filters = False
                continue
            for filter_item in config.config['filters']:
                if filter_item.lower() in notice['text'].lower():
                    passes_filters = False
                    break
            if passes_filters:
                if (not self.timeline_type in ["direct", "sentdirect"]
                    ) and notice["source"] == "ostatus" and config.config[
                        'expand_remote'] and "attachments" in notice:
                    import urllib2
                    for attachment in notice['attachments']:
                        if attachment['mimetype'] != "text/html":
                            continue
                        req = urllib2.Request(attachment['url'])
                        try:
                            page = urllib2.urlopen(req).read()
                            try:
                                notice['text'] = helpers.html_unescape_string(
                                    helpers.title_regex.findall(page)[0])
                            except IndexError:  # no title could be found
                                pass
                        except:  # link was broken
                            pass
                        break
                temp_timeline.append(notice)

        if self.timeline_type in [
                "direct", "mentions"
        ]:  # alert on changes to these. maybe config option later?
            if (len(self.timeline) > 0) and (
                    len(temp_timeline) > 0
            ):  # only fire when there's new stuff _and_ we've already got something in the timeline
                if config.config['notify'] == 'flash':
                    curses.flash()
                elif config.config['notify'] == 'beep':
                    curses.beep()

        if len(self.timeline) == 0:
            self.timeline = temp_timeline[:]
        else:
            self.timeline = temp_timeline + self.timeline
            if len(self.timeline) > get_count:  # truncate long timelines
                self.timeline = self.timeline[:get_count]

        self.timeline.sort(key=itemgetter('id'), reverse=True)

        self.search_highlight_line = -1

        self.update_buffer()
Exemplo n.º 4
0
    def update(self):
        self.update_name()

        if self.paused:
            self.update_buffer()
            return

        get_count = config.config["notice_limit"]

        if self.prev_page != self.page:
            self.timeline = []

        last_id = 0
        if len(self.timeline) > 0:
            for notice in self.timeline:
                if notice["ic__from_web"]:  # don't consider inserted posts latest
                    last_id = notice["id"]
                    break

        if self.timeline_type == "home":
            raw_timeline = self.conn.statuses_home_timeline(count=get_count, page=self.page, since_id=last_id)
        elif self.timeline_type == "mentions":
            raw_timeline = self.conn.statuses_mentions(count=get_count, page=self.page, since_id=last_id)
        elif self.timeline_type == "direct":
            raw_timeline = self.conn.direct_messages(count=get_count, page=self.page, since_id=last_id)
        elif self.timeline_type == "user":
            raw_timeline = self.conn.statuses_user_timeline(
                user_id=self.type_params["user_id"],
                screen_name=self.type_params["screen_name"],
                count=get_count,
                page=self.page,
                since_id=last_id,
            )
        elif self.timeline_type == "group":
            raw_timeline = self.conn.statusnet_groups_timeline(
                group_id=self.type_params["group_id"],
                nickname=self.type_params["nickname"],
                count=get_count,
                page=self.page,
                since_id=last_id,
            )
        elif self.timeline_type == "tag":
            raw_timeline = self.conn.statusnet_tags_timeline(
                tag=self.type_params["tag"], count=get_count, page=self.page, since_id=last_id
            )
        elif self.timeline_type == "sentdirect":
            raw_timeline = self.conn.direct_messages_sent(count=get_count, page=self.page, since_id=last_id)
        elif self.timeline_type == "public":
            raw_timeline = self.conn.statuses_public_timeline()
        elif self.timeline_type == "favourites":
            raw_timeline = self.conn.favorites(page=self.page, since_id=last_id)
        elif self.timeline_type == "search":
            raw_timeline = self.conn.search(
                self.type_params["query"], page=self.page, standardise=True, since_id=last_id
            )
        elif self.timeline_type == "context":
            raw_timeline = []
            if last_id == 0:  # don't run this if we've already filled the timeline
                next_id = self.type_params["notice_id"]
                while next_id is not None:
                    notice = self.conn.statuses_show(id=next_id)
                    raw_timeline.append(notice)
                    if "retweeted_status" in notice:
                        next_id = notice["retweeted_status"]["id"]
                    else:
                        next_id = notice["in_reply_to_status_id"]

        self.prev_page = self.page

        temp_timeline = []
        old_ids = [n["id"] for n in self.timeline]

        for notice in raw_timeline:
            notice["ic__raw_datetime"] = helpers.notice_datetime(notice)
            notice["ic__from_web"] = True
            passes_filters = True
            if notice["id"] in old_ids:
                passes_filters = False
                continue
            for filter_item in config.config["filters"]:
                if filter_item.lower() in notice["text"].lower():
                    passes_filters = False
                    break
            if passes_filters:
                if (
                    (not self.timeline_type in ["direct", "sentdirect"])
                    and notice["source"] == "ostatus"
                    and config.config["expand_remote"]
                    and "attachments" in notice
                ):
                    import urllib2

                    for attachment in notice["attachments"]:
                        if attachment["mimetype"] != "text/html":
                            continue
                        req = urllib2.Request(attachment["url"])
                        try:
                            page = urllib2.urlopen(req).read()
                            try:
                                notice["text"] = helpers.html_unescape_string(helpers.title_regex.findall(page)[0])
                            except IndexError:  # no title could be found
                                pass
                        except:  # link was broken
                            pass
                        break
                temp_timeline.append(notice)

        if self.timeline_type in ["direct", "mentions"]:  # alert on changes to these. maybe config option later?
            if (len(self.timeline) > 0) and (
                len(temp_timeline) > 0
            ):  # only fire when there's new stuff _and_ we've already got something in the timeline
                if config.config["notify"] == "flash":
                    curses.flash()
                elif config.config["notify"] == "beep":
                    curses.beep()

        if len(self.timeline) == 0:
            self.timeline = temp_timeline[:]
        else:
            self.timeline = temp_timeline + self.timeline
            if len(self.timeline) > get_count:  # truncate long timelines
                self.timeline = self.timeline[:get_count]

        self.timeline.sort(key=itemgetter("id"), reverse=True)

        self.search_highlight_line = -1

        self.update_buffer()