<commit_before># -*- coding: utf-8 -*-
import Baidu
import requests
import re
import json
import urllib.request

class Tieba(Baidu.Baidu):
    def _get_tbs(self, url):
        tbs_pattern = re.compile("'tbs': \"(.+?)\"")
        match = tbs_pattern.search(self.session.get(url).text)
        if match:
            return match.group(1)
        else:
            return ""

    def _set_stoken(self):
        likes_url = "https://passport.baidu.com/v3/login/api/auth/?tpl=tb&jump=&return_type=3&u=http%3A%2F%2Ftie" \
                    "ba.baidu.com%2Ff%2Flike%2Fmylike%3Fpn%3D1"
        res = self.session.get(likes_url, allow_redirects=False)
        likes_url = res.headers['Location']
        res = self.session.get(likes_url, allow_redirects=False)
        stoken = re.search("STOKEN=(.+?);", res.headers['Set-Cookie']).group(1)
        self.session.cookies["STOKEN"] = stoken

    def _check_sign(self, tieba_name):
        res = self.session.get("http://tieba.baidu.com/mo/m?kw=" + tieba_name)
        return "已签到" in res.text

    def get_likes(self):
        #self._set_stoken()
        res = self.session.get('http://tieba.baidu.com/f/like/mylike?pn=1', allow_redirects=False)
        last = re.search("pn=(\d+)\">尾页</a>", res.text).group(1)
        likes_url = 'http://tieba.baidu.com/f/like/mylike?pn='
        likes_tieba = []
        for i in range(1, int(last) + 1):
            res = self.session.get(likes_url + str(i))
            likes_tieba += re.compile('<a href.+?title="(.+?)"').findall(res.text)
        return likes_tieba

    def sign_PC(self, tieba_name):
        tieba_url = "http://tieba.baidu.com/f?kw={0}&fr=index".format(tieba_name)
        if self._check_sign(tieba_url):
            print("Already signed in", tieba_name)
            return False
        tbs = self._get_tbs(tieba_url)
        if not tbs:
            print("Failed to grasp ", tieba_name, " !")
            return False
        sign_url = "http://tieba.baidu.com/sign/add"
        response = self.session.post(
            sign_url,
            data={
                'ie': 'utf-8',
                'kw': tieba_name,
                'tbs': tbs,
            },
        )
        data = json.loads(response.text)
        if data['no'] == 0:
            print("Signed successfully in", tieba_name)
            return True
        else:
            print("Failed to sign, reason:", data['error'], tieba_name)
            return False

    def sign_Wap(self, tieba_name):
        if self._check_sign(tieba_name):
            print("Already signed in", tieba_name)
            return False
        tieba_url = "http://tieba.baidu.com/mo/m?kw=" + tieba_name
        res = self.session.get(tieba_url)
        match = re.search('(/mo/q[^<]+?)">签到', res.text)
        if match:
            sign_url = "http://tieba.baidu.com" + match.group(1)
            sign_url = sign_url.replace("&amp;", "&")
            res = self.session.get(sign_url)

        if "已签到" in res.text:
            print("Signed successfully in", tieba_name)
            return True
        else:
            print("Fail to sign in", tieba_name)
            return False

    def reply(self, tid, content):
        if 'p' in str(tid):
            # tid is an URL
            url = tid
            tid = re.search("\d+",url).group(0)
        else:
            # tid is a string of numbers
            url = "http://tieba.baidu.com/p/" + str(tid)
        res = self.session.get(url)
        fid = re.search("fid:'(\d+?)'", res.text).group(1)
        kw = re.search("kw:'(.+?)'", res.text).group(1)
        res = self.session.post(
            "http://tieba.baidu.com/f/commit/post/add",
            data={
                "ie": "utf-8",
                "kw": kw,
                "fid": fid,
                "tid": tid,
                "vcode_md5": "",
                "rich_text": 1,
                "tbs": re.search('"tbs"  : "(.+?)"', res.text).group(1),
                "content": content,
                "__type__": "reply"
            })
        print(res.text)

    def commit(self, tieba_name, title, content):
        url = "http://tieba.baidu.com/f?kw=" + tieba_name
        fid = re.search("fid: (\d+)", self.session.get(url).text).group(1)
        res = self.session.post(
            "http://tieba.baidu.com/f/commit/thread/add",
            data = {
                "ie": "utf-8",
                "kw": tieba_name,
                "fid": fid,
                "tid": 0,
                "vcode_md5": "",
                "rich_text": 1,
                "floor_num": 0,
                "tbs": self._get_tbs(url),
                "content": content,
                "title": title,
                "__type__": "thread"
            })
        print(res.text)
