Skip to content

Commit

Permalink
fix photo rail
Browse files Browse the repository at this point in the history
  • Loading branch information
KTachibanaM committed Oct 5, 2024
1 parent ad8c3ca commit 34b4a1b
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 44 deletions.
20 changes: 20 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/debian
{
"name": "nitter",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/base:bullseye",
"postCreateCommand": ".devcontainer/init.sh"

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
7 changes: 7 additions & 0 deletions .devcontainer/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

sudo apt update
sudo apt install -y redis libsass-dev

curl https://nim-lang.org/choosenim/init.sh -sSf | sh
echo 'export PATH=/home/vscode/.nimble/bin:$PATH' >> ~/.bashrc
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ this: `journalctl -u nitter.service` (add `--follow` to see just the last 15
lines). If you're running the Docker image, you can do this:
`docker logs --follow *nitter container id*`

### Development in devcontainer
```
redis-server & # start redis server in the background
nimble scss # compile scss
nimble md # compile doc
nimble run # access on localhost:8080
```

## Contact

Feel free to join our [Matrix channel](https://matrix.to/#/#nitter:matrix.org).
Expand Down
23 changes: 16 additions & 7 deletions src/api.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only
import options
import asyncdispatch, httpclient, uri, strutils, sequtils, sugar
import packedjson
import types, query, formatters, consts, apiutils, parser
Expand Down Expand Up @@ -136,13 +137,21 @@ proc getGraphUserSearch*(query: Query; after=""): Future[Result[User]] {.async.}
result = parseGraphSearch[User](await fetch(url, Api.search), after)
result.query = query

proc getPhotoRail*(name: string): Future[PhotoRail] {.async.} =
if name.len == 0: return
let
ps = genParams({"screen_name": name, "trim_user": "true"},
count="18", ext=false)
url = photoRail ? ps
result = parsePhotoRail(await fetch(url, Api.photoRail))
proc getPhotoRail*(id: string): Future[PhotoRail] {.async.} =
if id.len == 0: return
let mediaTweets = await getGraphUserTweets(id, TimelineKind.media)

result = @[]
for thread in mediaTweets.tweets.content:
let
t = thread[0]
url = if t.photos.len > 0: t.photos[0]
elif t.video.isSome: get(t.video).thumb
elif t.gif.isSome: get(t.gif).thumb
elif t.card.isSome: get(t.card).image
else: ""
if url.len == 0: continue
result.add GalleryPhoto(url: url, tweetId: $t.id)

proc resolve*(url: string; prefs: Prefs): Future[string] {.async.} =
let client = newAsyncHttpClient(maxRedirects=0)
Expand Down
19 changes: 0 additions & 19 deletions src/apiutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,6 @@ const

var pool: HttpPool

proc genParams*(pars: openArray[(string, string)] = @[]; cursor="";
count="20"; ext=true): seq[(string, string)] =
result = timelineParams
for p in pars:
result &= p
if ext:
result &= ("include_ext_alt_text", "1")
result &= ("include_ext_media_stats", "1")
result &= ("include_ext_media_availability", "1")
if count.len > 0:
result &= ("count", count)
if cursor.len > 0:
# The raw cursor often has plus signs, which sometimes get turned into spaces,
# so we need to turn them back into a plus
if " " in cursor:
result &= ("cursor", cursor.replace(" ", "+"))
else:
result &= ("cursor", cursor)

proc getOauthHeader(url, oauthToken, oauthTokenSecret: string): string =
let
encodedUrl = url.replace(",", "%2C").replace("+", "%20")
Expand Down
17 changes: 0 additions & 17 deletions src/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -289,23 +289,6 @@ proc parseTweet(js: JsonNode; jsCard: JsonNode = newJNull()): Tweet =
result.text.removeSuffix(" Learn more.")
result.available = false

proc parsePhotoRail*(js: JsonNode): PhotoRail =
with error, js{"error"}:
if error.getStr == "Not authorized.":
return

for tweet in js:
let
t = parseTweet(tweet, js{"tweet_card"})
url = if t.photos.len > 0: t.photos[0]
elif t.video.isSome: get(t.video).thumb
elif t.gif.isSome: get(t.gif).thumb
elif t.card.isSome: get(t.card).image
else: ""

if url.len == 0: continue
result.add GalleryPhoto(url: url, tweetId: $t.id)

proc parseGraphTweet(js: JsonNode; isLegacy=false): Tweet =
if js.kind == JNull:
return Tweet()
Expand Down
3 changes: 2 additions & 1 deletion src/redis_cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ proc getCachedPhotoRail*(name: string): Future[PhotoRail] {.async.} =
if rail != redisNil:
rail.deserialize(PhotoRail)
else:
result = await getPhotoRail(name)
let userId = await getUserId(name)
result = await getPhotoRail(userId)
await cache(result, name)

proc getCachedList*(username=""; slug=""; id=""): Future[List] {.async.} =
Expand Down

0 comments on commit 34b4a1b

Please sign in to comment.