-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshort_codes.py
More file actions
72 lines (53 loc) · 2.09 KB
/
short_codes.py
File metadata and controls
72 lines (53 loc) · 2.09 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
import os
import json
import requests
import logging
from pathlib import Path
from dotenv import load_dotenv
BASE_DIR = Path(__file__).resolve().parent
load_dotenv(BASE_DIR/ '.env')
log_file_path = os.path.join(BASE_DIR/"logs", 'short_codes.log')
logger = logging.getLogger(__name__)
logging.basicConfig(
filename=log_file_path,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
level=logging.INFO
)
shortcode_file_path = os.path.join(BASE_DIR/"from_shortcodes", 'shortcodes.json')
# fetch env variables
login_url = os.getenv('LOGIN_URL')
login_success_url = os.getenv('LOGIN_SUCCESS_URL')
protected_url = os.getenv('PROTECTED_URL_BASE')
username = os.getenv('LOGIN_USERNAME')
password = os.getenv('LOGIN_PASSWORD')
payload = {
'usernameOrEmail': username,
'password': password
}
# Scraping logic
session = requests.Session()
try:
login_response = session.post(login_url, data=payload)
if login_response.url == login_success_url:
logger.info(f"Login successful! This is the success URL: {login_response.url}")
response = session.get(f"{protected_url}shortcode?_=1763835813693")
logging.info(f"Fetched shortcodes from {protected_url}shortcode?_=1763835813693")
if response.status_code == 200:
shortcodes = response.json()
try:
with open(shortcode_file_path, 'w') as json_file:
json.dump(
shortcodes,
json_file,
indent=4
)
logger.info(f"Shortcodes successfully written to {shortcode_file_path}")
except Exception as e:
logger.exception("Error while writing to file")
else:
logger.error(f"Failed to fetch shortcodes, status code: {response.status_code}")
else:
logger.error(f"Login failed with status code: {login_response.status_code}")
except Exception as e:
logger.exception("An error occurred during the scraping process")