Refactor siteinfo/license fetching
This commit is contained in:
		
							parent
							
								
									6b176e3f8f
								
							
						
					
					
						commit
						9aba3ad432
					
				
					 5 changed files with 19 additions and 18 deletions
				
			
		
							
								
								
									
										2
									
								
								info.rkt
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								info.rkt
									
										
									
									
									
								
							| 
						 | 
					@ -1,3 +1,3 @@
 | 
				
			||||||
#lang info
 | 
					#lang info
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(define build-deps '("rackunit-lib" "web-server-lib" "http-easy-lib" "html-parsing" "html-writing" "json-pointer" "ini-lib"))
 | 
					(define build-deps '("rackunit-lib" "web-server-lib" "http-easy-lib" "html-parsing" "html-writing" "json-pointer" "ini-lib" "memo"))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										27
									
								
								src/data.rkt
									
										
									
									
									
								
							
							
						
						
									
										27
									
								
								src/data.rkt
									
										
									
									
									
								
							| 
						 | 
					@ -1,33 +1,34 @@
 | 
				
			||||||
#lang racket/base
 | 
					#lang racket/base
 | 
				
			||||||
(require (prefix-in easy: net/http-easy)
 | 
					(require racket/list
 | 
				
			||||||
 | 
					         (prefix-in easy: net/http-easy)
 | 
				
			||||||
 | 
					         memo
 | 
				
			||||||
         "url-utils.rkt"
 | 
					         "url-utils.rkt"
 | 
				
			||||||
         "xexpr-utils.rkt")
 | 
					         "xexpr-utils.rkt")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(provide
 | 
					(provide
 | 
				
			||||||
 | 
					 (struct-out siteinfo)
 | 
				
			||||||
 (struct-out license)
 | 
					 (struct-out license)
 | 
				
			||||||
 license-default
 | 
					 siteinfo-fetch
 | 
				
			||||||
 license-auto)
 | 
					 license-default)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(struct siteinfo (sitename basepage license) #:transparent)
 | 
				
			||||||
(struct license (text url) #:transparent)
 | 
					(struct license (text url) #:transparent)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(define license-default (license "CC-BY-SA" "https://www.fandom.com/licensing"))
 | 
					(define license-default (license "CC-BY-SA" "https://www.fandom.com/licensing"))
 | 
				
			||||||
(define license-hash (make-hash))
 | 
					
 | 
				
			||||||
(define (license-fetch wikiname)
 | 
					(define/memoize (siteinfo-fetch wikiname) #:hash hash
 | 
				
			||||||
  (define dest-url
 | 
					  (define dest-url
 | 
				
			||||||
    (format "https://~a.fandom.com/api.php?~a"
 | 
					    (format "https://~a.fandom.com/api.php?~a"
 | 
				
			||||||
            wikiname
 | 
					            wikiname
 | 
				
			||||||
            (params->query '(("action" . "query")
 | 
					            (params->query '(("action" . "query")
 | 
				
			||||||
                             ("meta" . "siteinfo")
 | 
					                             ("meta" . "siteinfo")
 | 
				
			||||||
                             ("siprop" . "rightsinfo")
 | 
					                             ("siprop" . "general|rightsinfo")
 | 
				
			||||||
                             ("format" . "json")
 | 
					                             ("format" . "json")
 | 
				
			||||||
                             ("formatversion" . "2")))))
 | 
					                             ("formatversion" . "2")))))
 | 
				
			||||||
  (log-outgoing dest-url)
 | 
					  (log-outgoing dest-url)
 | 
				
			||||||
  (define res (easy:get dest-url))
 | 
					  (define res (easy:get dest-url))
 | 
				
			||||||
  (define data (easy:response-json res))
 | 
					  (define data (easy:response-json res))
 | 
				
			||||||
 | 
					  (siteinfo (jp "/query/general/sitename" data)
 | 
				
			||||||
 | 
					            (second (regexp-match #rx"/wiki/(.*)" (jp "/query/general/base" data)))
 | 
				
			||||||
            (license (jp "/query/rightsinfo/text" data)
 | 
					            (license (jp "/query/rightsinfo/text" data)
 | 
				
			||||||
           (jp "/query/rightsinfo/url" data)))
 | 
					                     (jp "/query/rightsinfo/url" data))))
 | 
				
			||||||
(define (license-auto wikiname)
 | 
					 | 
				
			||||||
  (if (hash-has-key? license-hash wikiname)
 | 
					 | 
				
			||||||
      (hash-ref license-hash wikiname)
 | 
					 | 
				
			||||||
      (let ([result (license-fetch wikiname)])
 | 
					 | 
				
			||||||
        (hash-set! license-hash wikiname result)
 | 
					 | 
				
			||||||
        result)))
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -89,7 +89,7 @@
 | 
				
			||||||
                (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 (siteinfo-license (siteinfo-fetch wikiname))])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    (define title (preprocess-html-wiki (jp "/parse/title" page-data prefixed-category)))
 | 
					    (define title (preprocess-html-wiki (jp "/parse/title" page-data prefixed-category)))
 | 
				
			||||||
    (define page-html (preprocess-html-wiki (jp "/parse/text" page-data "")))
 | 
					    (define page-html (preprocess-html-wiki (jp "/parse/text" page-data "")))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -70,7 +70,7 @@
 | 
				
			||||||
   (thread-let
 | 
					   (thread-let
 | 
				
			||||||
    ([dest-res (log-outgoing dest-url)
 | 
					    ([dest-res (log-outgoing dest-url)
 | 
				
			||||||
               (easy:get dest-url #:timeouts timeouts)]
 | 
					               (easy:get dest-url #:timeouts timeouts)]
 | 
				
			||||||
     [license (license-auto wikiname)])
 | 
					     [license (siteinfo-license (siteinfo-fetch wikiname))])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    (define data (easy:response-json dest-res))
 | 
					    (define data (easy:response-json dest-res))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -248,7 +248,7 @@
 | 
				
			||||||
                                         ("format" . "json")))))
 | 
					                                         ("format" . "json")))))
 | 
				
			||||||
              (log-outgoing dest-url)
 | 
					              (log-outgoing dest-url)
 | 
				
			||||||
              (easy:get dest-url #:timeouts timeouts)]
 | 
					              (easy:get dest-url #:timeouts timeouts)]
 | 
				
			||||||
    [license (license-auto wikiname)])
 | 
					    [license (siteinfo-license (siteinfo-fetch wikiname))])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   (cond
 | 
					   (cond
 | 
				
			||||||
     [(eq? 200 (easy:response-status-code dest-res))
 | 
					     [(eq? 200 (easy:response-status-code dest-res))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue