Analysis of Facebook chat history.
778830
def stats(path, fmt, nocolor, timezones, utc, noprogress, most_common, resolve, length):
    """"""
    with colorize_output(nocolor):
        try:
            chat_history = _process_history(
                path=path, thread='', timezones=timezones,
                utc=utc, noprogress=noprogress, resolve=resolve)
        except ProcessingFailure:
            return
        statistics = ChatHistoryStatistics(
            chat_history, most_common=None if most_common < 0 else most_common)
        if fmt == 'text':
            statistics.write_text(sys.stdout, -1 if length < 0 else length)
        elif fmt == 'json':
            statistics.write_json(sys.stdout)
        elif fmt == 'pretty-json':
            statistics.write_json(sys.stdout, pretty=True)
        elif fmt == 'yaml':
            statistics.write_yaml(sys.stdout)
414075
def topic_over_time(self, k, mode='counts', slice_kwargs={}):
        

        return self.corpus.feature_distribution('topics', k, mode=mode,
                                                **slice_kwargs)
420761
def comments(self):
        ''''''
        if self.cache['comments']: return self.cache['comments']
        comments = []
        for message in self.messages[0:3]:
            comment_xml = self.bc.comments(message.id)
            for comment_node in ET.fromstring(comment_xml).findall("comment"):
                comments.append(Comment(comment_node))
        comments.sort()
        comments.reverse()
        self.cache['comments'] = comments
        return self.cache['comments']
1087353
def followed_topics(self):
        
        from .topic import Topic
        if self.url is None:
            return
        if self.followed_topic_num > 0:
            tag = self.soup.find('div', class_='zm-profile-side-topics')
            if tag is not None:
                for a in tag.find_all('a'):
                    yield Topic(Zhihu_URL + a['href'], a.img['alt'],
                                session=self._session)
            if self.followed_topic_num > 7:
                offset = 7
                gotten_data_num = 20
                while gotten_data_num == 20:
                    data = {'start': 0, 'offset': offset, '_xsrf': self.xsrf}
                    j = self._session.post(
                        Author_Get_More_Follow_Topic_URL.format(self.id),
                        data=data).json()
                    gotten_data_num = j['msg'][0]
                    offset += gotten_data_num
                    topic_item = BeautifulSoup(j['msg'][1]).find_all(
                        'div', class_='zm-profile-section-item')
                    for div in topic_item:
                        name = div.strong.text
                        url = Zhihu_URL + div.a['href']
                        yield Topic(url, name, session=self._session)
555977
def _parseMessage(self, content):
        """"""
        self._seq = content.get("seq", "0")

        if "lb_info" in content:
            self._sticky = content["lb_info"]["sticky"]
            self._pool = content["lb_info"]["pool"]

        if "batches" in content:
            for batch in content["batches"]:
                self._parseMessage(batch)

        if "ms" not in content:
            return

        for m in content["ms"]:
            mtype = m.get("type")
            try:
                # Things that directly change chat
                if mtype == "delta":
                    self._parseDelta(m)
                # Inbox
                elif mtype == "inbox":
                    self.onInbox(
                        unseen=m["unseen"],
                        unread=m["unread"],
                        recent_unread=m["recent_unread"],
                        msg=m,
                    )

                # Typing
                elif mtype == "typ" or mtype == "ttyp":
                    author_id = str(m.get("from"))
                    thread_id = m.get("thread_fbid")
                    if thread_id:
                        thread_type = ThreadType.GROUP
                        thread_id = str(thread_id)
                    else:
                        thread_type = ThreadType.USER
                        if author_id == self._uid:
                            thread_id = m.get("to")
                        else:
                            thread_id = author_id
                    typing_status = TypingStatus(m.get("st"))
                    self.onTyping(
                        author_id=author_id,
                        status=typing_status,
                        thread_id=thread_id,
                        thread_type=thread_type,
                        msg=m,
                    )

                # Delivered

                # Seen
                # elif mtype == "m_read_receipt":
                #
                #     self.onSeen(m.get('realtime_viewer_fbid'), m.get('reader'), m.get('time'))

                elif mtype in ["jewel_requests_add"]:
                    from_id = m["from"]
                    self.onFriendRequest(from_id=from_id, msg=m)

                # Happens on every login
                elif mtype == "qprimer":
                    self.onQprimer(ts=m.get("made"), msg=m)

                # Is sent before any other message
                elif mtype == "deltaflow":
                    pass

                # Chat timestamp
                elif mtype == "chatproxy-presence":
                    statuses = dict()
                    for id_, data in m.get("buddyList", {}).items():
                        statuses[id_] = ActiveStatus._from_chatproxy_presence(id_, data)
                        self._buddylist[id_] = statuses[id_]

                    self.onChatTimestamp(buddylist=statuses, msg=m)

                # Buddylist overlay
                elif mtype == "buddylist_overlay":
                    statuses = dict()
                    for id_, data in m.get("overlay", {}).items():
                        old_in_game = None
                        if id_ in self._buddylist:
                            old_in_game = self._buddylist[id_].in_game

                        statuses[id_] = ActiveStatus._from_buddylist_overlay(
                            data, old_in_game
                        )
                        self._buddylist[id_] = statuses[id_]

                    self.onBuddylistOverlay(statuses=statuses, msg=m)

                # Unknown message type
                else:
                    self.onUnknownMesssageType(msg=m)

            except Exception as e:
                self.onMessageError(exception=e, msg=m)
1185590
def getChatMembersCount(self, chat_id):
        
        p = _strip(locals())
        return self._api_request('getChatMembersCount', _rectify(p))
1006498
def discussions_for(user, only_open=True):
    '''
    
    '''
    # Only fetch required fields for discussion filtering (id and slug)
    # Greatly improve performances and memory usage
    datasets = Dataset.objects.owned_by(user.id, *user.organizations).only('id', 'slug')
    reuses = Reuse.objects.owned_by(user.id, *user.organizations).only('id', 'slug')

    qs = Discussion.objects(subject__in=list(datasets) + list(reuses))
    if only_open:
        qs = qs(closed__exists=False)
    return qs
1006024
def person_involved(self, person):
        
        return any(message.posted_by == person for message in self.discussion)
288355
public function get_messages($id_chat, $last = null, $day = null): array
  {
    $res = ['success' => false, 'last' => null, 'messages' => []];
    if ( $this->check() ){
      $dir = BBN_DATA_PATH.'users/'.$this->user->get_id().'/chat/'.$id_chat.'/'.($day ?: date('Y-m-d'));
      if ( $this->is_participant($id_chat) && is_dir($dir) ){
        $res['success'] = true;
        $files = bbn\file\dir::get_files($dir);
        foreach ( $files as $file ){
          $time = (float)basename($file, '.msg');
          if ( (!$last || \bbn\x::compare_floats($time, $last, '>')) && ($st = file_get_contents($file)) ){
            $enc = bbn\util\enc::crypt('test');
            $res['messages'][] = json_decode(bbn\util\enc::decrypt($st), true);
          }
        }
        if ( isset($time) ){
          $res['last'] = $time;
        }
      }
    }
    return $res;
  }
667767
def discussions_notifications(user):
    ''''''
    notifications = []

    # Only fetch required fields for notification serialization
    # Greatly improve performances and memory usage
    qs = discussions_for(user).only('id', 'created', 'title', 'subject')

    # Do not dereference subject (so it's a DBRef)
    # Also improve performances and memory usage
    for discussion in qs.no_dereference():
        notifications.append((discussion.created, {
            'id': discussion.id,
            'title': discussion.title,
            'subject': {
                'id': discussion.subject['_ref'].id,
                'type': discussion.subject['_cls'].lower(),
            }
        }))

    return notifications
