| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Twitter article utilities""" |
| 10 |
|
| 11 |
from ... import text |
| 12 |
|
| 13 |
|
| 14 |
def to_document(article, content=None): |
| 15 |
title = text.escape(article["title"]) |
| 16 |
cover = article["cover_media"]["media_info"] |
| 17 |
|
| 18 |
html = [f"""\ |
| 19 |
<!DOCTYPE html> |
| 20 |
<html dir="ltr" lang="en"> |
| 21 |
<head> |
| 22 |
<meta charset="utf-8"> |
| 23 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 24 |
<title>{title}</title> |
| 25 |
<style>{STYLESHEET} </style> |
| 26 |
<link rel="stylesheet" href="style.css"> |
| 27 |
</head> |
| 28 |
<body> |
| 29 |
<img src="{text.escape(media_url(cover["original_img_url"]))}" \ |
| 30 |
class="cover" alt="{title}"> |
| 31 |
<h1>{title}</h1> |
| 32 |
|
| 33 |
"""] |
| 34 |
html.extend(content or to_html(article)) |
| 35 |
html.append("""\ |
| 36 |
</body> |
| 37 |
</html> |
| 38 |
""") |
| 39 |
|
| 40 |
return html |
| 41 |
|
| 42 |
|
| 43 |
def to_html(article): |
| 44 |
content = article["content_state"] |
| 45 |
entities = { |
| 46 |
entity["key"]: entity["value"] |
| 47 |
for entity in content["entityMap"] |
| 48 |
} |
| 49 |
media = { |
| 50 |
entity["media_id"]: entity["media_info"] |
| 51 |
for entity in article["media_entities"] |
| 52 |
} |
| 53 |
|
| 54 |
html = [] |
| 55 |
index = 0 |
| 56 |
blocks = content["blocks"] |
| 57 |
blocks_len = len(blocks) |
| 58 |
while index < blocks_len: |
| 59 |
index = process_block(html, blocks, index, entities, media) |
| 60 |
return html |
| 61 |
|
| 62 |
|
| 63 |
def process_block(html, blocks, i, entities, media): |
| 64 |
block = blocks[i] |
| 65 |
type = block["type"] |
| 66 |
|
| 67 |
html.append(f'<section class="{type}" id="{block["key"]}">\n') |
| 68 |
if type == "unstyled": |
| 69 |
if ranges := block.get("entityRanges"): |
| 70 |
for range in ranges: |
| 71 |
process_entity(html, block, entities[str(range["key"])], media) |
| 72 |
process_text(html, block) |
| 73 |
html.append("\n") |
| 74 |
|
| 75 |
elif type == "atomic": |
| 76 |
if ranges := block.get("entityRanges"): |
| 77 |
for range in ranges: |
| 78 |
process_entity(html, block, entities[str(range["key"])], media) |
| 79 |
|
| 80 |
elif type.endswith("ordered-list-item"): |
| 81 |
html.append(f"<{type[0]}l>\n") |
| 82 |
while True: |
| 83 |
html.append(" <li>") |
| 84 |
process_text(html, block) |
| 85 |
html.append("</li>\n") |
| 86 |
|
| 87 |
try: |
| 88 |
block = blocks[i+1] |
| 89 |
if type != block["type"]: |
| 90 |
break |
| 91 |
|
| 92 |
except Exception: |
| 93 |
break |
| 94 |
|
| 95 |
i += 1 |
| 96 |
html.append(f"</{type[0]}l>\n") |
| 97 |
|
| 98 |
else: |
| 99 |
import logging |
| 100 |
logging.getLogger("twitter.article").warning( |
| 101 |
"Unsupported block type %r", type) |
| 102 |
|
| 103 |
html.append("</section>\n\n") |
| 104 |
return i + 1 |
| 105 |
|
| 106 |
|
| 107 |
def process_entity(html, block, entity, media): |
| 108 |
if entity is None: |
| 109 |
return |
| 110 |
type = entity["type"] |
| 111 |
|
| 112 |
if type == "MEDIA": |
| 113 |
data = entity["data"] |
| 114 |
medias = [media[m["mediaId"]] for m in data["mediaItems"]] |
| 115 |
for img in medias: |
| 116 |
src = text.escape(media_url(img["original_img_url"])) |
| 117 |
html.append(f'<img src="{src}">\n') |
| 118 |
if caption := data.get("caption"): |
| 119 |
html.append( |
| 120 |
f'<span class="caption">{text.escape(caption)}</span>\n') |
| 121 |
|
| 122 |
elif type == "DIVIDER": |
| 123 |
html.append("<hr>\n") |
| 124 |
|
| 125 |
elif type == "TWEMOJI": |
| 126 |
return |
| 127 |
|
| 128 |
txt = block["text"] |
| 129 |
r = block["entityRanges"][0] |
| 130 |
li = r["length"] |
| 131 |
ri = li + r["offset"] |
| 132 |
block["text"] = \ |
| 133 |
f'{txt[:li]}<span class="emoji">{txt[li:ri]}</span>{txt[ri:]}' |
| 134 |
|
| 135 |
elif type == "LATEX": |
| 136 |
html.append(f"""\ |
| 137 |
<math xmlns="http://www.w3.org/1998/Math/MathML"> |
| 138 |
<semantics> |
| 139 |
<annotation encoding="application/x-tex">\ |
| 140 |
{text.escape(block["text"])}</annotation> |
| 141 |
</semantics> |
| 142 |
</math> |
| 143 |
""") |
| 144 |
|
| 145 |
else: |
| 146 |
import logging |
| 147 |
logging.getLogger("twitter.article").warning( |
| 148 |
"Unsupported entity type %r", type) |
| 149 |
|
| 150 |
|
| 151 |
def process_text(html, block): |
| 152 |
txt = block["text"] |
| 153 |
if data := block.get("data"): |
| 154 |
if mentions := data.get("mentions"): |
| 155 |
for r in mentions: |
| 156 |
u = text.unescape(r["text"]) |
| 157 |
txt = (f'{txt[:r["fromIndex"]]}' |
| 158 |
f'<a href="https://x.com/@{u}">@{u}</a>' |
| 159 |
f'{txt[r["toIndex"]:]}') |
| 160 |
if urls := data.get("urls"): |
| 161 |
for r in urls: |
| 162 |
u = text.unescape(r["text"]) |
| 163 |
txt = (f'{txt[:r["fromIndex"]]}' |
| 164 |
f'<a href="{u}">{u}</a>' |
| 165 |
f'{txt[r["toIndex"]:]}') |
| 166 |
if ranges := block.get("inlineStyleRanges"): |
| 167 |
for r in ranges: |
| 168 |
el = r["style"][0].lower() |
| 169 |
li = r["length"] |
| 170 |
ri = li + r["offset"] |
| 171 |
txt = f'{txt[:li]}<{el}>{txt[li:ri]}</{el}>{txt[ri:]}' |
| 172 |
html.append(txt) |
| 173 |
|
| 174 |
|
| 175 |
def media_url(url, name="orig"): |
| 176 |
if url[-4] == ".": |
| 177 |
base, _, fmt = url.rpartition(".") |
| 178 |
return f"{base}?format={fmt}&name={name}" |
| 179 |
return f"{url.rpartition('=')[0]}={name}" |
| 180 |
|
| 181 |
|
| 182 |
LIST = None |
| 183 |
STYLESHEET = """ |
| 184 |
body{ |
| 185 |
width: 598px; |
| 186 |
border: 1px solid rgb(47, 51, 54); |
| 187 |
margin: 0 auto; |
| 188 |
padding: 1em; |
| 189 |
font-family:"TwitterChirpExtendedHeavy","Verdana",-apple-system,\ |
| 190 |
BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif; |
| 191 |
} |
| 192 |
body > section{ |
| 193 |
text-align: center; |
| 194 |
font-size: 17px; |
| 195 |
line-height: 28px; |
| 196 |
margin-bottom: 28px; |
| 197 |
} |
| 198 |
body > section.unstyled{ |
| 199 |
text-align: left; |
| 200 |
} |
| 201 |
ul, ol { |
| 202 |
list-style-position: outside; |
| 203 |
margin-bottom: 28px; |
| 204 |
margin-top: 0; |
| 205 |
padding: 0; |
| 206 |
overflow-wrap: break-word; |
| 207 |
text-align: left; |
| 208 |
} |
| 209 |
li { |
| 210 |
margin-bottom: 12px; |
| 211 |
margin-left: 30px; |
| 212 |
} |
| 213 |
.caption{ |
| 214 |
display: block; |
| 215 |
font-size: 13px; |
| 216 |
text-align: left; |
| 217 |
color: rgb(145, 150, 154); |
| 218 |
} |
| 219 |
.emoji{padding: 0.15em;} |
| 220 |
.cover{width: 100%;} |
| 221 |
img{max-width: 100%;} |
| 222 |
h1{font-size: 34px;} |
| 223 |
a{color: rgb(231, 233, 234);} |
| 224 |
a:hover{color: revert;} |
| 225 |
math{display="block";} |
| 226 |
""" |