liquid.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package kpler
  2. import (
  3. "encoding/json"
  4. "eta/eta_data_analysis/models"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "eta/eta_data_analysis/utils"
  11. )
  12. // 获取token登录凭证
  13. func login()(token string, err error){
  14. url := "https://api.kpler.com/v1/login"
  15. method := "POST"
  16. payload := strings.NewReader(`{
  17. "email": "` + utils.KplerApiAccount + `",
  18. "password": "` + utils.KplerApiPassword + `"
  19. }`)
  20. client := &http.Client {
  21. }
  22. req, err := http.NewRequest(method, url, payload)
  23. if err != nil {
  24. fmt.Println(err)
  25. return
  26. }
  27. req.Header.Add("Content-Type", "application/json")
  28. res, err := client.Do(req)
  29. if err != nil {
  30. fmt.Println(err)
  31. return
  32. }
  33. defer res.Body.Close()
  34. body, err := ioutil.ReadAll(res.Body)
  35. if err != nil {
  36. fmt.Println(err)
  37. return
  38. }
  39. fmt.Println(string(body))
  40. //bodyStr := `{"token":"lfl883KgRgwsBg_yuHjv05vr6voK2ac8ju47jiUoR8ccs","user":{"accounts":["coal","lpg","lng","oil","cpp","merge","liquids"]}}`
  41. //解析body
  42. var result map[string]interface{}
  43. err = json.Unmarshal(body, &result)
  44. if err != nil {
  45. fmt.Println(err)
  46. return
  47. }
  48. token = result["token"].(string)
  49. return
  50. }
  51. // 分别获取group为:Clean Products;Crude/Co; DPP)的产品
  52. func GetProductsByApi(params models.KplerProductLibReq, token string) (data []models.KplerProduct, err error) {
  53. uri := "https://api.kpler.com/v1/products"
  54. ancestorFamilyIds := url.QueryEscape(params.AncestorFamilyIds)
  55. ancestorFamilyNames := url.QueryEscape(params.AncestorFamilyNames)
  56. ancestorGroupIds := url.QueryEscape(params.AncestorGroupIds)
  57. ancestorGroupNames := url.QueryEscape(params.AncestorGroupNames)
  58. ancestorProductIds := url.QueryEscape(params.AncestorProductIds)
  59. ancestorProductNames := url.QueryEscape(params.AncestorProductNames)
  60. ancestorGradeIds := url.QueryEscape(params.AncestorGradeIds)
  61. ancestorGradeNames := url.QueryEscape(params.AncestorGradeNames)
  62. products := params.Products
  63. productIds := params.ProductIds
  64. uri = fmt.Sprintf("%s?ancestorFamilyIds=%s&ancestorFamilyNames=%s&ancestorGroupIds=%s&ancestorGroupNames=%s&ancestorProductIds=%s&ancestorProductNames=%s&ancestorGradeIds=%s&ancestorGradeNames=%s&products=%s&productIds=%s", uri, ancestorFamilyIds, ancestorFamilyNames, ancestorGroupIds, ancestorGroupNames, ancestorProductIds, ancestorProductNames, ancestorGradeIds, ancestorGradeNames, products, productIds)
  65. method := "GET"
  66. client := &http.Client {
  67. }
  68. req, err := http.NewRequest(method, uri, nil)
  69. if err != nil {
  70. fmt.Println(err)
  71. return
  72. }
  73. req.Header.Add("Content-Type", "application/json")
  74. req.Header.Add("Authorization", token)
  75. res, err := client.Do(req)
  76. if err != nil {
  77. fmt.Println(err)
  78. return
  79. }
  80. defer res.Body.Close()
  81. body, err := ioutil.ReadAll(res.Body)
  82. if err != nil {
  83. fmt.Println(err)
  84. return
  85. }
  86. fmt.Println(string(body))
  87. //bodystr := "Id (Product);Name;Type (Product);Family;Family Id;Group;Group Id;Product;Product Id;Grade;Grade Id;Density (Product);Density Unit;Energy Density;Energy Density Unit;Expansion Ratio
  88. // 2952;CPC Russia;grade;Dirty;1398;Crude/Co;1370;Crude;1368;CPC Russia;2952;805.0;kg/cm;26948.236;MJ/cm;1.0
  89. // 2953;CPC Kazakhstan;grade;Dirty;1398;Crude/Co;1370;Crude;1368;CPC Kazakhstan;2953;805.0;kg/cm;26948.236;MJ/cm;1.0
  90. // 1360;CPC;grade;Dirty;1398;Crude/Co;1370;Crude;1368;CPC;1360;805.0;kg/cm;26948.236;MJ/cm;1.0"
  91. // 解析body
  92. var result map[string]interface{}
  93. resErr := json.Unmarshal(body, &result)
  94. if resErr == nil {
  95. if result["message"] == "Unauthorized" {
  96. fmt.Println("Unauthorized")
  97. return
  98. }
  99. err = fmt.Errorf(result["message"].(string))
  100. return
  101. }
  102. // 解析result
  103. bodyStr := string(body)
  104. lines := strings.Split(bodyStr, "\n")
  105. for _, line := range lines {
  106. fields := strings.Split(line, ";")
  107. if len(fields) < 10 {
  108. continue
  109. }
  110. data = append(data, models.KplerProduct{
  111. Id: fields[0],
  112. Name: fields[1],
  113. Type: fields[2],
  114. Family: fields[3],
  115. FamilyId: fields[4],
  116. Group: fields[5],
  117. GroupId: fields[6],
  118. Product: fields[7],
  119. ProductId: fields[8],
  120. Grade: fields[9],
  121. GradeId: fields[10],
  122. Density: fields[11],
  123. DensityUnit: fields[12],
  124. EnergyDensity: fields[13],
  125. EnergyDensityUnit: fields[14],
  126. ExpansionRatio: fields[15],
  127. })
  128. }
  129. return
  130. }
  131. // 根据flowDirection 和 products 循环调用
  132. func GetKplerDataByApi(params models.KplerFlowDataLibReq, token string) (ret *models.KplerFlowDataResp, err error) {
  133. flowDirection := params.FlowDirection
  134. granularity := params.Granularity
  135. products := url.QueryEscape(params.Products)
  136. split := url.QueryEscape(params.Split)
  137. startDate := params.StartDate
  138. endDate := params.EndDate
  139. unit := params.Unit
  140. withIntraRegion := params.WithIntraRegion
  141. fromZones := url.QueryEscape(params.FromZones)
  142. toZones := url.QueryEscape(params.ToZones)
  143. onlyRealized := params.OnlyRealized
  144. withForecast := params.WithForecast
  145. withProductEstimation := params.WithProductEstimation
  146. // fromInstallations := req.FromInstallations
  147. // toInstallations := req.ToInstallations
  148. // fromCountries := req.FromCountries
  149. // toCountries := req.ToCountries
  150. // vesselTypes := req.VesselTypes
  151. // vesselTypesAlt := req.VesselTypesAlt
  152. // withIntraCountry := req.WithIntraCountry
  153. //
  154. // withFreightView := req.WithFreightView
  155. url := fmt.Sprintf("https://api.kpler.com/v1/flows?unit=%s&flowDirection=%s&granularity=%s&products=%s&split=%s&withIntraRegion=%s&startDate=%s&endDate=%s&fromZones=%s&toZones=%s&onlyRealized=%s&withForecast=%s&withProductEstimation=%s", unit, flowDirection, granularity, products, split, withIntraRegion, startDate, endDate, fromZones, toZones, onlyRealized, withForecast, withProductEstimation)
  156. method := "GET"
  157. client := &http.Client {
  158. }
  159. req, err := http.NewRequest(method, url, nil)
  160. if err != nil {
  161. fmt.Println(err)
  162. return
  163. }
  164. req.Header.Add("Authorization", token)
  165. res, err := client.Do(req)
  166. if err != nil {
  167. fmt.Println(err)
  168. return
  169. }
  170. defer res.Body.Close()
  171. body, err := ioutil.ReadAll(res.Body)
  172. if err != nil {
  173. fmt.Println(err)
  174. return
  175. }
  176. fmt.Println(string(body))
  177. //{"message":"Unauthorized"}
  178. // 解析body
  179. var result map[string]interface{}
  180. resErr := json.Unmarshal(body, &result)
  181. if resErr == nil {
  182. if result["message"] == "Unauthorized" {
  183. fmt.Println("Unauthorized")
  184. return
  185. }
  186. err = fmt.Errorf(result["message"].(string))
  187. return
  188. }
  189. // bodystr :=`Date;China;Period End Date
  190. // 2024-07;35763.15;2024-07-31
  191. // 2024-08;35386.42;2024-08-31
  192. // 2024-09;39657.10;2024-09-30
  193. // 2024-10;39909.08;2024-10-31
  194. // 2024-11;36541.03;2024-11-30
  195. // 2024-12;38551.49;2024-12-31
  196. // 2025-01;34607.56;2025-01-31
  197. // 2025-02;28280.53;2025-02-28
  198. // 2025-03;29965.73;2025-03-31
  199. // 2025-04;15157.51;2025-04-30
  200. // 2025-05;3795.25;2025-05-31
  201. // 2025-06;0;2025-06-30`
  202. // 解析result
  203. bodyStr := string(body)
  204. lines := strings.Split(bodyStr, "\n")
  205. // 解析lines
  206. splitNameMap := make(map[int]string)
  207. splitDataMap := make(map[int][]models.KplerBaseExcelData)
  208. endDateCol := 0
  209. for row, line := range lines {
  210. fields := strings.Split(line, ";")
  211. if len(fields) < 3 {
  212. continue
  213. }
  214. for col, field := range fields {
  215. if col == 0 {
  216. continue
  217. }
  218. // 处理表头
  219. if row == 0 {
  220. if field == "Period End Date" {
  221. endDateCol = col
  222. }else if field == "Date" {
  223. continue
  224. }else{
  225. splitNameMap[col] = field
  226. }
  227. }else{
  228. if col == endDateCol {
  229. continue
  230. }
  231. date := fields[endDateCol]
  232. value := fields[col]
  233. splitDataMap[col] = append(splitDataMap[col], models.KplerBaseExcelData{
  234. DataTime: date,
  235. Value: value,
  236. })
  237. }
  238. }
  239. }
  240. data := make([]models.KplerFlowData, 0)
  241. for col, name := range splitNameMap {
  242. data = append(data, models.KplerFlowData{
  243. SplitItem: name,
  244. IndexData: splitDataMap[col],
  245. })
  246. }
  247. ret = &models.KplerFlowDataResp{
  248. List: data,
  249. ApiQueryUrl: url,
  250. }
  251. return
  252. }
  253. func GetZonesByApi(token string, ancestorName string, descendantType string) (data []models.KplerZone, err error) {
  254. //url := "https://api.kpler.com/v1/zones"
  255. url := fmt.Sprintf("https://api.kpler.com/v1/zones?ancestorName=%s&descendantType=%s", ancestorName, descendantType)
  256. method := "GET"
  257. client := &http.Client {
  258. }
  259. req, err := http.NewRequest(method, url, nil)
  260. if err != nil {
  261. fmt.Println(err)
  262. return
  263. }
  264. req.Header.Add("Content-Type", "application/json")
  265. req.Header.Add("Authorization", token)
  266. res, err := client.Do(req)
  267. if err != nil {
  268. fmt.Println(err)
  269. return
  270. }
  271. defer res.Body.Close()
  272. body, err := ioutil.ReadAll(res.Body)
  273. if err != nil {
  274. fmt.Println(err)
  275. return
  276. }
  277. fmt.Println(string(body))
  278. // bodyStr := `Ancestor Id;Ancestor Name;Ancestor Type;Descendant Id;Descendant Name;Descendant Type
  279. // 87;Baltic Sea;gulf;1669;Kokkola;port
  280. // 87;Baltic Sea;gulf;1264;Stigsnaes;port
  281. // 87;Baltic Sea;gulf;110162;Uddevalla;port
  282. // 87;Baltic Sea;gulf;112012;Harnosand;port
  283. // 87;Baltic Sea;gulf;112945;Energihamnen;port
  284. // 87;Baltic Sea;gulf;112957;Falkenberg;port
  285. // 87;Baltic Sea;gulf;110567;Jakobstad;port
  286. // 87;Baltic Sea;gulf;112930;Sandefjord;port
  287. // 87;Baltic Sea;gulf;113141;Korsor;port
  288. // 87;Baltic Sea;gulf;3603;Inkoo;port
  289. // 87;Baltic Sea;gulf;112946;Skeppsbron;port
  290. // 87;Baltic Sea;gulf;112943;Vartahamnen;port
  291. // 87;Baltic Sea;gulf;112936;Solvesborg;port
  292. // 87;Baltic Sea;gulf;3388;Pori;port
  293. // 87;Baltic Sea;gulf;112944;Stadsgarden;port
  294. // 87;Baltic Sea;gulf;1697;Nacka;port
  295. // 87;Baltic Sea;gulf;107545;Grenaa;port
  296. // 87;Baltic Sea;gulf;107515;Wismar;port
  297. // 87;Baltic Sea;gulf;2604;Vysotsk;port
  298. // 87;Baltic Sea;gulf;112752;Stockholm;port
  299. // 87;Baltic Sea;gulf;113125;Monsteras;port
  300. // 87;Baltic Sea;gulf;113161;Hirtshals;port
  301. // 87;Baltic Sea;gulf;116132;Trelleborg;port
  302. // 87;Baltic Sea;gulf;1400;Lindø Industrial Park;port
  303. // 87;Baltic Sea;gulf;112013;Sandarne;port
  304. // 87;Baltic Sea;gulf;112011;Ornskoldsvik;port
  305. // 87;Baltic Sea;gulf;107089;Landskrona;port
  306. // 87;Baltic Sea;gulf;4689;Koping;port
  307. // 87;Baltic Sea;gulf;112745;Kaskinen;port
  308. // 87;Baltic Sea;gulf;112210;Vasteras;port
  309. // 87;Baltic Sea;gulf;112165;Kalmar;port
  310. // 87;Baltic Sea;gulf;112167;Paljassaare;port
  311. // 87;Baltic Sea;gulf;112152;Forby;port
  312. // 87;Baltic Sea;gulf;112194;Port of Koge;port
  313. // 87;Baltic Sea;gulf;112202;Lomonosov;port
  314. // 87;Baltic Sea;gulf;3423;Aarhus;port
  315. // 87;Baltic Sea;gulf;107591;Koloniya;port
  316. // 87;Baltic Sea;gulf;6812;Nyborg;port
  317. // 87;Baltic Sea;gulf;113842;Halden;port
  318. // 87;Baltic Sea;gulf;1027;Porvoo;port
  319. // 87;Baltic Sea;gulf;116201;Nykobing Falster;port
  320. // 87;Baltic Sea;gulf;116181;Ostrand;port
  321. // 87;Baltic Sea;gulf;113276;Karlsborg;port
  322. // 87;Baltic Sea;gulf;1651;Gdynia;port
  323. // 87;Baltic Sea;gulf;1102;Naantali;port
  324. // 87;Baltic Sea;gulf;112137;Drammen;port
  325. // 87;Baltic Sea;gulf;1165;Klaipeda;port
  326. // 87;Baltic Sea;gulf;6167;Hamina;port
  327. // 87;Baltic Sea;gulf;113292;Vastervik;port
  328. // 87;Baltic Sea;gulf;116242;Saetre;port
  329. // 87;Baltic Sea;gulf;116535;Frederikshavn;port
  330. // 87;Baltic Sea;gulf;1444;Aabenraa;port
  331. // 87;Baltic Sea;gulf;3725;Apatyth FSU;port
  332. // 87;Baltic Sea;gulf;1271;Primorsk;port
  333. // 87;Baltic Sea;gulf;1465;Karlshamn;port
  334. // 87;Baltic Sea;gulf;1399;Paldiski;port
  335. // 87;Baltic Sea;gulf;1684;Kemi;port
  336. // 87;Baltic Sea;gulf;1717;Vaasa;port
  337. // 87;Baltic Sea;gulf;110127;Nordjyllandsvaerket;port
  338. // 87;Baltic Sea;gulf;3467;Kiel;port
  339. // 87;Baltic Sea;gulf;4239;Kaliningrad;port
  340. // 87;Baltic Sea;gulf;3805;Loudden;port
  341. // 87;Baltic Sea;gulf;1404;Provestenen;port
  342. // 87;Baltic Sea;gulf;3403;Södertälje;port
  343. // 87;Baltic Sea;gulf;2002;Liepaja;port
  344. // 87;Baltic Sea;gulf;3389;Mussalo;port
  345. // 87;Baltic Sea;gulf;3407;Sundsvall;port
  346. // 87;Baltic Sea;gulf;3392;Halmstad;port
  347. // 87;Baltic Sea;gulf;2215;Raahe;port
  348. // 87;Baltic Sea;gulf;1334;Riga Harbour;port
  349. // 87;Baltic Sea;gulf;3381;Miiduranna;port
  350. // 87;Baltic Sea;gulf;1166;Gdansk;port
  351. // 87;Baltic Sea;gulf;107049;Oskarshamn;port
  352. // 87;Baltic Sea;gulf;3413;Holmsund;port
  353. // 87;Baltic Sea;gulf;3391;Rauma;port
  354. // 87;Baltic Sea;gulf;3393;Helsingborg;port
  355. // 87;Baltic Sea;gulf;3438;Sjursoya;port
  356. // 87;Baltic Sea;gulf;1553;Rostock;port
  357. // 87;Baltic Sea;gulf;1155;Sillamäe;port
  358. // 87;Baltic Sea;gulf;3664;Szczecin;port
  359. // 87;Baltic Sea;gulf;1362;Malmo;port
  360. // 87;Baltic Sea;gulf;1104;Nynashamn;port
  361. // 87;Baltic Sea;gulf;1158;Butinge;port
  362. // 87;Baltic Sea;gulf;1700;Oulu;port
  363. // 87;Baltic Sea;gulf;5454;Slagen;port
  364. // 87;Baltic Sea;gulf;1477;Norrkoping;port
  365. // 87;Baltic Sea;gulf;6722;Kunda Bay;port
  366. // 87;Baltic Sea;gulf;6761;Pitea;port
  367. // 87;Baltic Sea;gulf;1020;Swinoujscie Area;port
  368. // 87;Baltic Sea;gulf;3426;Aalborg;port
  369. // 87;Baltic Sea;gulf;105360;Visby;port
  370. // 87;Baltic Sea;gulf;3151;Gavle;port
  371. // 87;Baltic Sea;gulf;1445;Oxelosund;port
  372. // 87;Baltic Sea;gulf;3411;Ronnskar;port
  373. // 87;Baltic Sea;gulf;113011;Husum;port
  374. // 87;Baltic Sea;gulf;2008;Lulea;port
  375. // 87;Baltic Sea;gulf;107538;Varberg;port
  376. // 87;Baltic Sea;gulf;107537;Orrskar;port
  377. // 87;Baltic Sea;gulf;4690;Uusikaupunki Port;port
  378. // 87;Baltic Sea;gulf;110094;Studstrup;port
  379. // 87;Baltic Sea;gulf;6723;Helsinki;port
  380. // 87;Baltic Sea;gulf;1028;St Petersburg;port
  381. // 87;Baltic Sea;gulf;107467;Valko;port
  382. // 87;Baltic Sea;gulf;116671;Skoghall;port
  383. // 87;Baltic Sea;gulf;2464;Ventspils;port
  384. // 87;Baltic Sea;gulf;113860;Soby Havn;port
  385. // 87;Baltic Sea;gulf;3382;Kopli;port
  386. // 87;Baltic Sea;gulf;1156;Muuga Harbour;port
  387. // 87;Baltic Sea;gulf;2601;Ust Luga;port`
  388. // 解析result
  389. bodyStr := string(body)
  390. lines := strings.Split(bodyStr, "\n")
  391. for i, line := range lines {
  392. if i == 0 {
  393. continue
  394. }
  395. fields := strings.Split(line, ";")
  396. if len(fields) < 6 {
  397. continue
  398. }
  399. data = append(data, models.KplerZone{
  400. AncestorId: fields[0],
  401. AncestorName: fields[1],
  402. AncestorType: fields[2],
  403. DescendantId: fields[3],
  404. DescendantName: fields[4],
  405. DescendantType: fields[5],
  406. })
  407. }
  408. return
  409. }