-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrap.py
58 lines (48 loc) · 1.97 KB
/
scrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import requests
import time
from facebook_scraper import get_posts, exceptions
from conftest import post as test_db
DELAY_SECONDS = 2
class Post(object):
def __init__(self, page_name: str, num_pages: int):
self.page_name = page_name
self.num_pages = num_pages
self.post_info = {}
self.url = "https://www.facebook.com/"
def check_connection(self) -> bool:
try:
get = requests.get(self.url)
if get.status_code == 200:
print(f"{self.url}: is reachable")
return True
else:
time.sleep(1)
print(f"{self.url}: is Not reachable, status_code: {get.status_code}")
return False
except requests.exceptions.ConnectionError as e:
raise ConnectionError(f"{self.url}: is Not reachable \nErr: {e}")
# will scrap all the text, photo, and time as one dict
def scrape_full_post(self) -> dict:
if self.check_connection():
keys = ['text', 'photo', 'time']
try:
for post in get_posts(self.page_name, pages=self.num_pages):
if self.resume_scarping(f_posts=post, last_post=test_db):
print("resuming scraping..")
time.sleep(DELAY_SECONDS)
values = [post['post_text'], post['image'], post['time']]
self.post_info = dict(zip(keys, values))
if self.post_info:
yield self.post_info
else:
raise Exception(exceptions.NotFound)
else:
print("Last post already scraped")
except exceptions.NotFound as e:
raise print(e)
@staticmethod
def resume_scarping(f_posts: dict, last_post: dict) -> bool:
if f_posts['time'] > last_post['time']:
return True
else:
return False