-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate json.py
198 lines (168 loc) · 5.89 KB
/
generate json.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
import pickle
import json
import pprint
import math
import os
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from urllib import parse
from jsonmerge import merge
import requests
# get latest with offset, https://chorus.fightthe.pw/api/latest?from=0
# get total count, https://chorus.fightthe.pw/api/count
pages = 1
# start_song = 14931
start_song_page = math.ceil(int(start_song) / 20)
total = 0
start = 1
count = requests.get('https://chorus.fightthe.pw/api/count')
count = str(count.json())
pages = math.ceil(int(count) / 20)
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
data = {}
def percentage(part, whole):
return 100 * float(part)/float(whole)
def getsize(id):
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
try:
# Call the Drive v3 API
request = service.files().get(fileId=id, fields='*')
response = request.execute()
# pprint.pprint(response)
if 'size' in response:
size = response['size']
# print(size)
else:
size = 0
return size
except:
return 'error'
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return "%s %s" % (s, size_name[i])
def write_file(data, filename):
json_object = json.dumps(data, indent = 4)
with open(filename, "w") as outfile:
outfile.write(json_object)
cat_json("data.json", "dump.json")
data = {}
def write_file1(data, filename):
json_object = json.dumps(data, indent = 4)
with open(filename, "w") as outfile:
outfile.write(json_object)
data = {}
def cat_json(out, in1):
outfile = open(out, "r")
with open(out) as outfile:
outfiledata = json.load(outfile)
with open(in1) as infile1:
infile1data = json.load(infile1)
result = merge(outfiledata, infile1data)
write_file1(result, "data.json")
def get_bytes(size, suffix):
size = int(float(size))
suffix = suffix.lower()
if suffix == 'b':
return size
if suffix == 'kb' or suffix == 'kib':
return size << 10
elif suffix == 'mb' or suffix == 'mib':
return size << 20
elif suffix == 'gb' or suffix == 'gib':
return size << 30
return False
def size2():
total = 0
with open("data.json", "r") as outfile:
outfiledata = json.load(outfile)
for v in outfiledata:
# print(v)
for f in outfiledata[v]['files']:
# print(f)
size = outfiledata[v]['files'][f]['size']
raw = size.split(" ")
try:
by = get_bytes(raw[0], raw[1])
except:
pass
total = by + total
# print(total)
return convert_size(total)
def main():
print("starting")
for x in range(start_song_page, pages):
print()
print("---------------")
print("CURRENT PAGE IS: " + str(x))
global start
if start == 1:
y = start_song
start = 0
else:
y = x*20
response = requests.get('https://chorus.fightthe.pw/api/latest?from=' + str(y))
jsonResponse = response.json()
for x in range(20):
offset = x+y
name = jsonResponse['songs'][x]['name']
link = jsonResponse['songs'][x]['link']
direct = jsonResponse['songs'][x]['directLinks']
print("TOTAL NUMBER OF SONGS: " + str(count))
print("CURRENT SONG OFFEST IS: " + str(offset))
print("CURRENT SONG IS: " + name)
print("percentage done is: " + str(round(percentage(offset, count),2)) + "%")
print("total size so far: " + size2())
data = {}
data[offset] = {}
data[offset]['name'] = name
data[offset]['files'] = {}
for k, v in direct.items():
if "google" in v:
print(v + " IS GOOGLE!")
id = dict(parse.parse_qsl(parse.urlsplit(v).query))
id = id['id']
raw_size = getsize(str(id))
if raw_size != 'error':
size = convert_size(int(raw_size))
else:
size = raw_size
data[offset]['files'][k] = {}
data[offset]['files'][k]['size'] = size
data[offset]['files'][k]['link'] = v
if size != 'error':
write_file(data, "dump.json")
else:
data = {}
else:
print(v + " IS NOT GOOGLE!")
pass
print("---------------")
if __name__ == '__main__':
main()