-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheqsl_saver.py
More file actions
162 lines (157 loc) · 6.28 KB
/
Copy patheqsl_saver.py
File metadata and controls
162 lines (157 loc) · 6.28 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
""" Save QSLs from eqsl.cc Archive directory """
import os
import re
import sys
import shutil
import time
import getpass
import requests
from bs4 import BeautifulSoup
MAIN_URL = "https://www.eqsl.cc/qslcard/"
URLS = {
"LOGIN": MAIN_URL + "Login.cfm",
"LOGIN_FORM": MAIN_URL + "LoginFinish.cfm",
"COOKIETEST": MAIN_URL + "CookieTest.cfm?sw=1280&sh=1024",
"QSLLIST": MAIN_URL + "Inbox.cfm?Archive=1&Reject=0"
}
if len(sys.argv) != 2:
if sys.platform == "win32":
print("Usage: python eqsl_saver.py username")
elif sys.platform == "linux":
print("Usage: python3 eqsl_saver.py username")
sys.exit()
pw = getpass.getpass()
LOGIN_FIELDS = {
"Callsign": sys.argv[1],
"EnteredPassword": pw,
"ZeroType": "Slash"
}
if not os.path.exists("cards"):
print("Create directory")
os.makedirs("cards")
req = requests.Session()
print("Load login page...", end=" ")
login_resp = req.get(URLS[
"LOGIN"],
timeout=100
)
if login_resp.status_code == 200:
print("...success!")
print("Wait 1 second...")
time.sleep(1)
print("Send login datas...", end=" ")
login_form_resp = req.post(
URLS["LOGIN_FORM"],
data=LOGIN_FIELDS,
timeout=100
)
if login_form_resp.status_code == 200:
print("...success!")
print("Wait 3 seconds...")
time.sleep(3)
print("Load cookietest page...", end=" ")
cookietest_resp = req.get(
URLS["COOKIETEST"],
timeout=100
)
if cookietest_resp.status_code == 200:
print("...success!")
print("Wait 1 second...")
time.sleep(1)
print("Load archive page...", end=" ")
archive_resp = req.get(
URLS["QSLLIST"],
timeout=100
)
if archive_resp.status_code == 200:
print("...success!")
archive_soup = BeautifulSoup(
archive_resp.text,
'html.parser'
)
table_rows = archive_soup.find(id="MainForm").find("table").find_all("tr")
rownum = len(table_rows)
for i in range(2, rownum-1):
display_eqsl_cols = table_rows[i].find_all("td")
display_eqsl_href = display_eqsl_cols[0].find("a")["href"]
display_eqsl_callsign = display_eqsl_cols[1].find("a").text
display_eqsl_datetime = display_eqsl_cols[2].contents
display_eqsl_url = MAIN_URL + re.sub(
r"Javascript:popupPrintPage\('(.[^']*)'.*",
r"\1",
display_eqsl_href
)
fname = display_eqsl_callsign + "_"
fname += display_eqsl_datetime[0].strip() + "_"
fname += display_eqsl_datetime[2].strip().replace(":", "-")
fname += ".jpg"
if os.path.isfile(
os.path.join("cards", fname)
):
print(
"#" + str(i - 1) + ": " +
"eQSL card page from " +
display_eqsl_callsign +
" QSO at " +
display_eqsl_datetime[0].strip() +
" " +
display_eqsl_datetime[2].strip() +
" exists - omit."
)
else:
print("Wait 10 seconds...")
time.sleep(10)
print(
"#" + str(i - 1) + ": " +
"Load eQSL card page from " +
display_eqsl_callsign +
" QSO at " +
display_eqsl_datetime[0].strip() +
" " +
display_eqsl_datetime[2].strip() +
"...",
end=""
)
display_eqsl_resp = req.get(
display_eqsl_url,
timeout=100
)
if display_eqsl_resp.status_code == 200:
print("...success!")
display_eqsl_img_soup = BeautifulSoup(
display_eqsl_resp.text,
'html.parser'
)
try:
display_eqsl_img_src = display_eqsl_img_soup.find("img")["src"]
except TypeError:
print("ERROR: Image not found in QSL!")
else:
print("Wait 3 seconds...")
time.sleep(3)
print(
"#" + str(i - 1) + ": " +
"Save image...",
end=" "
)
display_eqsl_img_resp = requests.get(
"https://www.eqsl.cc/" + display_eqsl_img_src,
timeout=100,
stream=True
)
if display_eqsl_img_resp.status_code == 200:
print("...success!")
with open(
os.path.join("cards", fname),
'wb'
) as f:
display_eqsl_img_resp.raw.decode_content = True
shutil.copyfileobj(display_eqsl_img_resp.raw, f)
else:
print("...archive ERROR")
else:
print("...cookietest ERROR")
else:
print("...login Form ERROR")
else:
print("...login page ERROR")