コード例 #1
0
    def img_upload(self, markdowncontent, content):
        # 图片需要上传到 csdn,否则发布的文章没有图片
        upload_url = 'https://bizapi.csdn.net/blog-console-api/v3/image/transfer'

        content_html = etree.HTML(content)
        src_list = content_html.xpath('//img/@src')
        for src in src_list:
            while 1:
                time_stamp = int(time.time() * 1000)
                salt = self.gen_random()
                form_data = {"uuid": f"img-{salt}-{time_stamp}", "url": src}
                base_url = "/blog-console-api/v3/image/transfer"
                self.sess.headers = headers_to_dict(
                    self.gen_header(base_url, method='POST'))
                res = self.sess.post(url=upload_url, json=form_data)
                if res.text:
                    r_json = res.json()
                    # 图片在文章中本来就不存在
                    if r_json.get('code') == 400:
                        return markdowncontent, content
                    img_url = r_json.get('data').get('img_url')
                    content = content.replace(src, img_url)
                    markdowncontent = markdowncontent.replace(src, img_url)
                    time.sleep(random.random() * 0.1)
                    break
            # else:
            #     print("请求响应为空:")
            #     print(res.text, res.cookies.get_dict(), res.status_code)
        return markdowncontent, content
コード例 #2
0
 def img_upload(self, content, referer):
     img_header = f"""
         accept: */*
         accept-encoding: gzip, deflate, br
         accept-language: zh-CN,zh;q=0.9
         x-requested-with:fetch
         cookie: {self.cookie}
         origin: https://zhuanlan.zhihu.com
         referer: {referer}
         user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
     """
     # 图片需要上传到 csdn,否则发布的文章没有图片
     upload_url = 'https://zhuanlan.zhihu.com/api/uploaded_images'
     self.sess.headers = headers_to_dict(img_header)
     content_html = etree.HTML(content)
     src_list = content_html.xpath('//img/@src')
     for src in src_list:
         while 1:
             files = {'url': (None, src), 'source': (None, 'article')}
             res = self.sess.post(url=upload_url, files=files)
             # print('上传图片结果:', res.text)
             if res.text:
                 r_json = res.json()
                 # 图片在文章中本来就不存在
                 if r_json.get('code') == 400:
                     return content
                 img_url = r_json.get('watermark_src')
                 content = content.replace(src, img_url)
                 time.sleep(random.random() * 0.1)
                 break
     return content
コード例 #3
0
 def __init__(self, cookie):
     """
     @param cookie:
     """
     self.sess = requests.session()
     base_header = f"""
        
     """
     self.sess.headers = headers_to_dict(base_header)
コード例 #4
0
 def get_category(self):
     get_category_url = 'https://bizapi.csdn.net/blog-console-api/v3/editor/getBaseInfo'
     base_url = '/blog-console-api/v3/editor/getBaseInfo'
     self.sess.headers = headers_to_dict(
         self.gen_header(base_url, method='GET'))
     res = self.sess.get(get_category_url)
     if res.text and res.status_code == 200:
         categoty_list = res.json().get('data').get('categorys')
         return categoty_list
     return None
コード例 #5
0
 def __init__(self, cookie):
     self.sess = requests.session()
     base_header = f"""
         accept-encoding: gzip, deflate, br
         accept-language: zh-CN,zh;q=0.9
         cookie: {cookie}
         referer: https://i.cnblogs.com/posts/edit
         user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
     """
     self.sess.headers = headers_to_dict(base_header)
     edit_url = 'https://i.cnblogs.com/posts/edit'
     res_edit = self.sess.get(edit_url)
     self.xsrf_token = res_edit.cookies.get_dict().get('XSRF-TOKEN')
コード例 #6
0
 def __init__(self, cookie):
     self.sess = requests.session()
     base_header = f"""
         accept: */*
         accept-encoding: gzip, deflate, br
         accept-language: zh-CN,zh;q=0.9
         content-type: application/json
         cookie: {cookie}
         origin: https://juejin.cn
         referer: https://juejin.cn/
         user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
     """
     self.sess.headers = headers_to_dict(base_header)
