-
Notifications
You must be signed in to change notification settings - Fork 21
/
json_parse.py
92 lines (70 loc) · 2.09 KB
/
json_parse.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
# -*- coding: utf-8 -*-
import json
# from json import loads
# import json as JSON
JSON_FIlE = 'likes.json'
url_list = open("url_list.txt",'w',encoding='utf-8')
def open_json_file():
with open(JSON_FIlE,'r',encoding="utf-8") as load_f:
'''
raw_jsons = load_f.read()
raw_jsons = raw_jsons.split("\n")
# print(raw_jsons)
json_lists = list()
for raw_json in raw_jsons:
json_list = raw_json.lstrip().rstrip()
if json_list:
json_lists.append(json_list)
'''
json_lists = list()
json_lists = load_f.readlines()
print('list_len = ',len(json_lists))
return json_lists
def get_pic(json_data,item_len):
for x in range(0,item_len):
try:
pic_len = len(json_data['response']['liked_posts'][x]['photos'])
# print('len = ',pic_len)
for y in range(0,pic_len):
url_item = json_data['response']['liked_posts'][x]['photos'][y]['original_size']['url']
# print(url_item)
url_list.write(url_item+'\n')
except Exception as e:
# print("video")
pass
# print("pic_cnt = ",pic_cnt)
# return pic_len
def get_video(json_data,item_len):
for x in range(0,item_len):
try:
url_item = json_data['response']['liked_posts'][x]['video_url']
# print(url_item)
url_list.write(url_item+'\n')
except Exception as e:
# print("pic")
pass
# print('video_cnt = ',video_cnt)
# return video_cnt
def get_content(json_data):
item_len = len(json_data['response']['liked_posts'])
print("item_len = ", item_len)
try:
# print( len(load_dict['response']['liked_posts'][i]['photos']) ) #[0]['original_size']['url'] ) #pic
# print( load_dict['response']['liked_posts'][i]['video_url'] ) #video
get_pic(json_data, item_len)
get_video(json_data, item_len)
except Exception as e:
print("error")
return item_len
# if __name__ == "__main__":
def main():
print('#2 json_parse main()')
json_lists = open_json_file()
item_len = 0
for i in range(0, len(json_lists)):
json_data = json.loads(json_lists[i])
item_len += get_content(json_data)
url_list.close()
print("item total = ", item_len)
if __name__ == "__main__":
main()