-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathscraper.py
208 lines (157 loc) · 6.19 KB
/
scraper.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
199
200
201
202
203
204
205
206
207
208
import os
import shutil
import sqlite3
import ssl
import time
import platform
from urllib.request import urlopen
try:
import httplib
except:
import http.client as httplib
# check whether the internet is working or not
def have_internet():
conn = httplib.HTTPConnection("www.google.com", timeout=5)
try:
conn.request("HEAD", "/")
conn.close()
return True
except:
conn.close()
return False
# --------------------------------------------------------------------
def exit_gracefully():
f.close()
conn.commit()
print("Exiting....")
exit()
# --------------------------------------------------------------------
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
if __name__ == '__main__':
target = 1000000
if platform.system()=="Linux":
download_dir = os.getcwd() + "/tmp/"
else:
download_dir = os.getcwd() + "\\tmp\\"
database = "g_x_apps.sqlite"
conn = sqlite3.connect(database)
cur = conn.cursor()
base_site = "http://app.mi.com/download/"
# load the last id number from which to continue downloading
f = open("ids_done.txt", "r")
numbers = f.readlines()
numbers = [a.rstrip() for a in numbers]
curr_num = int(numbers[-1])
f.close()
f = open("ids_done.txt", "a")
c = 1
while curr_num != target:
try:
curr_num += 1
c += 1
# commit after every 500 iterations
if c == 500:
conn.commit()
c = 0
# check net connectivity
if not have_internet():
# save progress
conn.commit()
f.close()
print("Internet disconnected.. waiting")
while not have_internet():
time.sleep(5)
f = open("ids_done.txt", "a")
print("Connected!")
print("------------------------------------------")
print("Processing:", curr_num)
# empty left over downloads
if platform.system() == 'Linux':
os.system("rm -f '" + download_dir + "{*,.*}'")
else:
os.system('del "' + download_dir + '*" /Q')
# ------------------------------------------------------------------
# check whether app on xiomi exists or not
try:
url = base_site + str(curr_num)
html = urlopen(url, context=ctx)
# check apk exists or not
if html.url[-4:] != ".apk":
f.write(str(curr_num) + "\n")
print("No app against this ID on Xiaomi Store")
continue
# if it exists
package = html.url.split('/')[-1]
package = package.split(".apk")[0]
print("Found on Xiaomi:", package)
except:
f.write(str(curr_num) + "\n")
print("No app against this ID on Xiaomi Store")
continue
# ------------------------------------------------------------------
# ------------------------------------------------------------------
# check if the same app exists on google
try:
url = "https://play.google.com/store/apps/details?id=" + package
html = urlopen(url, context=ctx)
if str(html.getcode()) != '200':
f.write(str(curr_num) + "\n")
print(package, " doesn't exist on Play Store")
continue
# if it exists
print("Found on PlayStore:", package)
except:
f.write(str(curr_num) + "\n")
print(package, " doesn't exist on Play Store")
continue
# ------------------------------------------------------------------
# ------------------------------------------------------------------
# download google
print("Downloading from Playstore")
os.system('gplaycli -d ' + package + ' -f "' + 'google_apps"' + ' -p')
# check if a file of that apk is created
if platform.system() == "Linux":
dir = os.getcwd() + "/google_apps/" + package + ".apk"
else:
dir = os.getcwd() + "\\google_apps\\" + package + ".apk"
time.sleep(1)
save = False
# check if that directory exists
if os.path.exists(dir):
save = True
if not save:
f.write(str(curr_num) + "\n")
print("App from playstore not downloaded (might be paid app or not available in your country).")
continue
print(package, ": Google Download Successful")
# ------------------------------------------------------------------
# ------------------------------------------------------------------
# download xiomi
print("Downloading from Xiaomi")
os.system("wget -P tmp/ --content-disposition -q " + base_site + str(curr_num))
# rename the file and move it to its folder
for file in os.listdir(download_dir):
if file.endswith(".apk"):
print('Moving Xiomi File...')
shutil.move(download_dir + file, "xiomi_apps/" + package + ".apk")
cur.execute("INSERT OR IGNORE INTO APPS VALUES (?,1,1,0)",
(package,))
print(package, ": Xiomi Download Successful")
else:
error = 'Error in downloading'
print(error, package)
# ------------------------------------------------------------------
f.write(str(curr_num) + "\n")
except KeyboardInterrupt:
exit_gracefully()
except:
pass
print("-----------------------------")
print("Downloads Complete!!!")
print("-----------------------------")
print('\n\nProcess Successfully finished!!!!!\n\n')
f.close()
conn.commit()