Skip to content

Commit

Permalink
Strip https://pbs.twimg.com from image urls
Browse files Browse the repository at this point in the history
  • Loading branch information
zedeus committed Jun 7, 2020
1 parent ea7222b commit 47c87f7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
14 changes: 7 additions & 7 deletions src/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ proc parseProfile(js: JsonNode; id=""): Profile =
fullname: js{"name"}.getStr,
location: js{"location"}.getStr,
bio: js{"description"}.getStr,
userpic: js{"profile_image_url_https"}.getStr.replace("_normal", ""),
userpic: js{"profile_image_url_https"}.getImageStr.replace("_normal", ""),
banner: js.getBanner,
following: $js{"friends_count"}.getInt,
followers: $js{"followers_count"}.getInt,
Expand Down Expand Up @@ -62,7 +62,7 @@ proc parseGraphList*(js: JsonNode): List =
userId: list{"user", "legacy", "id_str"}.getStr,
description: list{"description"}.getStr,
members: list{"member_count"}.getInt,
banner: list{"custom_banner_media", "media_info", "url"}.getStr
banner: list{"custom_banner_media", "media_info", "url"}.getImageStr
)

proc parseListMembers*(js: JsonNode; cursor: string): Result[Profile] =
Expand Down Expand Up @@ -100,15 +100,15 @@ proc parsePoll(js: JsonNode): Poll =
result.votes = result.values.sum

proc parseGif(js: JsonNode): Gif =
Gif(
url: js{"video_info", "variants"}[0]{"url"}.getStr,
thumb: js{"media_url_https"}.getStr
result = Gif(
url: js{"video_info", "variants"}[0]{"url"}.getImageStr,
thumb: js{"media_url_https"}.getImageStr
)

proc parseVideo(js: JsonNode): Video =
result = Video(
videoId: js{"id_str"}.getStr,
thumb: js{"media_url_https"}.getStr,
thumb: js{"media_url_https"}.getImageStr,
views: js{"ext", "mediaStats", "r", "ok", "viewCount"}.getStr,
available: js{"ext_media_availability", "status"}.getStr == "available",
title: js{"ext_alt_text"}.getStr,
Expand Down Expand Up @@ -249,7 +249,7 @@ proc parseTweet(js: JsonNode): Tweet =
for m in jsMedia:
case m{"type"}.getStr
of "photo":
result.photos.add m{"media_url_https"}.getStr
result.photos.add m{"media_url_https"}.getImageStr
of "video":
result.video = some(parseVideo(m))
of "animated_gif":
Expand Down
13 changes: 9 additions & 4 deletions src/parserutils.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import strutils, times, macros, htmlgen, uri, unicode, options
import strutils, times, macros, htmlgen, unicode, options
import regex, packedjson
import types, utils, formatters

Expand Down Expand Up @@ -58,8 +58,13 @@ proc getId*(js: JsonNode): int64 {.inline.} =
template getStrVal*(js: JsonNode; default=""): string =
js{"string_value"}.getStr(default)

template getImageVal*(js: JsonNode; default=""): string =
js{"image_value", "url"}.getStr(default)
proc getImageStr*(js: JsonNode): string =
result = js.getStr
result.removePrefix(https)
result.removePrefix(twimg)

template getImageVal*(js: JsonNode): string =
js{"image_value", "url"}.getImageStr

proc getCardUrl*(js: JsonNode; kind: CardKind): string =
result = js{"website_url"}.getStrVal
Expand All @@ -81,7 +86,7 @@ proc getCardTitle*(js: JsonNode; kind: CardKind): string =
result = js{"event_category"}.getStrVal

proc getBanner*(js: JsonNode): string =
let url = js{"profile_banner_url"}.getStr
let url = js{"profile_banner_url"}.getImageStr
if url.len > 0:
return url & "/1500x500"

Expand Down
13 changes: 8 additions & 5 deletions src/routes/media.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import asynchttpserver, asyncstreams, asyncfile, asyncnet
import jester, regex

import router_utils
import ".."/[types, formatters, agents]
import ".."/[types, formatters, agents, utils]
import ../views/general

export asynchttpserver, asyncstreams, asyncfile, asyncnet
Expand Down Expand Up @@ -66,11 +66,14 @@ proc createMediaRouter*(cfg: Config) =
resp Http404

get "/pic/@url":
cond "http" in @"url"
cond "twimg" in @"url"
var url = decodeUrl(@"url")
if "twimg.com" notin url:
url.insert(twimg)
if not url.startsWith(https):
url.insert(https)

let uri = parseUri(decodeUrl(@"url"))
cond isTwitterUrl($uri) == true
let uri = parseUri(url)
cond isTwitterUrl(uri) == true

enableRawMode()
let code = await proxyMedia(request, $uri)
Expand Down
5 changes: 5 additions & 0 deletions src/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import nimcrypto, regex
var hmacKey = "secretkey"

const
https* = "https://"
twimg* = "pbs.twimg.com/"
badJpgExts = @["1500x500", "jpgn", "jpg:", "jpg_", "_jpg"]
badPngExts = @["pngn", "png:", "png_", "_png"]
twitterDomains = @[
Expand Down Expand Up @@ -43,5 +45,8 @@ proc filterParams*(params: Table): seq[(string, string)] =
let filter = ["name", "id", "list", "referer", "scroll"]
toSeq(params.pairs()).filterIt(it[0] notin filter and it[1].len > 0)

proc isTwitterUrl*(uri: Uri): bool =
uri.hostname in twitterDomains

proc isTwitterUrl*(url: string): bool =
parseUri(url).hostname in twitterDomains
4 changes: 2 additions & 2 deletions tests/test_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_card(self, tweet, title, description, destination, large):
card = Card(Conversation.main + " ")
self.assert_text(title, card.title)
self.assert_text(destination, card.destination)
self.assertIn('twimg', self.get_image_url(card.image + ' img'))
self.assertIn('_img', self.get_image_url(card.image + ' img'))
if len(description) > 0:
self.assert_text(description, card.description)
if large:
Expand All @@ -130,7 +130,7 @@ def test_card_playable(self, tweet, title, description, destination):
card = Card(Conversation.main + " ")
self.assert_text(title, card.title)
self.assert_text(destination, card.destination)
self.assertIn('twimg', self.get_image_url(card.image + ' img'))
self.assertIn('_img', self.get_image_url(card.image + ' img'))
self.assert_element_visible('.card-overlay')
if len(description) > 0:
self.assert_text(description, card.description)
Expand Down

0 comments on commit 47c87f7

Please sign in to comment.