Commit 3da9abf4d5
Unsigned
Layout: unified · split
lib/awesome-privacy-readme-gen.py +125 −29
| @@ -1,13 +1,13 @@ | |||
| 1 | """ | 1 | """ |
| 2 | Reads app list from awesome-privacy.yml, | 2 | Reads app list from awesome-privacy.yml, |
| 3 | formats into markdown, and inserts into README.md | 3 | formats into markdown, and inserts into README.md |
| 4 | """ | 4 | """ |
| 5 | 5 | ||
| 6 | import os | 6 | import os |
| 7 | import re | 7 | import re |
| 8 | import yaml | 8 | import yaml |
| 9 | import logging | 9 | import logging |
| 10 | from urllib.parse import urlparse | 10 | from urllib.parse import urlparse, quote |
| 11 | 11 | ||
| 12 | # Configure Logging | 12 | # Configure Logging |
| 13 | LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO").upper() | 13 | LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO").upper() |
| @@ -44,14 +44,32 @@ def tosElement(tosdrId): | |||
| 44 | return "" | 44 | return "" |
| 45 | return f"[](https://tosdr.org/en/service/{tosdrId})" | 45 | return f"[](https://tosdr.org/en/service/{tosdrId})" |
| 46 | 46 | ||
| 47 | def statsElement(isOpenSource, isSecurityAudited, isAcceptsCrypto): | 47 | def statsElement(app, categoryName, sectionName): |
| 48 | statsStr = "" | 48 | statsStr = "" |
| 49 | if isOpenSource == True: | 49 | if app.get('openSource') == True: |
| 50 | statsStr += "📦 Open Source " | 50 | github = app.get('github') |
| 51 | if isSecurityAudited == True: | 51 | if github: |
| 52 | statsStr += "🛡️ Security Audited " | 52 | link = f"https://github.com/{github}" |
| 53 | if isAcceptsCrypto == True: | 53 | elif app.get('url'): |
| 54 | statsStr += "💰 Accepts Anonymous Payment " | 54 | link = app.get('url') |
| 55 | else: | ||
| 56 | link = f"https://awesome-privacy.xyz/{slugify(categoryName)}/{slugify(sectionName)}/{slugify(app.get('name'))}" | ||
| 57 | statsStr += ( | ||
| 58 | f"[]({link}) " | ||
| 60 | ) | ||
| 61 | if app.get('securityAudited') == True: | ||
| 62 | statsStr += ( | ||
| 63 | " " | ||
| 67 | ) | ||
| 68 | if app.get('acceptsCrypto') == True: | ||
| 69 | statsStr += ( | ||
| 70 | " " | ||
| 72 | ) | ||
| 55 | return statsStr | 73 | return statsStr |
| 56 | 74 | ||
| 57 | def slugify(title): | 75 | def slugify(title): |
| @@ -63,6 +81,11 @@ def slugify(title): | |||
| 63 | title = title.replace('?', '') | 81 | title = title.replace('?', '') |
| 64 | return title | 82 | return title |
| 65 | 83 | ||
| 84 | def shieldsEncode(text): | ||
| 85 | if not text: return '' | ||
| 86 | text = text.strip().replace('-', '--').replace('_', '__').replace(' ', '_') | ||
| 87 | return quote(text, safe='_-.') | ||
| 88 | |||
| 66 | def awesomePrivacyReport(categoryName, sectionName, serviceName): | 89 | def awesomePrivacyReport(categoryName, sectionName, serviceName): |
| 67 | if not serviceName: | 90 | if not serviceName: |
| 68 | return "" | 91 | return "" |
| @@ -72,12 +95,74 @@ def awesomePrivacyReport(categoryName, sectionName, serviceName): | |||
| 72 | f"(https://awesome-privacy.xyz/{slugify(categoryName)}/{slugify(sectionName)}/{slugify(serviceName)})" | 95 | f"(https://awesome-privacy.xyz/{slugify(categoryName)}/{slugify(sectionName)}/{slugify(serviceName)})" |
| 73 | ) | 96 | ) |
| 74 | 97 | ||
| 75 | def makeStatsCard(): | 98 | def playStoreBadge(name, androidApp): |
| 76 | return ( | 99 | if not androidApp: return "" |
| 77 | f"\t- <details><summary>Stats</summary>\n\n" | 100 | encoded = shieldsEncode(name) |
| 78 | f"" | 101 | return ( |
| 79 | f"\n\n</details>" | 102 | f"[ | 103 | f"?style=flat&logo=android&logoColor=white)]" |
| 104 | f"(https://play.google.com/store/apps/details?id={androidApp}) " | ||
| 105 | ) | ||
| 106 | |||
| 107 | def appStoreBadge(name, iosApp): | ||
| 108 | if not iosApp: return "" | ||
| 109 | encoded = shieldsEncode(name) | ||
| 110 | return ( | ||
| 111 | f"[]" | ||
| 113 | f"({iosApp}) " | ||
| 114 | ) | ||
| 115 | |||
| 116 | def redditBadge(subreddit): | ||
| 117 | if not subreddit or not subreddit.strip(): return "" | ||
| 118 | sub = subreddit.strip() | ||
| 119 | return ( | ||
| 120 | f"[]" | ||
| 122 | f"(https://reddit.com/r/{sub}) " | ||
| 123 | ) | ||
| 124 | |||
| 125 | def discordBadge(name, discordInvite): | ||
| 126 | if not discordInvite or not discordInvite.strip(): return "" | ||
| 127 | invite = discordInvite.strip() | ||
| 128 | encoded = shieldsEncode(name) | ||
| 129 | link = invite if invite.startswith('https://') else f"https://discord.gg/{invite}" | ||
| 130 | return ( | ||
| 131 | f"[]" | ||
| 133 | f"({link}) " | ||
| 134 | ) | ||
| 135 | |||
| 136 | _MD_PATTERNS = [ | ||
| 137 | re.compile(r'\[([^\]]*)\]\([^)]*\)'), # [text](url) — group 1 = visible text | ||
| 138 | re.compile(r'\*\*(.+?)\*\*'), # **bold** | ||
| 139 | re.compile(r'`([^`]+)`'), # `code` | ||
| 140 | re.compile(r'(?<!\*)\*([^*]+)\*(?!\*)'), # *italic* | ||
| 141 | ] | ||
| 142 | |||
| 143 | def truncateMarkdown(text, maxLen=200): | ||
| 144 | """Returns (truncated_text, was_truncated) preserving markdown constructs.""" | ||
| 145 | if len(text) <= maxLen: | ||
| 146 | return text, False | ||
| 147 | |||
| 148 | result = [] | ||
| 149 | visible = 0 | ||
| 150 | i = 0 | ||
| 151 | |||
| 152 | while i < len(text) and visible < maxLen: | ||
| 153 | for pattern in _MD_PATTERNS: | ||
| 154 | m = pattern.match(text, i) | ||
| 155 | if m: | ||
| 156 | result.append(m.group(0)) | ||
| 157 | visible += len(m.group(1)) | ||
| 158 | i = m.end() | ||
| 159 | break | ||
| 160 | else: | ||
| 161 | result.append(text[i]) | ||
| 162 | visible += 1 | ||
| 163 | i += 1 | ||
| 164 | |||
| 165 | return ''.join(result).rstrip(), True | ||
| 81 | 166 | ||
| 82 | def makeHref(text): | 167 | def makeHref(text): |
| 83 | if not text: return "#" | 168 | if not text: return "#" |
| @@ -116,21 +201,32 @@ def makeAwesomePrivacy(): | |||
| 116 | ) | 201 | ) |
| 117 | # For each service, list it's name, icon, url, and description | 202 | # For each service, list it's name, icon, url, and description |
| 118 | for app in section.get('services') or []: | 203 | for app in section.get('services') or []: |
| 204 | description, was_truncated = truncateMarkdown(app.get('description', '')) | ||
| 205 | ap_link = ( | ||
| 206 | f"https://awesome-privacy.xyz/" | ||
| 207 | f"{slugify(category.get('name'))}/{slugify(section.get('name'))}/{slugify(app.get('name'))}" | ||
| 208 | ) | ||
| 209 | ellipsis = f"[…]({ap_link} \"View full {app.get('name')} report\")" if was_truncated else "" | ||
| 119 | markdown += ( | 210 | markdown += ( |
| 120 | f"- **[{iconElement(app.get('url'), app.get('icon'))} {app.get('name')}]" | 211 | f"- **[{iconElement(app.get('url'), app.get('icon'))} {app.get('name')}]" |
| 121 | f"({app.get('url')})** - {app.get('description')}" | 212 | f"({app.get('url')})** - {description}{ellipsis} \n" |
| 122 | f"[…](https://awesome-privacy.xyz/" | ||
| 123 | f"{slugify(category.get('name'))}/{slugify(section.get('name'))}/{slugify(app.get('name'))} \"View full {app.get('name')} report\") \n" | ||
| 124 | + (( | ||
| 125 | f"\t- <details>\n\t\t<summary>Stats</summary>\n\n\t\t" | ||
| 126 | f"{repoElement(app.get('github'))} " | ||
| 127 | f"{tosElement(app.get('tosdrId'))} " | ||
| 128 | f"{awesomePrivacyReport(category.get('name'), section.get('name'), app.get('name'))} \n" | ||
| 129 | f"{statsElement(app.get('openSource'), app.get('securityAudited'), app.get('acceptsCrypto'))}˙ \n" | ||
| 130 | f"\n\t\t</details>\n" | ||
| 131 | ) | ||
| 132 | if app.get('github') or app.get('tosdrId') else '') | ||
| 133 | ) | 213 | ) |
| 214 | badges = ' '.join(filter(None, [ | ||
| 215 | repoElement(app.get('github')), | ||
| 216 | tosElement(app.get('tosdrId')), | ||
| 217 | awesomePrivacyReport(category.get('name'), section.get('name'), app.get('name')), | ||
| 218 | statsElement(app, category.get('name'), section.get('name')).rstrip(), | ||
| 219 | playStoreBadge(app.get('name'), app.get('androidApp')).rstrip(), | ||
| 220 | appStoreBadge(app.get('name'), app.get('iosApp')).rstrip(), | ||
| 221 | redditBadge(app.get('subreddit')).rstrip(), | ||
| 222 | discordBadge(app.get('name'), app.get('discordInvite')).rstrip(), | ||
| 223 | ])) | ||
| 224 | if badges: | ||
| 225 | markdown += ( | ||
| 226 | f"\t- <details>\n\t\t<summary>Stats</summary>\n\n\t\t" | ||
| 227 | f"{badges}ㅤ \n" | ||
| 228 | f"\n\t\t</details>\n" | ||
| 229 | ) | ||
| 134 | markdown += "\n" | 230 | markdown += "\n" |
| 135 | # If word of warning exists, append it | 231 | # If word of warning exists, append it |
| 136 | if section.get('wordOfWarning'): | 232 | if section.get('wordOfWarning'): |
| @@ -145,7 +241,7 @@ def makeAwesomePrivacy(): | |||
| 145 | markdown += f"> - [{mention.get('name')}]({mention.get('url')})" + ( | 241 | markdown += f"> - [{mention.get('name')}]({mention.get('url')})" + ( |
| 146 | f" - {mention.get('description')}" if mention.get('description') else "\n" | 242 | f" - {mention.get('description')}" if mention.get('description') else "\n" |
| 147 | ) | 243 | ) |
| 148 | else: | 244 | else: |
| 149 | notable_mentions = section.get('notableMentions').replace('\n', '\n> ') | 245 | notable_mentions = section.get('notableMentions').replace('\n', '\n> ') |
| 150 | markdown += f"> {notable_mentions}" | 246 | markdown += f"> {notable_mentions}" |
| 151 | 247 | ||
| @@ -170,7 +266,7 @@ def update_content_between_markers(content, start_marker, end_marker, new_conten | |||
| 170 | logger.info(f"Updating content between {start_marker} and {end_marker} markers...") | 266 | logger.info(f"Updating content between {start_marker} and {end_marker} markers...") |
| 171 | start_index = content.find(start_marker) | 267 | start_index = content.find(start_marker) |
| 172 | end_index = content.find(end_marker) | 268 | end_index = content.find(end_marker) |
| 173 | 269 | ||
| 174 | if start_index != -1 and end_index != -1: | 270 | if start_index != -1 and end_index != -1: |
| 175 | before_section = content[:start_index + len(start_marker)] | 271 | before_section = content[:start_index + len(start_marker)] |
| 176 | after_section = content[end_index:] | 272 | after_section = content[end_index:] |