<commit_msg>Use Wap to get liked tieba.<commit_after># -*- coding: utf-8 -*-
import Baidu
import re


def Retry(func):
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs) or func(*args, **kwargs) or func(*args, **kwargs)
    return wrapper


class Tieba(Baidu.Baidu):
    def _get_tbs(self, url):
        tbs_pattern = re.compile("'tbs': \"(.+?)\"")
        match = tbs_pattern.search(self.session.get(url).text)
        if match:
            return match.group(1)
        else:
            return ""

    def _check_sign(self, tieba_name):
        res = self.session.get("http://tieba.baidu.com/mo/m?kw=" + tieba_name)
        return "已签到" in res.text

    @Retry
    def get_likes(self):
        try:
            res = self.session.get("http://tieba.baidu.com/mo/")
            likes_url = "http://tieba.baidu.com" + \
                        re.search('"([^"]+tab=favorite)"', res.text).group(1).replace("&amp;", "&")
            res = self.session.get(likes_url)
            likes = re.findall('kw.+?">(.+?)<', res.text)
            return likes
        except:
            return False

    def sign_Wap(self, tieba_name):
        if self._check_sign(tieba_name):
            print("Already signed in", tieba_name)
            return False
        tieba_url = "http://tieba.baidu.com/mo/m?kw=" + tieba_name
        res = self.session.get(tieba_url)
        match = re.search('(/mo/q[^<]+?)">签到', res.text)
        if match:
            sign_url = "http://tieba.baidu.com" + match.group(1)
            sign_url = sign_url.replace("&amp;", "&")
            res = self.session.get(sign_url)

        if "已签到" in res.text:
            print("Signed successfully in", tieba_name)
            return True
        else:
            print("Fail to sign in", tieba_name)
            return False

    def reply(self, tid, content):
        if 'p' in str(tid):
            # tid is an URL
            url = tid
            tid = re.search("\d+", url).group(0)
        else:
            # tid is a string of numbers
            url = "http://tieba.baidu.com/p/" + str(tid)
        res = self.session.get(url)
        fid = re.search("fid:'(\d+?)'", res.text).group(1)
        kw = re.search("kw:'(.+?)'", res.text).group(1)
        res = self.session.post(
            "http://tieba.baidu.com/f/commit/post/add",
            data={
                "ie": "utf-8",
                "kw": kw,
                "fid": fid,
                "tid": tid,
                "vcode_md5": "",
                "rich_text": 1,
                "tbs": re.search('"tbs"  : "(.+?)"', res.text).group(1),
                "content": content,
                "__type__": "reply"
            })
        print(res.text)

    def commit(self, tieba_name, title, content):
        url = "http://tieba.baidu.com/f?kw=" + tieba_name
        fid = re.search("fid: (\d+)", self.session.get(url).text).group(1)
        res = self.session.post(
            "http://tieba.baidu.com/f/commit/thread/add",
            data={
                "ie": "utf-8",
                "kw": tieba_name,
                "fid": fid,
                "tid": 0,
                "vcode_md5": "",
                "rich_text": 1,
                "floor_num": 0,
                "tbs": self._get_tbs(url),
                "content": content,
                "title": title,
                "__type__": "thread"
            })
        print(res.text)
{'fraction_non_alphanumeric': 0.16287366219707222, 'fraction_numerical': 0.006396850781153893, 'mean_word_length': 1.6387536514118792, 'pattern_counts': {'":': 40, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5742615', 'n_tokens_mistral': 2825, 'n_tokens_neox': 2734, 'n_words': 494}