This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
85 lines (75 loc) · 2.9 KB
/
Main.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
# -- coding: utf-8 --
import urllib2
import MySQLdb
import json
import time
import Logger
from Properties import Properties
class PastebinScraper:
client = None
cursor = None
conn = None
def getpastetext(self, paste_key):
try:
req = urllib2.urlopen('https://scrape.pastebin.com/api_scrape_item.php?i=' + paste_key, timeout=3)
pastetext = req.read()
file = open(Properties.pastedirectory + paste_key, "w")
file.write(pastetext)
file.close()
return pastetext
except IOError as e:
Logger.log("getPasteText", e)
print("[!] API Error1:", e)
def initdb(self):
Logger.log("initDB", "Initializing database..")
try:
self.conn = MySQLdb.connect(
host=Properties.host,
user=Properties.user,
passwd=Properties.password,
db=Properties.database,
use_unicode=True,
charset=Properties.charset
)
self.conn.set_character_set('utf8')
self.cursor = self.conn.cursor()
self.cursor.execute('SET NAMES utf8mb4;')
self.cursor.execute('SET CHARACTER SET utf8mb4;')
self.cursor.execute('SET character_set_connection=utf8mb4;')
except Exception as e:
Logger.log("initDB", e)
print("[!] API Error:", e)
def scraper(self):
data, limit = self.getlimiteddata()
Logger.log("scraper", "Starting looping through pastes")
for d in range(limit):
print("date: " + data[d]["date"])
txt = self.getpastetext(data[d]["key"])
try:
self.cursor.execute(u"""INSERT INTO P2 (fullurl, pastedate, pastekey, size, expire, title, syntax, user, txt)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)""", (data[d]["full_url"], data[d]["date"], data[d]["key"], data[d]["size"], data[d]["expire"], data[d]["title"], data[d]["syntax"], data[d]["user"], txt))
self.conn.commit()
except Exception as e:
Logger.log("scraper", e)
print("[!] API Error3:", e)
def getlimiteddata(self):
limit = 100
fetchurl = 'https://scrape.pastebin.com/api_scraping.php?limit=' + str(limit)
req = urllib2.urlopen(fetchurl, timeout=3)
data = json.loads(req.read())
return data, limit
if __name__ == "__main__":
Logger.log("main", "Starting pastebin scrapper")
paste = PastebinScraper()
paste.initdb()
while True:
try:
start = time.time()
paste.scraper()
end = time.time()
secsleft = 60 - (int(round(end - start)))
Logger.log("main", str(secsleft) + " seconds sleeping")
time.sleep(secsleft)
except Exception as e:
Logger.log("main", e)
print("[!] API Error4:", e)