This commit is contained in:
blankie 2022-10-09 10:49:10 +07:00
parent 1a996e7c89
commit 51bf087b30
Signed by: blankie
GPG key ID: CC15FC822C7F61F5

View file

@ -20,8 +20,7 @@
"url-utils.rkt"
"xexpr-utils.rkt")
(provide
page-file)
(provide page-file)
;(module+ test
; (require rackunit)
@ -36,21 +35,18 @@
(define (get-media-html url content-type)
(cond
[(eq? content-type #f) `""]
[(regexp-match? #rx"(?i:^image/)" content-type)
`(img (@ (src ,url)))]
[(regexp-match? #rx"(?i:^image/)" content-type) `(img (@ (src ,url)))]
[(regexp-match? #rx"(?i:^audio/|^application/ogg(;|$))" content-type)
`(audio (@ (src ,url) (controls)))]
[(regexp-match? #rx"(?i:^video/)" content-type)
`(video (@ (src ,url) (controls)))]
`(audio (@ (src ,url) (controls)))]
[(regexp-match? #rx"(?i:^video/)" content-type) `(video (@ (src ,url) (controls)))]
[else `""]))
(define (generate-results-page
#:source-url source-url
#:wikiname wikiname
#:title title
#:media-detail media-detail
#:image-content-type image-content-type
#:license [license #f])
(define (generate-results-page #:source-url source-url
#:wikiname wikiname
#:title title
#:media-detail media-detail
#:image-content-type image-content-type
#:license [license #f])
(define video-embed-code (jp "/videoEmbedCode" media-detail ""))
(define raw-image-url (jp "/rawImageUrl" media-detail))
(define image-url (jp "/imageUrl" media-detail raw-image-url))
@ -59,9 +55,8 @@
(define smaller-article-list (jp "/smallerArticleList" media-detail))
(define article-list-is-smaller (jp "/articleListIsSmaller" media-detail))
(define image-description (jp "/imageDescription" media-detail #f))
(define maybe-proxied-raw-image-url (if (config-true? 'strict_proxy)
(u-proxy-url raw-image-url)
raw-image-url))
(define maybe-proxied-raw-image-url
(if (config-true? 'strict_proxy) (u-proxy-url raw-image-url) raw-image-url))
(generate-wiki-page
#:source-url source-url
#:wikiname wikiname
@ -69,35 +64,27 @@
#:license license
`(div
,(if (non-empty-string? video-embed-code)
(update-tree-wiki (html->xexp (preprocess-html-wiki video-embed-code)) wikiname)
(get-media-html image-url image-content-type))
(p
,(if (non-empty-string? video-embed-code)
`""
`(span (a (@ (href ,maybe-proxied-raw-image-url)) "View original file") ". "))
"Added by "
(a (@ (href ,(format "/~a/wiki/User:~a" wikiname username))) ,username)
"."
,(if is-posted-in
`(span " Posted in "
,@(map
(λ (article)
(define page-path (jp "/title" article))
(define title (jp "/titleText" article page-path))
`(span
,(if (eq? (car smaller-article-list) article)
""
", ")
(a (@ (href ,(format "/~a/wiki/~a" wikiname page-path)))
,title)))
smaller-article-list)
,(if (eq? article-list-is-smaller 1)
""
"."))
`""))
(update-tree-wiki (html->xexp (preprocess-html-wiki video-embed-code)) wikiname)
(get-media-html image-url image-content-type))
(p ,(if (non-empty-string? video-embed-code)
`""
`(span (a (@ (href ,maybe-proxied-raw-image-url)) "View original file") ". "))
"Added by "
(a (@ (href ,(format "/~a/wiki/User:~a" wikiname username))) ,username)
"."
,(if is-posted-in
`(span " Posted in "
,@(map (λ (article)
(define page-path (jp "/title" article))
(define title (jp "/titleText" article page-path))
`(span ,(if (eq? (car smaller-article-list) article) "" ", ")
(a (@ (href ,(format "/~a/wiki/~a" wikiname page-path))) ,title)))
smaller-article-list)
,(if (eq? article-list-is-smaller 1) "" "."))
`""))
,(if (string? image-description)
(update-tree-wiki (html->xexp (preprocess-html-wiki image-description)) wikiname)
""))))
(update-tree-wiki (html->xexp (preprocess-html-wiki image-description)) wikiname)
""))))
(define (page-file req)
(define wikiname (path/param-path (first (url-path (request-uri req)))))
@ -105,45 +92,40 @@
(define origin (format "https://~a.fandom.com" wikiname))
(define source-url (format "~a/wiki/~a" origin prefixed-title))
(thread-let
([media-detail (define dest-url
(format "~a/wikia.php?~a"
origin
(params->query `(("format" . "json")
("controller" . "Lightbox")
("method" . "getMediaDetail")
("fileTitle" . ,prefixed-title)))))
(log-outgoing dest-url)
(define dest-res (easy:get dest-url #:timeouts timeouts))
(easy:response-json dest-res)]
[license (license-auto wikiname)])
(if (not (jp "/exists" media-detail #f))
(next-dispatcher)
(response-handler
(define file-title (jp "/fileTitle" media-detail ""))
(define title (if (non-empty-string? file-title)
(format "File:~a" file-title)
prefixed-title))
(define image-content-type (if (non-empty-string? (jp "/videoEmbedCode" media-detail ""))
#f
(url-content-type (jp "/imageUrl" media-detail))))
(define body (generate-results-page
#:source-url source-url
#:wikiname wikiname
#:title title
#:media-detail media-detail
#:image-content-type image-content-type
#:license license))
(when (config-true? 'debug)
; used for its side effects
; convert to string with error checking, error will be raised if xexp is invalid
(xexp->html body))
(response/output
#:code 200
(λ (out)
(write-html body out)))))))
(thread-let ([media-detail
(define dest-url
(format "~a/wikia.php?~a"
origin
(params->query `(("format" . "json") ("controller" . "Lightbox")
("method" . "getMediaDetail")
("fileTitle" . ,prefixed-title)))))
(log-outgoing dest-url)
(define dest-res (easy:get dest-url #:timeouts timeouts))
(easy:response-json dest-res)]
[license (license-auto wikiname)])
(if (not (jp "/exists" media-detail #f))
(next-dispatcher)
(response-handler
(define file-title (jp "/fileTitle" media-detail ""))
(define title
(if (non-empty-string? file-title) (format "File:~a" file-title) prefixed-title))
(define image-content-type
(if (non-empty-string? (jp "/videoEmbedCode" media-detail ""))
#f
(url-content-type (jp "/imageUrl" media-detail))))
(define body
(generate-results-page #:source-url source-url
#:wikiname wikiname
#:title title
#:media-detail media-detail
#:image-content-type image-content-type
#:license license))
(when (config-true? 'debug)
; used for its side effects
; convert to string with error checking, error will be raised if xexp is invalid
(xexp->html body))
(response/output #:code 200
(λ (out) (write-html body out)))))))
;(module+ test
; (check-not-false ((query-selector (attribute-selector 'href "/test/wiki/Ankle_Monitor")
; (generate-results-page