def fetchBlogCategoryList(self, param=None): success, data = self.__fetchBlogCategoryList() if not success: return HttpResult.error(info="获取失败") return HttpResult.ok(info="获取成功", data=data)
def fetchBlogList(self, param=None): url = "https://bizapi.csdn.net/blog-console-api/v1/column/getAllArticles?column_id=" + str( param['id']) uuid = self.__createUUID() sign = self.__httpGetSign(uuid, url) headers = { "Host": "bizapi.csdn.net", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0", "Accept": "application/json, text/plain, */*", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate, br", "Referer": "https://mp.csdn.net/console/column/allColumnList", "X-Ca-Key": "203803574", "X-Ca-Nonce": uuid, "X-Ca-Signature": sign, "X-Ca-Signature-Headers": "x-ca-key,x-ca-nonce", "Origin": "https://mp.csdn.net", "Connection": "keep-alive", "TE": "Trailers" } response = HttpRequestUtil.get(url, headers=headers, cookies=self.__cookies) if response.status_code != 200: return HttpResult.error(info="获取失败") result = json.loads(response.text) return HttpResult.ok(info="获取成功", data=result['data'])
def publishUpdateBlog(self, param): # 先保存更新内容 if not self.__saveUpdate(param): return HttpResult.error(info="更新发布失败") # 发布更新 url = "https://www.jianshu.com/author/notes/" + str(param['id']) + "/publicize" headers = { "Host": "www.jianshu.com", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0", "Accept": "application/json", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate, br", "Content-Type": "application/json; charset=UTF-8", "Content-Length": str(2), "Origin": "https://www.jianshu.com", "Connection": "keep-alive", "Referer": "https://www.jianshu.com/writer", } payload = {} response = HttpRequestUtil.post(url, headers=headers, data=json.dumps(payload), cookies=self.__cookies) result = json.loads(response.text) if 'error' in result: return HttpResult.error(info=result['error'][0]['message']) return HttpResult.ok(info="更新发布成功", data=result)
def fetchBlogList(self, param=None): """ 获取某一个分类下的文章列表 """ url = 'https://api.juejin.cn/content_api/v1/article/query_list' userInfo = self.__getUserInfo() if 0 != userInfo['err_no']: return HttpResult.error(info="获取失败") userId = userInfo['data']['user_id'] payload = {"user_id": userId, "sort_type": 2, "cursor": "0"} headers = { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.5', 'Connection': 'keep-alive', 'Content-Length': str(len(payload)), 'Content-Type': 'application/json', 'Host': 'api.juejin.cn', 'Origin': 'https://juejin.cn', 'Referer': 'https://juejin.cn/user/' + userId + '/post', 'TE': 'Trailers', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0' } response = HttpRequestUtil.post(url, headers=headers, data=json.dumps(payload), cookies=self.__cookies) result = json.loads(response.text) if 0 != result['err_no']: return HttpResult.error(info="获取失败") return HttpResult.ok(info="获取成功", data=result['data'])
def fetchContentBlog(self, param=None): aid = str(param['id']) url = "https://bizapi.csdn.net/blog-console-api/v3/editor/getArticle?id=" + aid + "&model_type" uuid = self.__createUUID() sign = self.__httpGetSign(uuid, url) headers = { "Host": "bizapi.csdn.net", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0", "Accept": "application/json, text/plain, */*", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate, br", "Referer": "https://editor.csdn.net/md/?articleId=" + aid, "X-Ca-Key": "203803574", "X-Ca-Nonce": uuid, "X-Ca-Signature": sign, "X-Ca-Signature-Headers": "x-ca-key,x-ca-nonce", "Origin": "https://editor.csdn.net", "Connection": "keep-alive", "TE": "Trailers", "Cache-Control": "max-age=0" } response = HttpRequestUtil.get(url, headers=headers, cookies=self.__cookies) if response.status_code != 200: return HttpResult.error(info="获取失败") return HttpResult.ok(info="获取成功", data=response.text)
def deleteBlog(self, param): """ 删除 """ url = 'https://api.juejin.cn/content_api/v1/article/delete' article_id = param['id'] payload = {"article_id": article_id} headers = { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.5', 'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Content-Length': str(len(payload)), 'Host': 'api.juejin.cn', 'Origin': 'https://juejin.cn', 'Referer': 'https://juejin.cn/user/' + article_id + '/posts', 'TE': 'Trailers', 'Cache-Control': 'max-age=0', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0' } response = HttpRequestUtil.post(url, headers=headers, data=json.dumps(payload), cookies=self.__cookies) result = json.loads(response.text) if 0 != result['err_no']: return HttpResult.error(info="删除失败") return HttpResult.ok(info="删除成功")
def publishNewBlog(self, param): url = " https://bizapi.csdn.net/blog-console-api/v3/mdeditor/saveArticle" uuid = self.__createUUID() caSignStr = f"POST\n*/*\n\napplication/json\n\nx-ca-key:203803574\nx-ca-nonce:{uuid}\n/blog-console-api/v3/mdeditor/saveArticle".encode( ) sign = self.__httpPostSign(caSignStr) payload = { "title": str(param['title']), "markdowncontent": str(param['content']), "content": str(param['content']), "readType": "public", "tags": "", "status": 0, "categories": str(param['cate']), "type": "original", "original_link": "", "authorized_status": False, "not_auto_saved": "1", "source": "pc_mdeditor" } headers = { "Host": "bizapi.csdn.net", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0", "Accept": "*/*", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate, br", "Referer": 'https://editor.csdn.net/md/', "X-Ca-Key": "203803574", "X-Ca-Nonce": uuid, "X-Ca-Signature": sign, "X-Ca-Signature-Headers": "x-ca-key,x-ca-nonce", "Origin": "https://editor.csdn.net", "Connection": "keep-alive", "TE": "Trailers", "Content-Type": "application/json", "Content-Length": str(len(payload)) } response = HttpRequestUtil.post(url, headers=headers, data=json.dumps(payload), cookies=self.__cookies) if response.status_code != 200: return HttpResult.error(info="发布失败") return HttpResult.ok(info="发布成功", data=response.text)
def deleteBlog(self, param): url = 'https://mp.toutiao.com/mp/agw/article/delete/' blog_attr = param['blog']['article_attr'] logger.info(blog_attr) item_id = str(blog_attr['item_id']) mp_id = str(blog_attr['mp_id']) create_time = str(blog_attr['create_time']) article_type = str(blog_attr['type']) pgc_cell = json.loads(blog_attr['pgc_cell']) group_source = str(pgc_cell['group_source']) has_article_pgc = str(pgc_cell['has_article_pgc']) payload = 'item_id=' + item_id \ + '&pgc_id=' + item_id \ + '&id=' + item_id \ + '&article_type=' + article_type \ + '&group_id=' + item_id \ + '&source_type=0&has_article_pgc=' + has_article_pgc \ + '&book_id=&create_time=' + create_time \ + '&group_source=' + group_source \ + '&mp_id=' + mp_id headers = { "Host": "mp.toutiao.com", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0", "Accept": "application/json, text/plain, */*", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", "Content-Length": str(len(payload)), "Origin": "https://mp.toutiao.com", "Referer": "https://mp.toutiao.com/profile_v4/graphic/articles", "Connection": "keep-alive", "TE": "Trailers" } response = HttpRequestUtil.post(url, headers=headers, data=json.dumps(payload), cookies=self.__cookies) if response.status_code != 200: return HttpResult.error(info="删除失败") return HttpResult.ok(info="删除成功")
def publishNewBlog(self, param): """ 发布 """ success, draft = self.__createArticleDraft(param['title'], param['content']) if not success: return HttpResult.error(info="发布失败") return self.__publishBlog(draft['id'])
def fetchContentBlog(self, param=None): url = "https://www.jianshu.com/author/notes/" + str(param["id"]) + "/content" headers = { "Host": "www.jianshu.com", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0", "Accept": "application/json", "Accept-Language": "en-US,en;q=0.5", "Cache-Control": "max-age=0", "Accept-Encoding": "gzip, deflate, br", "Content-Type": "application/json; charset=UTF-8", "Connection": "keep-alive", "Referer": "https://www.jianshu.com/writer", } response = HttpRequestUtil.get(url, headers=headers, cookies=self.__cookies) if response.status_code != 200: return HttpResult.error(info="获取失败") return HttpResult.ok(info="获取成功", data=response.text)
def publishUpdateBlog(self, param): """ 更新 """ success, data = self.__updateArticleDraft(param) if False == success: return HttpResult.error(info=data) article_id = data['id'] return self.__publishBlog(article_id)
def fetchContentBlog(self, param=None): aid = str(param['id']) url = "https://mp.toutiao.com/mp/agw/article/edit?pgc_id=" + aid + "&format=json" headers = { "Host": "mp.toutiao.com", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0", "Accept": "application/json, text/plain, */*", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Connection": "keep-alive", "Referer": "https://mp.toutiao.com/profile_v4/graphic/publish?pgc_id=" + aid } response = HttpRequestUtil.get(url, headers=headers, cookies=self.__cookies) if response.status_code != 200: return HttpResult.error(info="获取失败") return HttpResult.ok(info="获取成功", data=response.text)
def publishNewBlog(self, param): success, cateList = self.__fetchBlogCategoryList() if not success: return HttpResult.error(info="发布失败") cateListData = json.loads(cateList) firstCateId = cateListData[0]['id'] success, result = self.__createDefaultNewBlog(firstCateId) if not success: return HttpResult.error(info='发布失败') newBlog = json.loads(result) param = {"id": str(newBlog['id']), "autosave_control": 1, "title": param['title'], "content": param['content']} success, self.publishUpdateBlog(param) if not success: return HttpResult.error(info='发布失败') return HttpResult.ok(info='发布成功')
def deleteBlog(self, param): url = 'https://bizapi.csdn.net/blog-console-api/v1/article/del' uuid = self.__createUUID() uri = urlparse(url) caSignStr = f"POST\napplication/json, text/plain, */*\n\napplication/json\n\nx-ca-key:203803574\nx-ca-nonce:{uuid}\n{uri.path}".encode( ) sign = self.__httpPostSign(caSignStr) payload = {"article_id": str(param['id']), "deep": "false"} headers = { "Host": "bizapi.csdn.net", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0", "Accept": "application/json, text/plain, */*", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate, br", "Referer": 'https://mp.csdn.net/console/article', "X-Ca-Key": "203803574", "X-Ca-Nonce": uuid, "X-Ca-Signature": sign, "X-Ca-Signature-Headers": "x-ca-key,x-ca-nonce", "Origin": "https://editor.csdn.net", "Connection": "keep-alive", "TE": "Trailers", "Cache-Control": "max-age=0", "Content-Type": "application/json", "Content-Length": str(len(payload)) } response = HttpRequestUtil.post(url, headers=headers, data=json.dumps(payload), cookies=self.__cookies) if response.status_code != 200: return HttpResult.error(info="删除失败") return HttpResult.ok(info="删除成功", data=response.text)
def fetchBlogList(self, param=None): # url = 'https://mp.toutiao.com/mp/agw/creator_center/list?type=2&status=0&start_cursor=0&end_cursor=0&size=10&mode=2&need_stat=true&page_time={"index":1,"time_stamp":0}&filter_params={"search_word":"","source":0}&app_id=1231' url = 'https://mp.toutiao.com/mp/agw/creator_center/list?' headers = { "Host": "mp.toutiao.com", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0", "Accept": "application/json, text/plain, */*", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Connection": "keep-alive", "Referer": "https://mp.toutiao.com/profile_v4/graphic/articles" } ''' response.content #字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩 类型:bytes reponse.text #字符串方式的响应体,会自动根据响应头部的字符编码进行解码。类型:str ''' response = HttpRequestUtil.get(url, headers=headers, cookies=self.__cookies) if response.status_code != 200: return HttpResult.error(info="获取失败") return HttpResult.ok(info="获取成功", data=response.text)
def deleteBlog(self, param): url = "https://www.jianshu.com/author/notes/" + str(param['id']) + "/soft_destroy" headers = { "Host": "www.jianshu.com", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0", "Accept": "application/json", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate, br", "Content-Type": "application/json; charset=UTF-8", "Content-Length": str(2), "Origin": "https://www.jianshu.com", "Connection": "keep-alive", "Referer": "https://www.jianshu.com/writer", } payload = {} response = HttpRequestUtil.post(url, headers=headers, data=json.dumps(payload), cookies=self.__cookies) if response.status_code != 200: return HttpResult.error(info="删除失败") result = json.loads(response.text) return HttpResult.ok(info="删除成功", data=result)
def publishNewBlog(self, param): url = 'https://mp.toutiao.com/profile_v4/graphic/publish' webDriver = WebDriver.getDriver() webDriver.maximize_window() # 必须先打开才能添加cookie webDriver.get(url) self.__setDriverCookie(webDriver) webDriver.get(url) try: title_xpath = '/html/body/div[1]/div/div[3]/section/main/div[2]/div/div/div[1]/div[3]/div/div/div[2]/div/div/div/textarea' WebDriverWait(webDriver, 20, 0.5).until( expected_conditions.presence_of_element_located((By.XPATH, title_xpath))) title = webDriver.find_element_by_xpath(title_xpath) title.clear() title.send_keys(param['title']) # 设置内容 content = StringUtil.removeBlankAndBreakLine(param['content']) logger.warning(content) js = "document.getElementsByClassName('ProseMirror')[0].innerHTML = '" + content + "'" logger.warning(js) webDriver.execute_script(js) # 设置无图 coverBtn_xpath = '/html/body/div[1]/div/div[3]/section/main/div[2]/div/div/div[2]/div[2]/div[1]/div/div[2]/div/div[1]/label[3]/span/div' WebDriverWait(webDriver, 20, 0.5).until( expected_conditions.presence_of_element_located((By.XPATH, coverBtn_xpath))) coverBtn = webDriver.find_element_by_xpath(coverBtn_xpath) coverBtn.click() # 点击发布按钮 publishBtn_xpath = '/html/body/div[1]/div/div[3]/section/main/div[2]/div/div/div[3]/div/button[4]' WebDriverWait(webDriver, 20, 0.5).until( expected_conditions.presence_of_element_located((By.XPATH, publishBtn_xpath))) publishBtn = webDriver.find_element_by_xpath(publishBtn_xpath) publishBtn.click() except Exception as e: logger.warning(e) finally: time.sleep(5) webDriver.close() return HttpResult.ok(info="发布成功")