book.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package inventaire
  2. import (
  3. "notabug.org/apiote/amuse/datastructure"
  4. "notabug.org/apiote/amuse/network"
  5. "database/sql"
  6. "encoding/json"
  7. "net/http"
  8. "strings"
  9. "notabug.org/apiote/gott"
  10. )
  11. type InventaireUriResponse struct {
  12. Entities map[string]struct {
  13. Labels map[string]string
  14. Claims map[string][]string
  15. Descriptions map[string]string
  16. Image struct {
  17. Url string
  18. Credits struct {
  19. Text string
  20. Url string
  21. }
  22. }
  23. Sitelinks map[string]string
  24. }
  25. }
  26. type InventaireClaimResponse struct {
  27. Uris []string
  28. }
  29. func createBookRequest(args ...interface{}) (interface{}, error) {
  30. request := args[0].(*network.Request)
  31. result := args[1].(*network.Result)
  32. result.Client = &http.Client{}
  33. httpRequest, err := http.NewRequest("GET", "https://inventaire.io/api/entities?action=by-uris&uris="+request.Id+"&refresh=false", nil)
  34. result.Request = httpRequest
  35. return gott.Tuple(args), err
  36. }
  37. func findByTag(haystack map[string]string, tag, suffix string) string {
  38. tag = strings.ToLower(tag)
  39. result := haystack[tag+suffix]
  40. if result == "" {
  41. tag = strings.Split(tag, "-")[0]
  42. result = haystack[tag+suffix]
  43. }
  44. if result == "" {
  45. tag = "en"
  46. result = haystack[tag+suffix]
  47. }
  48. return result
  49. }
  50. func unmarshalBook(args ...interface{}) (interface{}, error) {
  51. id := args[0].(*network.Request).Id
  52. language := args[0].(*network.Request).Language
  53. result := args[1].(*network.Result)
  54. response := &InventaireUriResponse{}
  55. err := json.Unmarshal(result.Body, response)
  56. book := &datastructure.Book{}
  57. book.Id = id
  58. book.Uri = "/books/" + id
  59. source := datastructure.Source{
  60. Url: "https://inventaire.io/entity/" + id,
  61. Name: "Inventaire",
  62. }
  63. book.Source = append(book.Source, source)
  64. book.Authors = append(book.Authors, response.Entities[id].Claims["wdt:P50"]...)
  65. book.Title = findByTag(response.Entities[id].Labels, language, "")
  66. book.Article = findByTag(response.Entities[id].Sitelinks, language, "wiki")
  67. book.Description = findByTag(response.Entities[id].Descriptions, language, "")
  68. result.Result = book
  69. return gott.Tuple(args), err
  70. }
  71. func getBook(args ...interface{}) (interface{}, error) {
  72. id := args[0].(string)
  73. language := args[1].(string)
  74. book := args[2].(*datastructure.Book)
  75. result, err := gott.
  76. NewResult(gott.Tuple{&network.Request{Id: id, Language: language}, &network.Result{}}).
  77. Bind(createBookRequest).
  78. //Bind(cache.getCacheEntry).
  79. Map(network.AddHeaders).
  80. Bind(network.DoRequest).
  81. Bind(network.HandleRequestError).
  82. Bind(network.ReadResponse).
  83. //Tee(cache.cleanCache).
  84. //Tee(cache.saveCacheEntry).
  85. Bind(unmarshalBook).
  86. Finish()
  87. if err == nil {
  88. *book = *result.(gott.Tuple)[1].(*network.Result).Result.(*datastructure.Book)
  89. }
  90. return gott.Tuple(args), err
  91. }
  92. func createAuthorsRequest(args ...interface{}) (interface{}, error) {
  93. request := args[0].(*network.Request)
  94. result := args[1].(*network.Result)
  95. result.Client = &http.Client{}
  96. httpRequest, err := http.NewRequest("GET", "https://inventaire.io/api/entities?action=by-uris&uris="+request.Id+"&refresh=false", nil)
  97. result.Request = httpRequest
  98. return gott.Tuple(args), err
  99. }
  100. func unmarshalAuthors(args ...interface{}) (interface{}, error) {
  101. id := args[0].(*network.Request).Id
  102. language := args[0].(*network.Request).Language
  103. result := args[1].(*network.Result)
  104. response := &InventaireUriResponse{}
  105. err := json.Unmarshal(result.Body, response)
  106. ids := strings.Split(id, "|")
  107. result.Result = []string{}
  108. for _, authorId := range ids {
  109. result.Result = append(result.Result.([]string), findByTag(response.Entities[authorId].Labels, language, ""))
  110. }
  111. return gott.Tuple(args), err
  112. }
  113. func getAuthors(args ...interface{}) (interface{}, error) {
  114. language := args[1].(string)
  115. book := args[2].(*datastructure.Book)
  116. id := strings.Join(book.Authors, "|")
  117. author, err := gott.
  118. NewResult(gott.Tuple{&network.Request{Id: id, Language: language}, &network.Result{}}).
  119. Bind(createAuthorsRequest).
  120. //Bind(cache.getCacheEntry).
  121. Map(network.AddHeaders).
  122. Bind(network.DoRequest).
  123. Bind(network.HandleRequestError).
  124. Bind(network.ReadResponse).
  125. //Tee(cache.cleanCache).
  126. //Tee(cache.saveCacheEntry).
  127. Bind(unmarshalAuthors).
  128. Finish()
  129. if err == nil {
  130. book.Authors = author.(gott.Tuple)[1].(*network.Result).Result.([]string)
  131. }
  132. return gott.Tuple(args), err
  133. }
  134. func createEditionRequest(args ...interface{}) (interface{}, error) {
  135. request := args[0].(*network.Request)
  136. result := args[1].(*network.Result)
  137. result.Client = &http.Client{}
  138. httpRequest, err := http.NewRequest("GET", "https://inventaire.io/api/entities?action=reverse-claims&value="+request.Id+"&property=wdt:P629&refresh=false", nil)
  139. result.Request = httpRequest
  140. return gott.Tuple(args), err
  141. }
  142. func unmarshalEdition(args ...interface{}) (interface{}, error) {
  143. result := args[1].(*network.Result)
  144. response := &InventaireClaimResponse{}
  145. err := json.Unmarshal(result.Body, response)
  146. if len(response.Uris) > 0 {
  147. result.Result = response.Uris[0]
  148. } else {
  149. result.Result = ""
  150. }
  151. return gott.Tuple(args), err
  152. }
  153. func getEditions(args ...interface{}) (interface{}, error) {
  154. id := args[0].(string)
  155. language := args[1].(string)
  156. book := args[2].(*datastructure.Book)
  157. edition, err := gott.
  158. NewResult(gott.Tuple{&network.Request{Id: id, Language: language}, &network.Result{}}).
  159. Bind(createEditionRequest).
  160. //Bind(cache.getCacheEntry).
  161. Map(network.AddHeaders).
  162. Bind(network.DoRequest).
  163. Bind(network.HandleRequestError).
  164. Bind(network.ReadResponse).
  165. //Tee(cache.cleanCache).
  166. //Tee(cache.saveCacheEntry).
  167. Bind(unmarshalEdition).
  168. Finish()
  169. if err == nil {
  170. book.Cover = edition.(gott.Tuple)[1].(*network.Result).Result.(string)
  171. }
  172. return gott.Tuple(args), err
  173. }
  174. func GetBook(id string, language string, connection *sql.DB) (*datastructure.Book, error) {
  175. r, err := gott.
  176. NewResult(gott.Tuple{id, language, &datastructure.Book{}}).
  177. Bind(getBook).
  178. Bind(getAuthors).
  179. Bind(getEditions).
  180. Finish()
  181. if err != nil {
  182. return &datastructure.Book{}, err
  183. } else {
  184. return r.(gott.Tuple)[2].(*datastructure.Book), nil
  185. }
  186. }