コード例 #7
0
 def publish_content(self, tags, title, markdowncontent, content,
                     plant_config):
     self.markdowncontent, self.content = self.img_upload(
         markdowncontent=markdowncontent, content=content)
     base_url = "/blog-console-api/v3/mdeditor/saveArticle"
     self.sess.headers = headers_to_dict(
         self.gen_header(base_url, method='POST'))
     url = 'https://bizapi.csdn.net/blog-console-api/v3/mdeditor/saveArticle'
     if not tags and not plant_config:
         return False
     if not tags:
         tags = plant_config[0].tags
     type_name = 'original'
     original_link = ''
     readType = 'public'
     if plant_config:
         type = plant_config[0].art_type
         art_pub_type = plant_config[0].art_publish_type
         if art_pub_type == 1:
             readType = 'public'
         elif art_pub_type == 2:
             readType = 'private'
         elif art_pub_type == 3:
             readType = 'read_need_vip'
         if type == 1:
             type_name = 'original'
         elif type == 2:
             type_name = 'repost'
             original_link = plant_config[0].art_source_url
         elif type == 3:
             type_name = 'translated'
             original_link = plant_config[0].art_source_url
     form_data = {
         "title": title,
         "markdowncontent": self.markdowncontent,
         "content": self.content,
         "readType": readType,  # 私密private vip可见 read_need_vip
         "tags": tags,
         "status": 0,
         "categories":
         plant_config[0].category_value if plant_config else "",
         "type": type_name,
         "original_link": original_link,
         "authorized_status": True if original_link else False,
         "not_auto_saved": "1",
         "source": "pc_mdeditor"
     }
     res = self.sess.post(url=url, json=form_data)
     if res.text:
         return res.text
     return None
コード例 #8
0
 def del_doc(self, art_url: str):
     art_id = art_url.rsplit('/', 1)[1]
     del_url = 'https://bizapi.csdn.net/blog-console-api/v1/article/del'
     base_url = "/blog-console-api/v1/article/del"
     self.sess.headers = headers_to_dict(
         self.gen_header(base_url, method='POST'))
     form_data = {"article_id": art_id, "deep": "false"}
     res = self.sess.post(del_url, json=form_data)
     if res.text:
         r_json = res.json()
         code = r_json.get('code')
         msg = r_json.get('msg')
         if code == 200 and msg == "success":
             return True
     return False
コード例 #9
0
 def __init__(self, cookie):
     """
     @param cookie:
     """
     self.sess = requests.session()
     base_header = f"""
         Accept: application/json
         Accept-Encoding: gzip, deflate, br
         Accept-Language: zh-CN,zh;q=0.9
         Cookie: {cookie}
         Host: www.jianshu.com
         Origin: https://www.jianshu.com
         Referer: https://www.jianshu.com/writer
         User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36       
     """
     self.sess.headers = headers_to_dict(base_header)
コード例 #10
0
 def __init__(self, seg_token):
     seg_token = cookie_to_dict(seg_token).get('PHPSESSID')
     self.sess = requests.session()
     base_header = f"""
         accept: */*
         accept-encoding: gzip, deflate, br
         accept-language: zh-CN,zh;q=0.9
         content-type: application/json
         cookie: 123
         token: {seg_token}
         origin: https://segmentfault.com
         referer: https://segmentfault.com/
         user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
         
     """
     self.sess.headers = headers_to_dict(base_header)
コード例 #11
0
    def upload_img(self, content):
        upload_head = """
            accept: application/json
            accept-language: zh-CN,zh;q=0.9
            origin: https://www.jianshu.com
            referer: https://www.jianshu.com/
            user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36
        """
        upload_url = 'https://upload.qiniup.com/'
        md = markdown.Markdown(extensions=[
            'markdown.extensions.extra', 'markdown.extensions.codehilite'
        ])
        content_html = md.convert(content)
        res_html = etree.HTML(content_html)
        src_list = res_html.xpath('//img/@src')
        for src in src_list:
            res_img = requests.get(src)
            file_name = src.rsplit('/', 1)[1].rsplit('.')[0] + '.png'
            token_url = f'https://www.jianshu.com/upload_images/token.json?filename={file_name}'
            res_token = self.sess.get(token_url)
            if res_token.text:
                res_token_json = res_token.json()
                token = res_token_json.get('token')
                key = res_token_json.get('key')
                if token and key:
                    m = MultipartEncoder(
                        fields={
                            "file": (file_name, res_img.content),
                            'token': token,
                            'key': key,
                            'x:protocol': 'https'
                        })
                    headers_dict = headers_to_dict(upload_head)
                    headers_dict['Content-Type'] = m.content_type

                    res_upload_file = requests.post(url=upload_url,
                                                    data=m,
                                                    headers=headers_dict)
                    if res_upload_file.text:
                        r_json = res_upload_file.json()
                        if r_json.get('error'):
                            continue
                        # 图片在文章中本来就不存在
                        img_url = r_json.get('url')
                        content = content.replace(src, img_url)
                        time.sleep(random.random())
        return content