-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (49 loc) · 2.38 KB
/
main.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
import os
import auth
import WP_request_handel
import json
import time
import urllib.parse
import ImageCreation
Authorization, Web_URL = auth.authorization()
NUM_OF_PAGE = 8
count = 0
all_posts = [] # Initialize an empty list to collect posts
for i in range(1, NUM_OF_PAGE):
result = WP_request_handel.posts_without_featured_image(Web_URL, Authorization, i)
dump_json = json.dumps(result)
result_json = json.loads(dump_json)
for post in result_json:
print("id = ", post['id'], "title = ", post['title']["rendered"], "post = ", post['featured_media'])
if post['featured_media'] == 0:
count += 1
print("How many posts updated ", count)
all_posts.append(post)
# Handle special characters in titles for image creation
title = post['title']['rendered']
title = title.replace("–", "-") # Replace "-" with ","
title = title.replace("&", "and") # Replace "&" with "and"
title = title.replace("…", "...") # Remove "?" from the title
title = title.replace("’s", "'s")
title = title.replace("’t", "'t")
# Update the title in the post dictionary (if needed for further processing)
post['title']['rendered'] = title
# image creation
IMG_DIR = ImageCreation.image_creation(str(title))
# Image upload
image_upload_response = WP_request_handel.upload_image(IMG_DIR, Web_URL, Authorization)
dump_json = json.dumps(image_upload_response, indent=4)
image_upload_response_result_json = json.loads(dump_json)
# Image update per post
image_id = image_upload_response_result_json["id"]
post_id = post['id']
end_result = WP_request_handel.update_post_with_featured_image(post_id, image_id, Web_URL, Authorization)
dump_json = json.dumps(end_result, indent=4)
end_result_json = json.loads(dump_json)
os.remove(IMG_DIR) # Remove the image after uploading
print(end_result_json)
print(count)
print(i, "page done -------- !!")
# After collecting all posts, write them to the file as a JSON array.
with open("JASON Response/data.json", "w") as outfile: # Corrected the folder name from "JASON Response" to "JSON Response"
json.dump(all_posts, outfile, indent=4)