1071106
def get_recent_repeated_responses(chatbot, conversation, sample=10, threshold=3, quantity=3):
    
    from collections import Counter

    # Get the most recent statements from the conversation
    conversation_statements = list(chatbot.storage.filter(
        conversation=conversation,
        order_by=['id']
    ))[sample * -1:]

    text_of_recent_responses = [
        statement.text for statement in conversation_statements
    ]

    counter = Counter(text_of_recent_responses)

    # Find the n most common responses from the conversation
    most_common = counter.most_common(quantity)

    return [
        counted[0] for counted in most_common
        if counted[1] >= threshold
    ]
1309500
def conversations(self):
        
        body = {
            "conversation_type": self.conversation_type,
            "audience_definition": self.audience_definition,
            "targeting_inputs": self.targeting_inputs
        }
        return self.__get(account=self.account, client=self.account.client, params=json.dumps(body))
458685
def getFriends(self,user_id="astronauta.mecanico",write=True):
        """"""
        while user_id not in self.browser.url:
            self.browser.visit("http://www.facebook.com/{}/friends".format(user_id), wait_time=3)
        #self.go("http://www.facebook.com/{}/friends".format(user_id))
        T=time.time()
        while 1:
            h1=self.browser.evaluate_script("document.body.scrollHeight")
            self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            h2=self.browser.evaluate_script("document.body.scrollHeight")
            if h1 != h2:
                T=time.time()
            elif time.time()-T>10:
                break
        #links=self.browser.find_link_by_partial_href("hc_location=friends_tab")
        links=self.browser.find_by_css(".fcb")
        friends=[]
        for link in links:
            name=link.value
            user_id_=link.find_by_tag("a")["href"].split("/")[-1].split("?")[0]
            friends.append((user_id_,name))
        tdict={}
        tdict["name"]=self.browser.find_by_id("fb-timeline-cover-name").value
        tdict["user_id"]=user_id
        tdict["friends"]=friends
        infos=self.browser.find_by_css("._3c_")
        mutual=0
        for info in infos:
            if info.value=="Mutual Friends":
                if info.find_by_css("._3d0").value:
                    tdict["n_mutual"]=info.find_by_css("._3d0").value
                    mutual=1
            if info.value=="All Friends":
                    tdict["n_friends"]=info.find_by_css("._3d0").value
        if mutual==0:
            links=self.browser.find_by_css("._gs6")
            if "Mutual" in links.value:
                tdict["n_mutual"]=links.value.split(" ")[0]
        if write:
            if not os.path.isdir("{}/fb_ids/".format(self._BASE_DIR)):
                os.mkdir("{}/fb_ids/".format(self._BASE_DIR))
            with open("{}fb_ids/{}.pickle".format(self._BASE_DIR,user_id),"wb") as f:
                pickle.dump(tdict,f)
        self.tdict=tdict
        return tdict
1355917
def _get_chat(self) -> Dict:
        

        if 'callback_query' in self._update:
            query = self._update['callback_query']
            if 'message' in query:
                return query['message']['chat']
            else:
                return {'id': query['chat_instance']}
        elif 'inline_query' in self._update:
            return patch_dict(
                self._update['inline_query']['from'],
                is_inline_query=True,
            )
        elif 'message' in self._update:
            return self._update['message']['chat']
273521
def history(self):
        """"""
        params = {"email": self.email_address}
        response = self._get("/subscribers/%s/history.json" %
                             self.list_id, params=params)
        return json_to_py(response)
450549
def list(self, page=1, per_page=10):
        
        return pagers.ChatList(self, self._raw_list, per_page=per_page,
                               page=page)
619035
def get_chatlist(chatfile):
    """"""
    if not chatfile:
        return set()
    try:
        with open(chatfile) as file_contents:
            return set(int(chat) for chat in file_contents)
    except (OSError, IOError) as exc:
        LOGGER.error('could not load saved chats:\n%s', exc)
        return set()