mirror of
https://git.vern.cc/cobra/NerdsforNerds.git
synced 2026-10-23 05:54:46 +05:30
nuke commit history
This commit is contained in:
commit
7804fb9784
3 changed files with 904 additions and 0 deletions
125
nerd/pages.scm
Normal file
125
nerd/pages.scm
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
;; Copyright (C) 2024 Skylar Astaroth <cobra@vern.cc>
|
||||
;;
|
||||
;; This file is part of NerdsforNerds
|
||||
;;
|
||||
;; NerdsforNerds is free software: you can redistribute it and/or modify it
|
||||
;; under the terms of the GNU Affero General Public License as published by the
|
||||
;; Free Software Foundation, either version 3 of the License, or (at your
|
||||
;; option) any later version.
|
||||
;;
|
||||
;; This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
||||
;; for more details.
|
||||
;;
|
||||
;; You should have received a copy of the GNU Affero General Public License
|
||||
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
(define-module (nerd pages)
|
||||
#:use-module (nerd templates)
|
||||
#:use-module (htmlprag)
|
||||
#:use-module (web response)
|
||||
#:use-module (web client)
|
||||
#:use-module (web http)
|
||||
#:use-module (web uri)
|
||||
#:use-module (ice-9 receive)
|
||||
#:use-module (ice-9 string-fun)
|
||||
#:export (error-page
|
||||
index-page
|
||||
article-page
|
||||
proxy-page))
|
||||
|
||||
(define good-response
|
||||
(build-response #:code 200
|
||||
#:headers `((content-type . (text/html)))))
|
||||
|
||||
(define (redirect response code)
|
||||
(values (build-response #:code code
|
||||
#:headers `((location . ,(parse-header
|
||||
'location
|
||||
(uri-path
|
||||
(response-location
|
||||
response))))))
|
||||
"Redirect"))
|
||||
|
||||
(define (error-page code)
|
||||
(display ";\tHandler: error-page") (newline)
|
||||
(values (build-response
|
||||
#:code code
|
||||
#:headers `((content-type . (text/html))))
|
||||
(shtml->html (error-template code))))
|
||||
|
||||
(define (generic-page procedure path)
|
||||
(let ((resp "")
|
||||
(body ""))
|
||||
(receive (_resp _body)
|
||||
(http-request (string-append "https://www.geeksforgeeks.org" path))
|
||||
(set! resp _resp)
|
||||
(set! body _body))
|
||||
(display (string-append ";\tStatus: "
|
||||
(number->string (response-code resp))))
|
||||
(newline)
|
||||
(cond ((equal? (response-code resp) 200)
|
||||
(let ((doc (html->sxml-0nf body #:strict? #t)))
|
||||
(values good-response
|
||||
(string-replace-substring
|
||||
(string-replace-substring
|
||||
(string-replace-substring
|
||||
(shtml->html (procedure doc))
|
||||
"&" "&")
|
||||
"https://media.geeksforgeeks.org/"
|
||||
"/proxy?url=https://media.geeksforgeeks.org/")
|
||||
"https://www.geeksforgeeks.org" ""))))
|
||||
((and (>= (response-code resp) 300) (<= (response-code resp) 399))
|
||||
(redirect resp (response-code resp)))
|
||||
(else (error-page (response-code resp))))))
|
||||
|
||||
(define (index-page host)
|
||||
(display ";\tHandler: index-page") (newline)
|
||||
(values good-response (shtml->html (index-template host))))
|
||||
|
||||
(define (article-page path)
|
||||
(display ";\tHandler: article-page")
|
||||
(generic-page article-template path))
|
||||
|
||||
(define (proxy-page query)
|
||||
(display ";\tHandler: proxy-page") (newline)
|
||||
(let ((resp "")
|
||||
(body "")
|
||||
(url ""))
|
||||
(map (lambda (p)
|
||||
(if (equal? (car p) "url")
|
||||
(set! url (cadr p))))
|
||||
(map (lambda (s)
|
||||
(string-split s #\=))
|
||||
(string-split query #\&)))
|
||||
(if (and
|
||||
(>= (string-length url) 32)
|
||||
(equal? (substring url 0 32) "https://media.geeksforgeeks.org/"))
|
||||
(begin
|
||||
(receive (_resp _body) (http-request url)
|
||||
(set! resp _resp)
|
||||
(set! body _body))
|
||||
(cond
|
||||
((equal? (response-code resp) 200)
|
||||
(values (build-response
|
||||
#:code 200
|
||||
#:headers `((content-type .
|
||||
,(response-content-type resp))
|
||||
(cache-control .
|
||||
,(parse-header 'cache-control
|
||||
"max-age=604800"))))
|
||||
body))
|
||||
((and (>= (response-code resp) 300) (<= (response-code resp) 399))
|
||||
(values (build-response
|
||||
#:code (response-code resp)
|
||||
#:headers `((location . ,(parse-header
|
||||
'location
|
||||
(uri-path
|
||||
(response-location
|
||||
resp))))
|
||||
(content-type .
|
||||
,(response-content-type resp))))
|
||||
"Redirect"))
|
||||
(else (error-page 404))))
|
||||
(error-page 400))))
|
||||
45
nerd/scraping.scm
Normal file
45
nerd/scraping.scm
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
;; Copyright (C) 2024 Skylar Astaroth <cobra@vern.cc>
|
||||
;;
|
||||
;; This file is part of NerdsforNerds
|
||||
;;
|
||||
;; NerdsforNerds is free software: you can redistribute it and/or modify it
|
||||
;; under the terms of the GNU Affero General Public License as published by the
|
||||
;; Free Software Foundation, either version 3 of the License, or (at your
|
||||
;; option) any later version.
|
||||
;;
|
||||
;; This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
||||
;; for more details.
|
||||
;;
|
||||
;; You should have received a copy of the GNU Affero General Public License
|
||||
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
(define-module (nerd scraping)
|
||||
#:use-module (sxml simple)
|
||||
#:use-module (sxml xpath)
|
||||
#:export (get-article))
|
||||
|
||||
(define (get-article d)
|
||||
(append
|
||||
(list ((sxpath '(// article div div div h1 *any*)) d))
|
||||
(cdr ((sxpath '(// article div div div div span *text*)) d))
|
||||
((sxpath '(html body div div div div a @ href *text*)) d)
|
||||
((sxpath '(html body div div div div a div span *text*)) d)
|
||||
((sxpath '(html body div div div div a div div *text*)) d)
|
||||
((sxpath '(html body div div ul)) d)
|
||||
((filter
|
||||
(lambda (nodeset)
|
||||
(equal?
|
||||
'(#f #f)
|
||||
(map (lambda (s)
|
||||
(equal?
|
||||
(string-prefix-length
|
||||
(sxml->string
|
||||
(append '(placeholder)
|
||||
((sxpath '(@ id)) nodeset)))
|
||||
s)
|
||||
4))
|
||||
'("GFG_" "_GFG")))))
|
||||
((take-until (filter (sxpath '(@ class (equal? "article_bottom_text")))))
|
||||
((sxpath '(// article (div 3) *)) d)))))
|
||||
113
nerd/templates.scm
Normal file
113
nerd/templates.scm
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
;; Copyright (C) 2024 Skylar Astaroth <cobra@vern.cc>
|
||||
;;
|
||||
;; This file is part of NerdsforNerds
|
||||
;;
|
||||
;; NerdsforNerds is free software: you can redistribute it and/or modify it
|
||||
;; under the terms of the GNU Affero General Public License as published by the
|
||||
;; Free Software Foundation, either version 3 of the License, or (at your
|
||||
;; option) any later version.
|
||||
;;
|
||||
;; This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
||||
;; for more details.
|
||||
;;
|
||||
;; You should have received a copy of the GNU Affero General Public License
|
||||
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
(define-module (nerd templates)
|
||||
#:use-module (nerd scraping)
|
||||
#:use-module (htmlprag)
|
||||
#:use-module (ice-9 string-fun)
|
||||
#:export (error-template
|
||||
index-template
|
||||
article-template))
|
||||
|
||||
(define (html-head title)
|
||||
`(head (title ,title " - NerdsforNerds")
|
||||
(meta (@ (charset "UTF-8")))
|
||||
(meta (@ (name "viewport") (content "width=device-width")))
|
||||
(link (@ (rel "stylesheet") (href "/style.css")))
|
||||
(link (@ (rel "icon") (type "image/png") (href "/favicon.png")))))
|
||||
|
||||
(define heading
|
||||
`(header))
|
||||
|
||||
(define source
|
||||
`(aside (@ (class "right"))
|
||||
(p (a (@ (href "https://git.vern.cc/cobra/NerdsforNerds"))
|
||||
"Source Code"))
|
||||
,(if (getenv "PATCHES_URL")
|
||||
`(p (a (@ (href ,(getenv "PATCHES_URL")))
|
||||
"Patches"))
|
||||
`())))
|
||||
|
||||
(define (index-template host)
|
||||
`(html ,(html-head "NerdsforNerds")
|
||||
(body
|
||||
,heading
|
||||
(article
|
||||
(h1 "NerdsforNerds")
|
||||
(p "NerdsforNerds is a privacy-respecting"
|
||||
" front-end to GeeksforGeeks.")
|
||||
(h2 "Usage")
|
||||
(p "You can use NerdsforNerds by replacing "
|
||||
(code "www.geeksforgeeks.org")
|
||||
" with this website's domain")
|
||||
(h3 "Example")
|
||||
(pre (code "https://www.geeksforgeeks.org/script-command-in"
|
||||
"-linux-with-examples/"))
|
||||
(p "becomes")
|
||||
(pre (code "https://" ,host "/script-command-in-"
|
||||
"linux-with-examples/")))
|
||||
,source)))
|
||||
|
||||
(define (article-template d)
|
||||
(let ((article (get-article d)))
|
||||
`(html ,(html-head (car article))
|
||||
(body
|
||||
,heading
|
||||
(input (@ (id "yuri")
|
||||
(type "checkbox")
|
||||
(name "girls,,,")))
|
||||
(aside (@ (class "left"))
|
||||
(label (@ (for "yuri"))
|
||||
(svg
|
||||
(@ (xmlns "http://www.w3.org/2000/svg")
|
||||
(fill "#ffffff")
|
||||
(width "5em")
|
||||
(height "5em")
|
||||
(viewbox "-2.5 0 19 19"))
|
||||
(path
|
||||
(@ (d ;,(string-append
|
||||
"M.789 4.836a1.03 1.03 0 0 1 1"
|
||||
".03-1.029h10.363a1.03 1.03 0 "
|
||||
"1 1 0 2.059H1.818A1.03 1.03 0"
|
||||
" 0 1 .79 4.836zm12.422 4.347a"
|
||||
"1.03 1.03 0 0 1-1.03 1.029H1."
|
||||
"819a1.03 1.03 0 0 1 0-2.059h1"
|
||||
"0.364a1.03 1.03 0 0 1 1.029 1"
|
||||
".03zm0 4.345a1.03 1.03 0 0 1-"
|
||||
"1.03 1.03H1.819a1.03 1.03 0 1"
|
||||
" 1 0-2.059h10.364a1.03 1.03 0"
|
||||
" 0 1 1.029 1.03z"))))) ;)
|
||||
,(list-ref article 5)
|
||||
(a (@ (href ,(string-replace-substring
|
||||
(caddr article)
|
||||
"https://www.geeksforgeeks.org" "")))
|
||||
(p ,(cadddr article))
|
||||
(p ,(list-ref article 4))))
|
||||
(article
|
||||
(h1 ,(car article))
|
||||
(h4 "Last updated: " ,(cadr article))
|
||||
(hr)
|
||||
(main
|
||||
,(cddr (cddddr article))))
|
||||
,source))))
|
||||
|
||||
(define (error-template code)
|
||||
`(html ,(html-head (number->string code))
|
||||
(body
|
||||
,heading
|
||||
(h1 ,(number->string code))
|
||||
,source)))
|
||||
Loading…
Reference in a new issue