-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrawler.py
89 lines (79 loc) · 3.67 KB
/
crawler.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
import os
import json
import subprocess
from bs4 import BeautifulSoup
import sys
def runProcess(commandString):
p = subprocess.Popen(commandString, shell=True, stderr=subprocess.PIPE)
output, err = p.communicate()
def urlFetch(targetFile,targetUrl):
runProcess("wget --no-check-certificate --output-document \"" + targetFile + "\" \""+targetUrl+"\"")
def downloadApps(targetAppDownloadLinks,targetDir):
for category in targetAppDownloadLinks.keys():
targetCategoryDir = targetDir + "/" + category
if not os.path.exists(targetCategoryDir):
os.makedirs(targetCategoryDir)
for app in targetAppDownloadLinks[category].keys():
targetFile = targetCategoryDir + "/" + app + ".apk"
if not os.path.exists(targetFile):
print "Downloading:"+targetAppDownloadLinks[category][app]+" to "+targetFile
urlFetch(targetFile, targetAppDownloadLinks[category][app])
def slideme_getAllAppLinks(targetAppLinkDict,targetFileName,topUrl):
targetData = None
with open (targetFileName, "r") as myfile:
targetData=myfile.read()
if targetData:
parsed_html = BeautifulSoup(targetData)
for childApp in parsed_html.find("ul", { "class" : "category-template" }).find_all("li"):
urlLink = childApp.find("div",{"class":"category-template-down"}).find("a").get("href")
targetUrl = topUrl + urlLink
if not (targetUrl in targetAppLinkDict):
targetAppLinkDict.append(targetUrl)
def slideme_getAppDownloadLinks(targetAppDownloadLinks,targetAppLinkDict,tempWorkingDir):
targetData = None
targetFileName = tempWorkingDir + "/dummyAppd.html"
for tempUrl in targetAppLinkDict:
urlFetch(targetFileName,tempUrl)
with open (targetFileName, "r") as myfile:
targetData=myfile.read()
if targetData:
parsed_html = BeautifulSoup(targetData)
for childApp in parsed_html.find_all("div", { "class" : "fast-download-box" }):
try:
appName = childApp.find("h1").find("span",{"class":"file"}).get_text()
for para in childApp.find_all("p"):
try:
urlLink=para.find("a",{"id":"download_link"}).get("href")
except:
pass
category = "entertainment"
targetKey = category.lower()
appName = str(appName).strip()
if not (targetKey in targetAppDownloadLinks):
targetAppDownloadLinks[targetKey] = {}
if not (appName in targetAppDownloadLinks[targetKey]):
targetAppDownloadLinks[targetKey][appName] = urlLink
except:
pass
if len(sys.argv) != 3:
print "Usage:"+sys.argv[0] +" <downloadDirectory> <tempWorkingDir>"
sys.exit(-1)
tempWorkingDir = sys.argv[2]
downloadDir = sys.argv[1]
if not os.path.exists(tempWorkingDir):
os.makedirs(tempWorkingDir)
if not os.path.exists(downloadDir):
os.makedirs(downloadDir)
maxPageNumber = 20
urlPrefix = "https://apkpure.com/entertainment?page="
currPageNo = 1
while currPageNo <= maxPageNumber:
targetAppLinks = []
targetAppDownloadLinks = {}
targetUrl = urlPrefix + str(currPageNo)
targetFileName = tempWorkingDir + "/dummyd.html"
currPageNo = currPageNo + 1
urlFetch(targetFileName, targetUrl)
slideme_getAllAppLinks(targetAppLinks, targetFileName,"http://apkpure.com")
slideme_getAppDownloadLinks(targetAppDownloadLinks,targetAppLinks,tempWorkingDir)
downloadApps(targetAppDownloadLinks,downloadDir)