fetcher.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import json
  2. import requests
  3. import time
  4. class Fetcher:
  5. def __init__(self, farmId: "str" = "330110004"):
  6. self.FarmId: "str" = farmId
  7. self.Session: "requests.Session" = requests.session()
  8. self.ids: "list[tuple]" = []
  9. self.data: "list[tuple]" = [("id", "mark", "timestamp", "time", "inner", "outer", "env", "act")]
  10. def login(self):
  11. url = "http://119.3.44.183:8016/admin/my/loginMultilevel"
  12. payload = {"accountName": "superadmin", "password": "hm123456", "farmId": self.FarmId}
  13. resp = self.Session.post(url=url, json=payload)
  14. assert resp.status_code == 200, f"login failed: {resp.status_code}"
  15. self.Session.headers.setdefault("Accesstoken", resp.json()["data"]["token"])
  16. def listIds(self):
  17. url = "http://119.3.44.183:8016/manage2/eartagRegister/listUserEartagData"
  18. payload = {"pageNo": 1, "pageSize": 20, "earmark": "", "status": "", "farmId": "330110004"}
  19. for i in range(133): # 2624 = 131 * 20 + 4
  20. payload["pageNo"] = i + 1
  21. try:
  22. resp = self.Session.post(url=url, json=payload)
  23. this = [(itr["earmark"], itr["id"]) for itr in resp.json()["data"]["records"]]
  24. self.ids += this
  25. print(f"> page={i + 1}, count={len(this)}")
  26. except Exception as e:
  27. print(f"> ERROR: page={i + i}\n", e)
  28. time.sleep(0.5)
  29. break # first 20 only
  30. fp = open("raw-data/ear-tags.json", "w", encoding="utf-8")
  31. json.dump(self.ids, fp)
  32. fp.close()
  33. print(f"> got {len(self.ids)} ids in raw-data/ear-tags.json")
  34. @staticmethod
  35. def timestamp(ts: "str") -> "int":
  36. return int(time.mktime(time.strptime(ts, "%Y-%m-%d %H:%M:%S")))
  37. @staticmethod
  38. def notNull(data):
  39. return data if data is not None else 0
  40. def clean(self, data: "list[dict]", cur: "tuple") -> "list[tuple]":
  41. return [(
  42. cur[1], cur[0], self.timestamp(itr["addTime"]), itr["addTime"], self.notNull(itr["earTemp1"]),
  43. self.notNull(itr["earTemp2"]), self.notNull(itr["envTemp1"]), self.notNull(itr["act"])
  44. ) for itr in data]
  45. def fetch_one(self, tag: "str", start: "str", end: "str", _id: "int" = -1) -> "list[tuple]":
  46. url = "http://119.3.44.183:8016/manage2/eartagData/getEnvByTime"
  47. payload = {"earmark": tag, "startDate": start, "endDate": end, "farmId": self.FarmId}
  48. resp = self.Session.post(url=url, json=payload)
  49. assert resp.status_code == 200, f"> ERROR: fetch <{tag}> failed!"
  50. return self.clean(resp.json()["data"], (_id, tag))
  51. def fetch_all(self):
  52. total = len(self.ids)
  53. for (index, current) in enumerate(self.ids):
  54. try:
  55. this = self.fetch_one(current[1], "2023-11-14 00:00:00", "2024-02-08 00:00:00", current[0])
  56. self.data += this
  57. print(f"> process [{index + 1:02d}/{total}], mark={current[0]}, id={current[1]}, count={len(this)}")
  58. except Exception as e:
  59. print(f"> ERROR: mark={current[0]}, id={current[1]}\n", e)
  60. time.sleep(1)
  61. self.csv()
  62. @staticmethod
  63. def tuple2str(t: "tuple") -> "str":
  64. return f"{t[0]},{t[1]},{t[2]},{t[3]},{t[4]},{t[5]},{t[6]},{t[7]}"
  65. def csv(self):
  66. mid = [self.tuple2str(itr) for itr in self.data]
  67. data = "\n".join(mid)
  68. fp = open("raw-data/data.csv", "w", encoding="utf-8")
  69. fp.write(data)
  70. fp.close()
  71. print(f"> all data saved in raw-data/data.csv")
  72. def run(self):
  73. self.login()
  74. self.listIds()
  75. self.fetch_all()
  76. if __name__ == "__main__":
  77. fetch = Fetcher()
  78. fetch.run()