nitter/src/parser.nim

137 lines
4.1 KiB
Nim
Raw Normal View History

2019-06-24 04:14:14 +01:00
import xmltree, sequtils, strtabs, strutils, strformat, json
2019-06-20 15:16:20 +01:00
2019-06-24 07:07:36 +01:00
import ./types, ./parserutils, ./formatters
2019-06-20 15:16:20 +01:00
2019-06-21 01:15:46 +01:00
proc parsePopupProfile*(node: XmlNode): Profile =
2019-06-26 17:51:21 +01:00
let profile = node.select(".profile-card")
2019-06-27 20:07:29 +01:00
if profile == nil: return
2019-06-21 01:15:46 +01:00
result = Profile(
2019-06-24 01:09:32 +01:00
fullname: profile.getName(".fullname"),
username: profile.getUsername(".username"),
bio: profile.getBio(".bio"),
userpic: profile.getAvatar(".ProfileCard-avatarImage"),
verified: isVerified(profile),
protected: isProtected(profile),
banner: getBanner(profile)
2019-06-21 01:15:46 +01:00
)
2019-06-24 07:07:36 +01:00
2019-06-24 00:34:30 +01:00
result.getPopupStats(profile)
2019-06-20 15:16:20 +01:00
2019-06-21 01:15:46 +01:00
proc parseIntentProfile*(profile: XmlNode): Profile =
result = Profile(
2019-06-24 01:09:32 +01:00
fullname: profile.getName("a.fn.url.alternate-context"),
username: profile.getUsername(".nickname"),
bio: profile.getBio("p.note"),
2019-06-26 17:51:21 +01:00
userpic: profile.select(".profile.summary").getAvatar("img.photo"),
2019-06-27 20:07:29 +01:00
verified: profile.select("li.verified") != nil,
protected: profile.select("li.protected") != nil,
2019-06-24 01:09:32 +01:00
banner: getBanner(profile)
2019-06-21 01:15:46 +01:00
)
2019-06-24 07:07:36 +01:00
2019-06-24 00:34:30 +01:00
result.getIntentStats(profile)
2019-06-21 01:15:46 +01:00
proc parseTweetProfile*(profile: XmlNode): Profile =
2019-06-20 15:16:20 +01:00
result = Profile(
2019-06-27 20:07:29 +01:00
fullname: profile.attr("data-name").stripText(),
username: profile.attr("data-screen-name"),
2019-06-24 00:34:30 +01:00
userpic: profile.getAvatar(".avatar"),
verified: isVerified(profile)
)
2019-06-24 07:07:36 +01:00
proc parseQuote*(quote: XmlNode): Quote =
result = Quote(
2019-06-27 20:07:29 +01:00
id: quote.attr("data-item-id"),
link: quote.attr("href"),
2019-06-25 03:52:38 +01:00
text: getQuoteText(quote)
2019-06-24 00:34:30 +01:00
)
result.profile = Profile(
2019-06-25 01:38:18 +01:00
fullname: quote.selectText(".QuoteTweet-fullname").stripText(),
2019-06-27 20:07:29 +01:00
username: quote.attr("data-screen-name"),
2019-06-24 07:07:36 +01:00
verified: isVerified(quote)
2019-06-20 15:16:20 +01:00
)
2019-06-24 07:07:36 +01:00
result.getQuoteMedia(quote)
2019-06-26 17:51:21 +01:00
proc parseTweet*(node: XmlNode): Tweet =
let tweet = node.select(".tweet")
2019-06-27 20:07:29 +01:00
if tweet == nil: return Tweet()
2019-06-26 17:51:21 +01:00
2019-06-21 01:30:57 +01:00
result = Tweet(
2019-06-27 20:07:29 +01:00
id: tweet.attr("data-item-id"),
link: tweet.attr("data-permalink-path"),
2019-06-24 00:34:30 +01:00
text: getTweetText(tweet),
time: getTimestamp(tweet),
shortTime: getShortTime(tweet),
2019-06-27 20:07:29 +01:00
profile: parseTweetProfile(tweet),
pinned: "pinned" in tweet.attr("class"),
available: true
2019-06-21 01:30:57 +01:00
)
2019-06-20 15:16:20 +01:00
2019-06-24 00:34:30 +01:00
result.getTweetStats(tweet)
result.getTweetMedia(tweet)
2019-06-20 15:16:20 +01:00
2019-06-21 01:30:57 +01:00
let by = tweet.selectText(".js-retweet-text > a > b")
if by.len > 0:
2019-06-25 01:38:18 +01:00
result.retweetBy = some(by.stripText())
2019-06-27 20:07:29 +01:00
result.retweetId = some(tweet.attr("data-retweet-id"))
2019-06-21 01:30:57 +01:00
2019-06-26 17:51:21 +01:00
let quote = tweet.select(".QuoteTweet-innerContainer")
2019-06-27 20:07:29 +01:00
if quote != nil:
2019-06-24 07:07:36 +01:00
result.quote = some(parseQuote(quote))
2019-06-29 05:31:02 +01:00
proc parseTweets*(nodes: XmlNode): Tweets =
if nodes == nil: return
for n in nodes.filterIt(it.kind != xnText):
let class = n.attr("class").toLower()
if "tombstone" in class or "unavailable" in class:
result.add Tweet()
elif "morereplies" notin class:
result.add parseTweet(n)
2019-06-20 15:16:20 +01:00
proc parseConversation*(node: XmlNode): Conversation =
2019-06-24 04:14:14 +01:00
result = Conversation(
2019-06-26 17:51:21 +01:00
tweet: parseTweet(node.select(".permalink-tweet-container")),
2019-06-29 05:31:02 +01:00
before: parseTweets(node.select(".in-reply-to .stream-items"))
2019-06-24 04:14:14 +01:00
)
2019-06-20 15:16:20 +01:00
2019-06-29 05:31:02 +01:00
let replies = node.select(".replies-to .stream-items")
2019-06-27 20:07:29 +01:00
if replies == nil: return
2019-06-20 15:16:20 +01:00
2019-06-26 18:59:28 +01:00
for reply in replies.filterIt(it.kind != xnText):
2019-06-29 05:31:02 +01:00
let class = reply.attr("class").toLower()
let thread = reply.select(".stream-items")
if "self" in class:
result.after = parseTweets(thread)
elif "lone" in class:
2019-06-26 18:59:28 +01:00
result.replies.add parseTweets(reply)
2019-06-29 05:31:02 +01:00
else:
result.replies.add parseTweets(thread)
2019-06-24 04:14:14 +01:00
proc parseVideo*(node: JsonNode): Video =
let track = node{"track"}
2019-06-25 06:37:44 +01:00
let contentType = track["contentType"].to(string)
case contentType
of "media_entity":
result = Video(
contentType: m3u8,
thumb: node["posterImage"].to(string),
id: track["contentId"].to(string),
length: track["durationMs"].to(int),
views: track["viewCount"].to(string),
url: track["playbackUrl"].to(string),
available: track{"mediaAvailability"}["status"].to(string) == "available"
)
of "vmap":
result = Video(
contentType: vmap,
thumb: node["posterImage"].to(string),
url: track["vmapUrl"].to(string),
length: track["durationMs"].to(int),
)
else:
echo "Can't parse video of type ", contentType