@@ -5,10 +5,9 @@ import (
55 "fmt"
66 "io"
77 "net/http"
8- "os "
9- "strconv "
8+ "net/url "
9+ "path "
1010 "strings"
11- "time"
1211)
1312
1413// FeedsURL takes a set of query terms and returns the url with query
@@ -63,48 +62,67 @@ func RdmRecordURL(hostname string, id string) string {
6362 return fmt .Sprintf ("https://%s/api/records/%s/files" , hostname , id )
6463}
6564
66- func RdmFetchJSON (u string ) ([]byte , time.Duration , error ) {
67- var targetTime time.Time
65+ func RdmFetchJSON (u string ) ([]byte , error ) {
6866 res , err := http .Get (u )
69- tReset := res .Header .Get ("X-RateLimit-Reset" )
70- if tReset != "" {
71- unixTime , _ := strconv .ParseInt (tReset , 10 , 64 )
72- targetTime = time .Unix (unixTime , 0 )
73- fmt .Fprintf (os .Stderr , "Reset will happen at %s\n " , targetTime .Format (time .RFC822Z ))
74- }
7567 if err != nil {
76- return nil , time .Until (targetTime ), err
77- }
78- duration := time .Until (targetTime )
79- if res .StatusCode != 200 {
80- return nil , duration , fmt .Errorf ("failed to retrieve %s, %s" , u , res .Status )
68+ return nil , err
8169 }
8270 body , err := io .ReadAll (res .Body )
8371 if err != nil {
84- return nil , duration , err
72+ return nil , err
8573 }
8674 res .Body .Close ()
87- return body , duration , nil
75+ return body , nil
8876}
8977
9078func RdmPdfURLs (src []byte ) ([]string , error ) {
91- //fmt.Printf("DEBUG obj retrieved -> %s\n", src)
9279 contentUrls := []string {}
9380 obj := map [string ]interface {}{}
9481 if err := JSONUnmarshal (src , & obj ); err != nil {
9582 return nil , fmt .Errorf ("failed to unmarhsal object %s\n " , err )
9683 }
97- if entries , ok := obj ["entries" ].([]map [string ]interface {}); ok {
98- for _ , entry := range entries {
84+ // DEBUG
85+ //src, _ = JSONMarshalIndent(obj, "", " ") // DEBUG
86+ //fmt.Printf("DEBUG obj retrieved -> %s\n", src) // DEBUG
87+ if entries , ok := obj ["entries" ].([]interface {}); ok {
88+ //fmt.Printf("DEBUG entries (%T): %+v\n", entries)
89+ for _ , val := range entries {
90+ entry := val .(map [string ]interface {})
91+ //src, _ = JSONMarshalIndent(entry, "", " ") // DEBUG
92+ //fmt.Printf("DEBUG obj entries -> %s\n", src) // DEBUG
9993 if mimetype , ok := entry ["mimetype" ].(string ); ok && mimetype == "application/pdf" {
100- if links , ok := entry ["links" ].(map [string ]string ); ok {
101- if contentUrl , ok := links ["content" ]; ok {
102- fmt .Printf ("DEBUG contentUrl: %s\n " , contentUrl )
94+ //fmt.Printf("DEBUG entry.mimetype -> %s\n", mimetype) // DEBUG
95+ if links , ok := entry ["links" ].(map [string ]interface {}); ok {
96+ //src, _ = JSONMarshalIndent(links, "", " ") // DEBUG
97+ //fmt.Printf("DEBUG links %s\n", src) // DEBUG
98+ if contentUrl , ok := links ["content" ].(string ); ok {
99+ //fmt.Printf("DEBUG contentUrl: %s\n", contentUrl)
103100 contentUrls = append (contentUrls , contentUrl )
104101 }
105102 }
106103 }
107104 }
108105 }
109106 return contentUrls , nil
107+ }
108+
109+ func RdmGetFilenameFromContentURL (s string ) (string , error ) {
110+ u , err := url .Parse (s )
111+ if err != nil {
112+ return "" , err
113+ }
114+ return path .Base (strings .TrimSuffix (u .Path , "/content" )), nil
115+ }
116+
117+ func RdmRetrieveFile (u string ) ([]byte , error ) {
118+ res , err := http .Get (u )
119+ if err != nil {
120+ return nil , err
121+ }
122+ body , err := io .ReadAll (res .Body )
123+ if err != nil {
124+ return nil , err
125+ }
126+ res .Body .Close ()
127+ return body , nil
110128}
0 commit comments