/
filesearch.py
198 lines (169 loc) · 6.78 KB
/
filesearch.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#! /usr/bin/python
import os
from safetyqueue import SafetyQueue
import xlrd
import xlwt
from sharefile import ShareFile
import requests
import json
from bs4 import BeautifulSoup
from log import logger
from htmlparser import Parser
import time
class FileFinder:
count=0
def __init__(self, unvisited, visited, shares):
self.unvisited = unvisited
self.visited = visited
self.shares = shares
self.session = requests.session()
self.parser = Parser()
agent = {'user-agent',
r'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
self.session.headers = agent
self.stop = False
def findshare(self):
while not self.stop and self.unvisited.size() != 0:
publisher = self.unvisited.pop()
if publisher == None:
time.sleep(2)
continue
#construct a new session
self.session = requests.session()
agent = {'user-agent',
r'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
self.session.headers = agent
self.findShareFromPublisher(publisher)
self.visited.push(publisher)
FileFinder.count += 1
if FileFinder.count >= 5:
self.writepublishback()
FileFinder.count = 0
time.sleep(3)
self.writepublishback()
def stopWork(self):
self.stop = True
def findShareFromPublisher(self, p):
url = r'https://vdisk.weibo.com/u/' + p;
self.findShareFromUrl(url)
def findShareFromUrl(self, url, travseSibling = True):
try:
r = self.session.get(url)
if r.status_code == 200:
self.listFile(r.text, url)
# travser other pages
if travseSibling == True:
soup = BeautifulSoup(r.text, "html.parser")
nextPages = self.pageList(soup)
if len(nextPages) > 0:
for p in nextPages:
logger.info('To find share in page %s' % (url + p))
self.findShareFromUrl(url + p, travseSibling = False) #no need to traverse sibling
except Exception as e:
logger.warning("Unexpected error: %s"%str(e))
def writepublishback(self):
v = self.visited.clone()
u = self.unvisited.clone()
with open('visited.txt', 'w') as f:
for s in v:
f.write(s+'\n')
with open('unvisited.txt', 'w') as f:
for s in u:
f.write(s+'\n')
def listFile(self, text, url):
items = self.parser.getSharedItems(text)
for item in items:
if not item['is_dir']:
bytes = 'unknown'
if 'bytes' in item:
bytes = item['bytes']
sina_uid = 'unknown'
if 'sina_uid' in item:
sina_uid = item['sina_uid']
elif 'uid' in item:
sina_uid = item['uid']
sf = ShareFile(item['filename'], bytes, item['url'], sina_uid)
self.shares.push(sf)
else:
self.searchInDirectory(item['url'])
def pageList(self, soup):
vd_page = soup.find(name='div', attrs={'class': 'vd_page'})
pages = []
if vd_page != None:
page_links = vd_page.find_all(name='a')
for p in page_links:
if pages.count(p.attrs['href']) == 0: # ignore the next page by button
pages.append(p.attrs['href'])
#link page is not continuous, construct link page in continuous way
if len(pages) != 0:
min = int(pages[0].split(sep='=')[1])
max = int(pages[len(pages)-1].split(sep='=')[1])
pages.clear()
for i in range(min, max+1):
pages.append('?page=%d'%i)
logger.info('pages is %s' % pages)
return pages
def searchInDirectory(self, dir_url, travserSibling = True):
try:
r = self.session.get(dir_url)
if r.status_code == 200:
self.listFile(r.text, dir_url)
if (travserSibling):
pages = self.pageListInDirectory(r.text, dir_url)
for p in pages:
self.searchInDirectory(dir_url+p, travserSibling = False)
except Exception as e:
logger.warning("Unexpected error: %s"%str(e))
def pageListInDirectory(self, text, dir_url):
soup = BeautifulSoup(text, "html.parser")
vd_page = soup.find(name='div', attrs={'class': 'vd_page'})
pages = []
if vd_page != None:
page_links = vd_page.find_all(name='a')
for p in page_links:
if pages.count(p.attrs['href']) == 0: # ignore the next page by button
pages.append(p.attrs['href'])
if len(pages) != 0:
minlink = pages[0]
maxlink = pages[len(pages)-1]
min = int((((minlink.split(sep='&'))[2]).split(sep='='))[1])
max = int((((maxlink.split(sep='&'))[2]).split(sep='='))[1])
pages.clear()
for i in range(min, max+1):
p = minlink[0:len(minlink)-1] + str(i)
pages.append(p)
logger.info("page in directory: %s"%pages)
return pages
def convertJsonItem(self, tag):
downloadtag = tag.find(name='a', attrs={'class':'vd_pic_v2 vd_dload'})
share_item = json.loads(downloadtag.attrs['data-info'])
share_item['url'] = self.stripSlash(share_item['url'])
return share_item
def stripSlash(self, str):
s = ''
for c in str:
if c != r'\\':
s += c
return s
if __name__ == '__main__':
visited = SafetyQueue()
unvisited = SafetyQueue()
shares = SafetyQueue()
founder = FileFinder(visited, unvisited, shares)
'''
with open('vdisk.html', encoding='UTF-8') as f:
s = f.read()
founder.listFile(s)
'''
url = r'https://vdisk.weibo.com/u/5698929130'
founder.findShareFromUrl(url)
#founder.findShare('5811552055')
'''
for i in range(1, 100):
shares.push(ShareFile(str(i), str(i)))
founder = FileFinder(visited, unvisited, shares)
founder.writeFindingToExcel()
for i in range(100, 200):
shares.push(ShareFile(str(i), str(i)))
founder.writeFindingToExcel()
'''