-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprntsc.py
107 lines (79 loc) · 3.24 KB
/
prntsc.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
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
import urllib.request
import urllib.error
import random
import os
from datetime import datetime
from numberFormatter import formatNum
from timeConverter import convertTime
from sys import argv
def timeInSecs(x):
return (x.hour * 3600) + (x.minute * 60) + (x.second)
outputFolder = "output" # change if there is already a folder called output which is important
if not os.path.exists(outputFolder):
os.mkdir(outputFolder)
print("prnt.sc image scraper")
done = False
while not done:
try:
if len(argv) == 1:
amount = int(input("\nHow many pictures do you want? "))
else:
amount = int(argv[1])
except ValueError:
print("Please enter a number\n")
exit(-1)
if amount < 1:
print("Amount of images must be at least 1.")
else:
try:
with open("timings.txt") as f:
averageTime = f.read()
averageTime = float(averageTime)
except FileNotFoundError:
averageTime = 1.1 # default, about 1.1 images per second
except ValueError:
print("Warning: File 'timings.txt' is corrupted, deleting file...\n")
os.remove("timings.txt")
averageTime = 1.1
timeToScrape = amount / averageTime
print(f"It will take about {convertTime(timeToScrape)} to gather {amount} images.")
action = input("Continue? (y/n) ").lower()
if action in ("yes", "y"):
done = True
startTime = datetime.now().time()
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
}
chars = "a b c d e f g h j i k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9".split()
codeList = []
i = 1
while i <= amount:
code = ""
for j in range(6):
code += random.choice(chars)
if code[0] != "0" and code not in codeList:
codeList.append(code)
try:
print(f"[#{formatNum(i, 'lz', len(str(amount)) - len(str(i)))}] Trying code '{code}'...")
pageURL = f"https://prnt.sc/{code}"
pageRequest = urllib.request.Request(url=pageURL, headers=headers)
pageContent = str(urllib.request.urlopen(pageRequest).read())
imageURL = pageContent[int(pageContent.find("no-click screenshot-image") + 32) : len(pageContent)]
imageURL = imageURL[0 : imageURL.find('"')]
imageRequest = urllib.request.Request(url=imageURL, headers=headers)
image = urllib.request.urlopen(imageRequest)
with open(f"output/{code}.png", "wb") as f:
f.write(image.read())
i += 1
except urllib.error.HTTPError:
print(f"[!] HTTP Error with code '{code}'")
except:
print(f"[!] Error with code '{code}'")
elif code in codeList:
print(f"[!] Error: code '{code}' has already been saved")
i += 1
endTime = datetime.now().time()
timeDiff = timeInSecs(endTime) - timeInSecs(startTime)
with open("timings.txt", "w") as f:
f.write(str((averageTime + (amount / timeDiff)) / 2))
input(f"\n{amount} images were gathered in about {convertTime(timeDiff)}. Press 'Enter' to close program.")