Commit 24e59790d0
Unsigned
Layout: unified · split
lib/awesome-privacy-readme-gen.py added +145
| @@ -0,0 +1,145 @@ | ||
| 1 | """ | |
| 2 | Reads app list from awesome-privacy.yml, | |
| 3 | formats into markdown, and inserts into README.md | |
| 4 | """ | |
| 5 | ||
| 6 | import os | |
| 7 | import re | |
| 8 | import yaml | |
| 9 | import logging | |
| 10 | from urllib.parse import urlparse | |
| 11 | ||
| 12 | # Configure Logging | |
| 13 | LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO").upper() | |
| 14 | logging.basicConfig(level=LOG_LEVEL) | |
| 15 | logger = logging.getLogger(__name__) | |
| 16 | ||
| 17 | # Determine the project root based on the script's location | |
| 18 | project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| 19 | app_list_file_path = os.path.join(project_root, 'awesome-privacy.yml') | |
| 20 | readme_path = os.path.join(project_root, '.github/README.md') | |
| 21 | icon_size=16 | |
| 22 | ||
| 23 | # Read the main YAML file, where all data lives | |
| 24 | logger.info("Reading the awesome-privacy file...") | |
| 25 | with open(app_list_file_path, 'r') as file: | |
| 26 | data = yaml.safe_load(file) | |
| 27 | ||
| 28 | def iconElement(serviceUrl, serviceIcon): | |
| 29 | path = serviceIcon or f"https://icon.horse/icon/{urlparse(serviceUrl).netloc}" | |
| 30 | return f"<img src='{path}' width='{icon_size}' height='{icon_size}' alt='icon' />" | |
| 31 | ||
| 32 | def repoElement(repoUrl): | |
| 33 | if not repoUrl: | |
| 34 | return "" | |
| 35 | return ( | |
| 36 | f"\t- [[1]}" | |
| 38 | "&labelColor=%230d1117&color=%23302982&cacheSeconds=3600)]" | |
| 39 | f"(https://github.com/{repoUrl})\n" | |
| 40 | ) | |
| 41 | ||
| 42 | def makeHref(text): | |
| 43 | if not text: return "#" | |
| 44 | return re.sub(r'[^\w\s-]', '', text.lower()).replace(" ", "-") | |
| 45 | ||
| 46 | def makeContents(): | |
| 47 | contents = "## Contents\n\n" | |
| 48 | for category in data.get('categories'): | |
| 49 | contents += f"- **{category.get('name')}**" | |
| 50 | for section in category.get('sections'): | |
| 51 | contents += ( | |
| 52 | f"\n\t- [{section.get('name')}](#{makeHref(section.get('name'))}) " | |
| 53 | f"({len(section.get('services') or [])})" | |
| 54 | ) | |
| 55 | contents += "\n" | |
| 56 | return contents | |
| 57 | ||
| 58 | def makeAwesomePrivacy(): | |
| 59 | markdown = "" | |
| 60 | for category in data.get('categories'): | |
| 61 | markdown += f"## {category.get('name')}\n\n" | |
| 62 | for section in category.get('sections'): | |
| 63 | markdown += f"### {section.get('name')}\n\n" | |
| 64 | # Add intro | |
| 65 | if section.get('intro'): | |
| 66 | markdown += f"{section.get('intro')}\n" | |
| 67 | # No services yet | |
| 68 | if not section.get('services') or len(section.get('services')) == 0: | |
| 69 | markdown += ( | |
| 70 | "<p align=\"center\">" | |
| 71 | "<b>⚠️ This section is still a work in progress ⚠️</b><br />" | |
| 72 | "<i>Check back soon, or help us complete it by submitting a pull request</i>" | |
| 73 | "</p>" | |
| 74 | ) | |
| 75 | # For each service, list it's name, icon, url, and description | |
| 76 | for app in section.get('services') or []: | |
| 77 | markdown += ( | |
| 78 | f"- **[{iconElement(app.get('url'), app.get('icon'))} {app.get('name')}]" | |
| 79 | f"({app.get('url')})** - {app.get('description')} " | |
| 80 | f"{repoElement(app.get('github'))}" | |
| 81 | ) | |
| 82 | markdown += "\n" | |
| 83 | # If word of warning exists, append it | |
| 84 | if section.get('wordOfWarning'): | |
| 85 | markdown += "<details>\n<summary>⚠️ <b>Word of Warning</b></summary>\n\n" | |
| 86 | markdown += f"> {section.get('wordOfWarning')}\n\n" | |
| 87 | markdown += "</details>\n\n" | |
| 88 | # If notable mentions exists, append it (either as a list or a single string) | |
| 89 | if section.get('notableMentions'): | |
| 90 | markdown += "<details>\n<summary>✳️ <b>Notable Mentions</b></summary>\n\n" | |
| 91 | if isinstance(section.get('notableMentions'), list): | |
| 92 | for mention in section.get('notableMentions'): | |
| 93 | markdown += f"> - [{mention.get('name')}]({mention.get('url')})" + ( | |
| 94 | f" - {mention.get('description')}" if mention.get('description') else "\n" | |
| 95 | ) | |
| 96 | else: | |
| 97 | notable_mentions = section.get('notableMentions').replace('\n', '\n> ') | |
| 98 | markdown += f"> {notable_mentions}" | |
| 99 | ||
| 100 | markdown += "</details>\n\n" | |
| 101 | # If further info exists, append it | |
| 102 | if section.get('furtherInfo'): | |
| 103 | markdown += "<details>\n<summary>ℹ️ <b>Further Info</b></summary>\n\n" | |
| 104 | markdown += f"> {section.get('furtherInfo')}" | |
| 105 | markdown += "</details>\n\n" | |
| 106 | markdown += "<p align=\"right\"><sup><a href=\"#top\">⬆️ [Back to Top]</a></sub></p>\n" | |
| 107 | markdown += "\n---\n\n" | |
| 108 | return markdown | |
| 109 | ||
| 110 | awesome_privacy_results = makeContents() + makeAwesomePrivacy() | |
| 111 | ||
| 112 | # Update the README.md between markers | |
| 113 | logger.info("Reading README.md file...") | |
| 114 | with open(readme_path, 'r') as file: | |
| 115 | readme_content = file.read() | |
| 116 | ||
| 117 | def update_content_between_markers(content, start_marker, end_marker, new_content): | |
| 118 | logger.info(f"Updating content between {start_marker} and {end_marker} markers...") | |
| 119 | start_index = content.find(start_marker) | |
| 120 | end_index = content.find(end_marker) | |
| 121 | ||
| 122 | if start_index != -1 and end_index != -1: | |
| 123 | before_section = content[:start_index + len(start_marker)] | |
| 124 | after_section = content[end_index:] | |
| 125 | updated_content = before_section + '\n' + new_content + after_section | |
| 126 | return updated_content | |
| 127 | else: | |
| 128 | logger.error(f"Markers {start_marker} and {end_marker} not found.") | |
| 129 | return content | |
| 130 | ||
| 131 | # Update guides and resources in README.md | |
| 132 | readme_content = update_content_between_markers( | |
| 133 | readme_content, | |
| 134 | "<!-- awesome-privacy-start -->", | |
| 135 | "<!-- awesome-privacy-end -->", | |
| 136 | awesome_privacy_results | |
| 137 | ) | |
| 138 | ||
| 139 | # Write back the updated content to README.md | |
| 140 | logger.info("Writing back to README.md...") | |
| 141 | with open(readme_path, 'w') as file: | |
| 142 | file.write(readme_content) | |
| 143 | ||
| 144 | # All done. Time to go home for tea and medals. | |
| 145 | logger.info("Script completed successfully!") | |
lib/requirements.txt added +2
| @@ -0,0 +1,2 @@ | ||
| 1 | PyYAML==6.0.1 | |
| 2 | requests==2.31.0 | |