-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (56 loc) · 2.11 KB
/
Copy pathmain.py
File metadata and controls
73 lines (56 loc) · 2.11 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import argparse as ap
from datetime import datetime
from time import sleep
from instaloader import ProfileNotExistsException, ConnectionException
from configs import *
from scraping import InstagramScraper
def init_args_parser() -> ap.ArgumentParser:
parser = ap.ArgumentParser(
description='Python module for collecting information about user posts on Instagram '
'for the selected time period. The collected information is saved in the PostgreSQL database.'
)
parser.add_argument(
'username',
type=str,
help='the account whose posts data will be saved into the PostgreSQL database'
)
parser.add_argument(
'start_time',
type=str,
help='the beginning of the publication time period (in the format: 2022-01-01)'
)
parser.add_argument(
'end_time',
type=str,
help='the end of the publication time period (in the format: 2022-01-01)'
)
return parser
def main():
parser = init_args_parser()
args = parser.parse_args()
try:
start_time = datetime.fromisoformat(args.start_time)
end_time = datetime.fromisoformat(args.end_time)
except ValueError as ex:
print('exception occurred while converting start_time, end_time from strings to datetime objects: %s',
ex)
return
if start_time > end_time:
print('end_time date cannot be earlier then start_time date: start_time=%s, end_time=%s',
start_time, end_time)
return
while True:
try:
scraper = InstagramScraper(INSTA_LOGIN, INSTA_PASSWORD, DATABASE_URL)
break
except ConnectionException as ex:
print('exception occurred while getting %s account info: %s', args.username, ex)
print('sleep 10 second and retry to connect...')
sleep(10)
try:
scraper.scrape_posts(args.username, start_time, end_time)
except ProfileNotExistsException as ex:
print('exception occurred while getting %s account info: %s', args.username, ex)
return
if __name__ == '__main__':
main()