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