BlossomTheme Web Builder
Automated website builder for theme repositories
Loading...
Searching...
No Matches
builder.py
Go to the documentation of this file.
1import requests
2import json
3from datetime import date as dt
4import datetime
5import os
6import urllib.request
7import re
8import markdown
9from bs4 import BeautifulSoup
10
12 url = "https://api.github.com/orgs/BlossomTheme/repos?type=all&sort=created&direction=desc&per_page=100"
13 resp = requests.get(url)
14 data = resp.json()
15 to_be_updated = []
16 init_date = dt(2024, 7, 22)
17
18 with open("./builder.conf", "r")as file:
19 for line in file:
20 if "#" not in line:
21 init_date = dt.fromisoformat(line)
22 else:
23 continue
24
25 for entry in data:
26 date_created = dt.fromisoformat(entry["created_at"].split("T")[0])
27 if date_created >= init_date:
28 to_be_updated.append(entry["name"])
29
30 print("Fetching to be updated list")
31 return to_be_updated
32
33
34def create_project_page(name, description, image_locations, installation_instructions):
35 img_pattern = r'(<div id="images-go-here">\s*)(</div>)'
36 replacement = ""
37 installation_instructions = markdown.markdown(installation_instructions)
38
39 os.system(f"cp ./pages/SAMPLE.html ./pages/{name.lower()}.html")
40
41 with open(rf'./pages/{name.lower()}.html', 'r') as file:
42
43 data = file.read()
44 data = data.replace("-TITLE-", name)
45 data = data.replace("-DESCRIPTION-", description)
46 data = data.replace("-INSTALLATION-", installation_instructions)
47
48
49 for image in image_locations:
50 img_tags = f"<img src=\"https://raw.githubusercontent.com/BlossomTheme/{name}/main/{image}\" width=\"75%\">"
51 replacement = replacement + r'\1' + img_tags + r'\2' + '\n'
52
53 data = re.sub(img_pattern, replacement, data)
54
55
56 with open(rf'./pages/{name.lower()}.html', 'w') as file:
57 file.write(data)
58
59 print(f"Created project page for {name}")
60
61
63 url = f"https://api.github.com/repos/BlossomTheme/{name}"
64 images = []
65 pattern = r'!\[\]\‍((.*?)\‍)'
66 extracted_image_paths = []
67 read_me_file = urllib.request.urlopen(f"https://raw.githubusercontent.com/BlossomTheme/{name}/main/README.md")
68 installation_instructions = ""
69 installation_header_found = False
70
71 resp = requests.get(url)
72 data = resp.json()
73
74 description = data["description"]
75
76 for line in read_me_file:
77 if "![]" in line.decode('utf-8'):
78 images.append(line.decode('utf-8').replace("\n", ""))
79
80 elif "## Installation" in line.decode('utf-8'):
81 installation_header_found = True
82 continue
83
84 elif installation_header_found:
85 installation_instructions = installation_instructions + (line.decode('utf-8')).strip() + '\n'
86
87 else:
88 continue
89
90 for item in images:
91 match = re.search(pattern, item)
92 if match:
93 path = match.group(1)
94 if path.startswith('./'):
95 path = path[2:] # Remove './' from the beginning
96 extracted_image_paths.append(f"{path}")
97
98 print(f"Fetched details for {name}")
99 return description, extracted_image_paths, installation_instructions
100
101
102def add_project_page_entry(image, name):
103 # Read the HTML file
104 with open("./pages/projects.html", 'r', encoding='utf-8') as file:
105 soup = BeautifulSoup(file, 'html.parser')
106
107 # Find the container div
108 container = soup.find('div', class_='row tm-mb-90 tm-gallery')
109
110 if container:
111 # Find the last div in the container
112 last_div = container.find_all('div', class_='col-xl-3 col-lg-4 col-md-6 col-sm-6 col-12 mb-5')[-1]
113
114 # Create and insert new divs
115 new_div = BeautifulSoup(f'''
116 <div class="col-xl-3 col-lg-4 col-md-6 col-sm-6 col-12 mb-5">
117 <figure class="tm-video-item">
118 <img src={image} alt="Image" class="img-fluid">
119 <figcaption class="d-flex align-items-center justify-content-center">
120 <a href="./{name.lower()}.html">View more</a>
121 </figcaption>
122 </figure>
123 <div class="d-flex justify-content-between tm-text-gray">
124 <span>{name}</span>
125 </div>
126 </div>
127 ''', 'html.parser')
128 last_div.insert_after(new_div)
129 last_div = new_div
130
131 # Write the modified HTML back to the file
132 with open("./pages/projects.html", 'w', encoding='utf-8') as file:
133 file.write(str(soup))
134
135 print(f"Project Page entry for {name} created")
136
137
139 with open("./builder.conf", "w") as file:
140 file.write(f"# Last updated\n{datetime.datetime.now().strftime('%Y-%m-%d')}")
141 file.close
142
143 print("builder.conf updated")
144
145
146def main():
147 list_to_update = to_update()
148 # list_to_update = ["Gedit"] ## generate single theme(manual run)
149
150 if list_to_update:
151 for addition in list_to_update:
152 info = get_theme_info(addition)
153 create_project_page(addition, info[0], info[1], info[2])
154 print(f"Created page for {addition}")
155
156 if info[1]:
157 image = f"https://raw.githubusercontent.com/BlossomTheme/{addition}/main/{info[1][0]}"
158 add_project_page_entry(image, addition)
159 else:
160 print(f"No images found for {addition}")
161
162 print(f"Successfully added {addition} to web..........\n\n")
163 else:
164 print("Nothing found to be updated")
165
166 # updates the config with the site updated date
168
169
170if __name__ == "__main__":
171 main()
add_project_page_entry(image, name)
Definition builder.py:102
create_project_page(name, description, image_locations, installation_instructions)
Definition builder.py:34
to_update()
Definition builder.py:11
get_theme_info(name)
Definition builder.py:62
update_config()
Definition builder.py:138