From a9fc0eb41b0a8eb9ffc394c55dc3afe5974b2683 Mon Sep 17 00:00:00 2001 From: chenjunxue <1523825571@qq.com> Date: Fri, 28 Aug 2020 11:33:59 +0800 Subject: [PATCH] 2.2.5 测试添加接口,使用住宅ip --- conf/config.yaml | 4 ++-- lib/facebook.py | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- utils/cache.py | 10 ++++++++++ 3 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 utils/cache.py diff --git a/conf/config.yaml b/conf/config.yaml index fca9aa4..ad9f1bf 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -4,5 +4,5 @@ SERVER: reconnect_interval: 3 VERSION: - code: 114 - name: 2.2.4 \ No newline at end of file + code: 115 + name: 2.2.5 \ No newline at end of file diff --git a/lib/facebook.py b/lib/facebook.py index 1d96769..fe7365b 100644 --- a/lib/facebook.py +++ b/lib/facebook.py @@ -22,6 +22,7 @@ from uuid import uuid1 import demjson import furl import retrying +from bs4 import BeautifulSoup from fbchat import (Client, FBchatException, FBchatUserError, Message, Sticker, ThreadType, _exception, _graphql, _util, log) @@ -32,6 +33,7 @@ from lib.graph import GraphAPIError from lib.state import PCState from lib.ttl_cache import lru_cache from utils import _attachment, parse_html +from utils.cache import cache class PostParam(Enum): @@ -150,14 +152,35 @@ class FacebookClient(Client): "to_friend": fbid, "action": "add_friend", "how_found": how_found, + "logging_location": 'friends_center', + "floc": 'pymk' } if how_found == 'profile_friends': data['ref_param'] = 'friends_tab' try: + session = self._state.session_factory(self.user_obj.user_agent) + # res = session.get(url, cookies=self._state.get_cookies()) if how_found == 'profile_button': res = self._payload_post("/ajax/add_friend/action.php", data) else: - res = self._payload_post("/ajax/add_friend/action?" + self._to_url_params(data), {}) + proxies = { + "http": "http://8.210.170.163:24000", + "https": "https://8.210.170.163:24000", + } + + url = "https://www.facebook.com/ajax/add_friend/action?" + self._to_url_params(data) + res = session.post(url, cookies=self._state.get_cookies(), data=self._state.get_params(), + proxies=proxies) + content = _util.check_request(res) + j = _util.to_json(content) + # We can't yet, since errors raised in here need to be caught below + _util.handle_payload_error(j) + res = j["payload"] + # 下面是原来的加粉写法 + # if how_found == 'profile_button': + # res = self._payload_post("/ajax/add_friend/action.php", data) + # else: + # res = self._payload_post("/ajax/add_friend/action?" + self._to_url_params(data), {}) except _exception.FBchatNotLoggedIn: raise except _exception.FBchatFacebookError as err: @@ -1965,9 +1988,86 @@ class FacebookClient(Client): """ 你的照片 """ - #如果cursor为None,则请求 https://www.facebook.com/ana.sofia.583671/photos_all 获取到 - return - ###############graph接口 start ############## + # 如果cursor为None,则请求 https://www.facebook.com/ana.sofia.583671/photos_all 获取到 + if cursor is None or cursor == '': + url = 'https://www.facebook.com/ana.sofia.583671/photos_all' + try: + session = self._state.session_factory(self.user_obj.user_agent) + res = session.get(url, cookies=self._state.get_cookies()) + html_text = res.text + html_text2 = html_text.replace('', '') + soup = BeautifulSoup(html_text, 'lxml') + script = soup.find_all('script') + de = script[112].prettify() + cursor = re.findall(r'{__m:"__elem_559218ec_0_0"},"(.*?)"\]\],\["StarGrid"', de)[0] + num_list = re.findall(r'pagelet_timeline_app_collection_(.*?):(.*?):(.*?)"', de)[0] + de = script[92].prettify() + pagelet_token = re.findall(r'pagelet_token:"(.*?)"', de)[0] + print(cursor, num_list) + cache.set(f'{self.email}:AllPhotosAppCollectionPagelet', num_list) + cache.set(f'{self.email}:AllPhotosAppCollectionPagelet:pagelet_token', pagelet_token) + except Exception as err: + raise BaseException(f'获取必要参数错误{err}') + soup = BeautifulSoup(html_text2, 'lxml') + lis = soup.find_all("li", {"class": "fbPhotoStarGridElement"}) + data_list = list() + for li in lis: + pic_id = li.attrs.get('data-fbid', None) + url = li.attrs.get('data-starred-src', None) + label = li.a.attrs.get('aria-label', None) + data = { + 'pic_id': pic_id, + 'url': url, + 'label': label + } + data_list.append(data) + res = { + 'data_list': data_list, + 'cursor': cursor + } + return res + else: + num_list = cache.get(f'{self.email}:AllPhotosAppCollectionPagelet') + pagelet_token = cache.get(f'{self.email}:AllPhotosAppCollectionPagelet:pagelet_token') + assert num_list is not None, '游标失效,请重新获取首页游标。' + get_data = {"collection_token": f"{num_list[0]}:{num_list[1]}:{num_list[2]}", + "cursor": cursor, + "disablepager": False, "overview": False, "profile_id": num_list[0], + "pagelet_token": pagelet_token, + "tab_key": "photos", "lst": f"{num_list[0]}:{num_list[0]}:{str(int(time.time()))}", + "order": None, + "sk": "photos", "importer_state": None} + data = {'data': json.dumps(get_data), + 'fb_dtsg_ag': self._state.fb_dtsg_ag, + 'dpr': 2, + 'jazoest': self._state.jazoest + } + try: + url = furl.furl("https://www.facebook.com/ajax/pagelet/generic.php/AllPhotosAppCollectionPagelet").add( + data).url + f = self._state._get(url, {}) + except Exception as err: + print(err) + soup = BeautifulSoup(f, 'lxml') + lis = soup.find_all("li", {"class": "fbPhotoStarGridElement"}) + data_list = list() + for li in lis: + pic_id = li.attrs.get('data-fbid', None) + url = li.attrs.get('data-starred-src', None) + label = li.a.attrs.get('aria-label', None) + data = { + 'pic_id': pic_id, + 'url': url, + 'label': label + } + data_list.append(data) + res = { + 'data_list': data_list, + 'cursor': cursor + } + return res + + ###############graph接口 start ############## def checkVaild(self): try: diff --git a/utils/cache.py b/utils/cache.py new file mode 100644 index 0000000..03c56c9 --- /dev/null +++ b/utils/cache.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Time : 2020/8/27 14:51 +# @Author : AsiHacker +# @Site : +# @File : cache.py +# @Software: PyCharm +import cacheout + +cache = cacheout.LRUCache(maxsize=5000) \ No newline at end of file -- libgit2 0.24.0