nitter/src/parser.nim

267 lines
8.6 KiB
Nim
Raw Normal View History

2019-08-11 21:26:37 +02:00
import xmltree, sequtils, strutils, json
2019-06-20 16:16:20 +02:00
import types, parserutils, formatters
2019-06-20 16:16:20 +02:00
proc parseTimelineProfile*(node: XmlNode): Profile =
let profile = node.select(".ProfileHeaderCard")
if profile == nil: return
let pre = ".ProfileHeaderCard-"
result = Profile(
fullname: profile.getName(pre & "nameLink"),
username: profile.getUsername(pre & "screenname"),
joinDate: profile.getDate(pre & "joinDateText"),
2019-10-08 23:12:08 +02:00
website: profile.selectAttr(pre & "urlText a", "title"),
bio: profile.getBio(pre & "bio"),
location: getLocation(profile),
userpic: node.getAvatar(".profile-picture img"),
verified: isVerified(profile),
protected: isProtected(profile),
2019-08-11 23:24:02 +02:00
banner: getTimelineBanner(node),
media: getMediaCount(node)
)
result.getProfileStats(node.select(".ProfileNav-list"))
2019-09-13 22:24:58 +02:00
proc parsePopupProfile*(node: XmlNode; selector=".profile-card"): Profile =
let profile = node.select(selector)
2019-06-27 21:07:29 +02:00
if profile == nil: return
2019-06-21 02:15:46 +02:00
result = Profile(
2019-06-24 02:09:32 +02:00
fullname: profile.getName(".fullname"),
username: profile.getUsername(".username"),
2019-09-13 22:24:58 +02:00
bio: profile.getBio(".bio", fallback=".ProfileCard-bio"),
2019-06-24 02:09:32 +02:00
userpic: profile.getAvatar(".ProfileCard-avatarImage"),
verified: isVerified(profile),
protected: isProtected(profile),
banner: getBanner(profile)
2019-06-21 02:15:46 +02:00
)
2019-06-24 08:07:36 +02:00
2019-06-24 01:34:30 +02:00
result.getPopupStats(profile)
2019-06-20 16:16:20 +02:00
2019-09-21 01:08:30 +02:00
proc parseListProfile*(profile: XmlNode): Profile =
result = Profile(
fullname: profile.getName(".fullname"),
username: profile.getUsername(".username"),
bio: profile.getBio(".bio"),
userpic: profile.getAvatar(".avatar"),
verified: isVerified(profile),
protected: isProtected(profile),
)
2019-06-21 02:15:46 +02:00
proc parseIntentProfile*(profile: XmlNode): Profile =
result = Profile(
2019-06-24 02:09:32 +02:00
fullname: profile.getName("a.fn.url.alternate-context"),
username: profile.getUsername(".nickname"),
bio: profile.getBio("p.note"),
2019-06-26 18:51:21 +02:00
userpic: profile.select(".profile.summary").getAvatar("img.photo"),
2019-06-27 21:07:29 +02:00
verified: profile.select("li.verified") != nil,
protected: profile.select("li.protected") != nil,
2019-06-24 02:09:32 +02:00
banner: getBanner(profile)
2019-06-21 02:15:46 +02:00
)
2019-06-24 08:07:36 +02:00
2019-06-24 01:34:30 +02:00
result.getIntentStats(profile)
2019-06-21 02:15:46 +02:00
proc parseTweetProfile*(profile: XmlNode): Profile =
2019-06-20 16:16:20 +02:00
result = Profile(
2019-06-27 21:07:29 +02:00
fullname: profile.attr("data-name").stripText(),
username: profile.attr("data-screen-name"),
2019-06-24 01:34:30 +02:00
userpic: profile.getAvatar(".avatar"),
verified: isVerified(profile)
)
2019-06-24 08:07:36 +02:00
proc parseQuote*(quote: XmlNode): Quote =
result = Quote(
id: parseInt(quote.attr("data-item-id")),
text: getQuoteText(quote),
reply: parseTweetReply(quote),
hasThread: quote.select(".self-thread-context") != nil,
2019-07-04 04:38:23 +02:00
available: true
2019-06-24 01:34:30 +02:00
)
result.profile = Profile(
2019-06-25 02:38:18 +02:00
fullname: quote.selectText(".QuoteTweet-fullname").stripText(),
2019-06-27 21:07:29 +02:00
username: quote.attr("data-screen-name"),
2019-06-24 08:07:36 +02:00
verified: isVerified(quote)
2019-06-20 16:16:20 +02:00
)
2019-06-24 08:07:36 +02:00
result.getQuoteMedia(quote)
2019-06-26 18:51:21 +02:00
proc parseTweet*(node: XmlNode): Tweet =
if node == nil:
return Tweet()
if "withheld" in node.attr("class"):
return Tweet(tombstone: getTombstone(node.selectText(".Tombstone-label")))
2019-06-26 18:51:21 +02:00
let tweet = node.select(".tweet")
if tweet == nil:
2019-08-20 22:44:11 +02:00
return Tweet()
2019-06-26 18:51:21 +02:00
2019-06-21 02:30:57 +02:00
result = Tweet(
id: parseInt(tweet.attr("data-item-id")),
threadId: parseInt(tweet.attr("data-conversation-id")),
2019-06-24 01:34:30 +02:00
text: getTweetText(tweet),
time: getTimestamp(tweet),
shortTime: getShortTime(tweet),
2019-06-27 21:07:29 +02:00
profile: parseTweetProfile(tweet),
2019-07-01 23:48:25 +02:00
stats: parseTweetStats(tweet),
reply: parseTweetReply(tweet),
hasThread: tweet.select(".content > .self-thread-context") != nil,
2019-06-27 21:07:29 +02:00
pinned: "pinned" in tweet.attr("class"),
available: true
2019-06-21 02:30:57 +02:00
)
2019-06-20 16:16:20 +02:00
2019-06-24 01:34:30 +02:00
result.getTweetMedia(tweet)
2019-07-15 13:40:59 +02:00
result.getTweetCard(tweet)
2019-06-20 16:16:20 +02:00
2019-06-21 02:30:57 +02:00
let by = tweet.selectText(".js-retweet-text > a > b")
if by.len > 0:
2019-09-18 20:54:07 +02:00
result.retweet = some Retweet(
2019-07-01 23:22:00 +02:00
by: stripText(by),
id: parseInt(tweet.attr("data-retweet-id"))
2019-09-18 20:54:07 +02:00
)
2019-06-21 02:30:57 +02:00
2019-06-26 18:51:21 +02:00
let quote = tweet.select(".QuoteTweet-innerContainer")
2019-06-27 21:07:29 +02:00
if quote != nil:
2019-09-18 20:54:07 +02:00
result.quote = some parseQuote(quote)
let tombstone = tweet.select(".Tombstone")
if tombstone != nil:
if "unavailable" in tombstone.innerText():
let quote = Quote(tombstone: getTombstone(node.selectText(".Tombstone-label")))
2019-09-18 20:54:07 +02:00
result.quote = some quote
2019-06-24 08:07:36 +02:00
proc parseChain*(nodes: XmlNode): Chain =
2019-06-29 06:31:02 +02:00
if nodes == nil: return
result = Chain()
2019-06-29 06:31:02 +02:00
for n in nodes.filterIt(it.kind != xnText):
let class = n.attr("class").toLower()
2019-08-20 22:44:11 +02:00
if "tombstone" in class or "unavailable" in class or "withheld" in class:
2019-08-23 02:15:25 +02:00
result.content.add Tweet()
2019-07-01 03:13:12 +02:00
elif "morereplies" in class:
result.more = getMoreReplies(n)
else:
2019-08-23 02:15:25 +02:00
result.content.add parseTweet(n)
2019-06-20 16:16:20 +02:00
proc parseConversation*(node: XmlNode; after: string): Conversation =
let tweet = node.select(".permalink-tweet-container")
if tweet == nil:
return Conversation(tweet: parseTweet(node.select(".permalink-tweet-withheld")))
2019-06-24 05:14:14 +02:00
result = Conversation(
tweet: parseTweet(tweet),
before: parseChain(node.select(".in-reply-to .stream-items")),
replies: Result[Chain](
minId: node.selectAttr(".replies-to .stream-container", "data-min-position"),
hasMore: node.select(".stream-footer .has-more-items") != nil,
beginning: after.len == 0
)
2019-06-24 05:14:14 +02:00
)
2019-06-20 16:16:20 +02:00
let showMore = node.selectAttr(".ThreadedConversation-showMoreThreads button",
"data-cursor")
if showMore.len > 0:
result.replies.minId = showMore
result.replies.hasMore = true
2019-06-29 06:31:02 +02:00
let replies = node.select(".replies-to .stream-items")
2019-06-27 21:07:29 +02:00
if replies == nil: return
2019-06-20 16:16:20 +02:00
for i, reply in replies.filterIt(it.kind != xnText):
2019-06-29 06:31:02 +02:00
let class = reply.attr("class").toLower()
let thread = reply.select(".stream-items")
if i == 0 and "self" in class:
result.after = parseChain(thread)
2019-06-29 06:31:02 +02:00
elif "lone" in class:
result.replies.content.add parseChain(reply)
2019-06-29 06:31:02 +02:00
else:
result.replies.content.add parseChain(thread)
2019-06-24 05:14:14 +02:00
proc parseTimeline*(node: XmlNode; after: string): Timeline =
2019-09-19 02:23:22 +02:00
if node == nil: return Timeline()
result = Timeline(
content: parseChain(node.select(".stream > .stream-items")).content,
minId: node.attr("data-min-position"),
maxId: node.attr("data-max-position"),
hasMore: node.select(".has-more-items") != nil,
beginning: after.len == 0
)
proc parseVideo*(node: JsonNode; tweetId: int): Video =
2019-06-29 07:45:36 +02:00
let
track = node{"track"}
cType = track["contentType"].to(string)
pType = track["playbackType"].to(string)
2019-06-25 07:37:44 +02:00
2019-06-29 07:45:36 +02:00
case cType
2019-06-25 07:37:44 +02:00
of "media_entity":
result = Video(
2019-06-29 07:45:36 +02:00
playbackType: if "mp4" in pType: mp4 else: m3u8,
contentId: track["contentId"].to(string),
durationMs: track["durationMs"].to(int),
2019-06-25 07:37:44 +02:00
views: track["viewCount"].to(string),
url: track["playbackUrl"].to(string),
2019-08-19 22:03:00 +02:00
available: track{"mediaAvailability"}["status"].to(string) == "available",
reason: track{"mediaAvailability"}["reason"].to(string))
2019-06-25 07:37:44 +02:00
of "vmap":
result = Video(
2019-06-29 07:45:36 +02:00
playbackType: vmap,
2019-09-17 21:17:03 +02:00
durationMs: track.getOrDefault("durationMs").getInt(0),
2019-08-19 22:03:00 +02:00
url: track["vmapUrl"].to(string),
available: true)
2019-06-25 07:37:44 +02:00
else:
2019-06-29 07:45:36 +02:00
echo "Can't parse video of type ", cType
result.videoId = $tweetId
2019-06-29 07:45:36 +02:00
result.thumb = node["posterImage"].to(string)
2019-06-29 14:11:23 +02:00
proc parsePoll*(node: XmlNode): Poll =
let
choices = node.selectAll(".PollXChoice-choice")
votes = node.selectText(".PollXChoice-footer--total")
result.votes = votes.strip().split(" ")[0]
result.status = node.selectText(".PollXChoice-footer--time")
for choice in choices:
for span in choice.select(".PollXChoice-choice--text").filterIt(it.kind != xnText):
if span.attr("class").len == 0:
result.options.add span.innerText()
elif "progress" in span.attr("class"):
result.values.add parseInt(span.innerText()[0 .. ^2])
var highest = 0
for i, n in result.values:
if n > highest:
highest = n
result.leader = i
2019-07-04 04:18:32 +02:00
proc parsePhotoRail*(node: XmlNode): seq[GalleryPhoto] =
for img in node.selectAll(".tweet-media-img-placeholder"):
result.add GalleryPhoto(
url: img.attr("data-image-url"),
tweetId: img.attr("data-tweet-id"),
color: img.attr("background-color").replace("style: ", "")
2019-07-04 04:18:32 +02:00
)
2019-07-11 19:22:23 +02:00
proc parseCard*(card: var Card; node: XmlNode) =
card.title = node.selectText("h2.TwitterCard-title")
card.text = node.selectText("p.tcu-resetMargin")
card.dest = node.selectText("span.SummaryCard-destination")
2019-07-15 13:40:59 +02:00
if card.url.len == 0:
card.url = node.select("a").attr("href")
let image = node.select(".tcu-imageWrapper img")
2019-07-11 19:22:23 +02:00
if image != nil:
# workaround for issue 11713
2019-09-18 20:54:07 +02:00
card.image = some image.attr("data-src").replace("gname", "g&name")
2019-07-11 19:22:23 +02:00
if card.kind == liveEvent:
card.text = card.title
card.title = node.selectText(".TwitterCard-attribution--category")