api.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #! /usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # vi:ts=4:et
  4. # Trello API Python CLI
  5. # License: MIT / WeKan Team
  6. try:
  7. # python 3
  8. from urllib.parse import urlencode
  9. from urllib.request import urlretrieve
  10. except ImportError:
  11. # python 2
  12. from urllib import urlencode
  13. import json
  14. import requests
  15. import sys
  16. # ------- TODO START -------------
  17. #
  18. # - Check nested resources about how to recursively get all reactins etc:
  19. # https://developer.atlassian.com/cloud/trello/guides/rest-api/nested-resources/
  20. # - Add checking status codes and stop/delay if errors in API.
  21. # If board is big, instead get small amount of board with paging of Trello REST API,
  22. # then have small delay, and then get more of that big amount of data, so that
  23. # there would not be timeouts with too much data
  24. # https://developer.atlassian.com/cloud/trello/guides/rest-api/status-codes/
  25. # - Add batch requests, to get enough data at once:
  26. # https://developer.atlassian.com/cloud/trello/rest/api-group-batch/#api-batch-get
  27. # - Add rate limits with delays:
  28. # https://developer.atlassian.com/cloud/trello/guides/rest-api/rate-limits/
  29. # - Use webhooks to receive data from Trello to WeKan, so that there would not be
  30. # need to repeatedly get same data again (no polling data), but instead get
  31. # changes pushed to WeKan with webhooks when they happen
  32. # https://developer.atlassian.com/cloud/trello/guides/rest-api/webhooks/
  33. # https://developer.atlassian.com/cloud/trello/rest/api-group-webhooks/#api-webhooks-post
  34. #
  35. # ------- TODO END -------------
  36. # ------- TRELLO SETTINGS START -------------
  37. #
  38. # READ ABOVE TODO FIRST, BE CAREFUL WITH RATE LIMITS ETC.
  39. #
  40. # Keys and tokens:
  41. # - See API introduction:
  42. # https://developer.atlassian.com/cloud/trello/guides/rest-api/api-introduction/
  43. # - Get developer API key and create token at top of https://trello.com/app-key
  44. #
  45. key = 'TRELLO-API-KEY-HERE'
  46. token = 'TRELLO-API-TOKEN-HERE'
  47. #
  48. # ------- TRELLO SETTINGS END -------------
  49. arguments = len(sys.argv) - 1
  50. if arguments == 0:
  51. print("=== Trello API Python CLI ===")
  52. print("License: MIT / WeKan Team")
  53. print("See settings in this api.py script for api key and token.")
  54. print("If *nix: chmod +x api.py => ./api.py users")
  55. print("Syntax:")
  56. print(" python3 api.py emoji # List all available emoji")
  57. print(" python3 api.py boards # List All Boards")
  58. print(" python3 api.py board BOARDID # Info of BOARDID")
  59. print(" python3 api.py card CARDID # Info of CARDID")
  60. print(" python3 api.py actions BOARDID # Actions of BOARDID")
  61. print(" python3 api.py reactions ACTIONID # Reactions of ACTIONID")
  62. print(" python3 api.py attachments CARDID # List attachments of CARDID")
  63. print(" python3 api.py download ATTACHMENTURL # Download file from attachment URL like https://.../image.png with streaming and minimal RAM usage")
  64. exit
  65. if arguments == 2:
  66. if sys.argv[1] == 'board':
  67. # ------- BOARD START -----------
  68. #headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  69. headers = {'Accept': 'application/json'}
  70. boardid = sys.argv[2]
  71. print("=== ONE BOARD ===\n")
  72. listboard = 'https://api.trello.com/1/boards/' + boardid + '?key=' + key + '&token=' + token
  73. body = requests.get(listboard, headers=headers)
  74. data2 = body.text.replace('}',"}\n")
  75. print(data2)
  76. # ------- BOARD END -----------
  77. if sys.argv[1] == 'card':
  78. # ------- CARD START -----------
  79. headers = {'Accept': 'application/json'}
  80. cardid = sys.argv[2]
  81. print("=== ONE CARD ===\n")
  82. listcard = 'https://api.trello.com/1/cards/' + cardid + '?fields=all&key=' + key + '&token=' + token
  83. body = requests.get(listcard, headers=headers)
  84. data2 = body.text.replace('}',"}\n")
  85. print(data2)
  86. # ------- BOARD END -----------
  87. if sys.argv[1] == 'actions':
  88. # ------- BOARD ACTIONS START -----------
  89. headers = {'Accept': 'application/json'}
  90. boardid = sys.argv[2]
  91. print("=== ONE BOARD ACTIONS ===\n")
  92. listboardactions = 'https://api.trello.com/1/boards/' + boardid + '/actions?key=' + key + '&token=' + token
  93. body = requests.get(listboardactions, headers=headers)
  94. data2 = body.text.replace('}',"}\n")
  95. print(data2)
  96. # ------- BOARD ACTIONS END -----------
  97. if sys.argv[1] == 'reactions':
  98. # ------- REACTIONS OF ACTIONID START -----------
  99. headers = {'Accept': 'application/json'}
  100. actionid = sys.argv[2]
  101. print("=== REACTIONS OF ACTIONID ===\n")
  102. listreactions = 'https://api.trello.com/1/actions/' + actionid + '/reactionsSummary?key=' + key + '&token=' + token
  103. body = requests.get(listreactions, headers=headers)
  104. data2 = body.text.replace('}',"}\n")
  105. print(data2)
  106. # ------- REACTIONS OF ACTIONID END -----------
  107. if sys.argv[1] == 'attachments':
  108. # ------- LIST CARD ATTACHMENTS START -----------
  109. headers = {'Accept': 'application/json'}
  110. cardid = sys.argv[2]
  111. print("=== LIST CARD ATTACHMENTS ===\n")
  112. listcardattachments = 'https://api.trello.com/1/cards/' + cardid + '/attachments?key=' + key + '&token=' + token
  113. body = requests.get(listcardattachments, headers=headers)
  114. data2 = body.text.replace('}',"}\n")
  115. print(data2)
  116. # ------- LIST CARD ATTACHMENTS END -----------
  117. if sys.argv[1] == 'download':
  118. # ------- DOWNLOAD BOARD ATTACHMENT START -----------
  119. headers = {'Accept': 'application/json', 'Authorization': 'OAuth oauth_consumer_key="' + key + '", oauth_token="' + token + '"'}
  120. url = sys.argv[2]
  121. print("=== DOWNLOAD BOARD ATTACHMENT ===\n")
  122. local_filename = url.split('/')[-1]
  123. # NOTE the stream=True parameter below. Does streaming download with minimal RAM usage.
  124. with requests.get(url, stream=True, headers=headers) as r:
  125. r.raise_for_status()
  126. with open(local_filename, 'wb') as f:
  127. for chunk in r.iter_content(chunk_size=8192):
  128. # If you have chunk encoded response uncomment if
  129. # and set chunk_size parameter to None.
  130. #if chunk:
  131. f.write(chunk)
  132. print("filename: " + local_filename + "\n")
  133. # ------- DOWNLOAD BOARD ATTACHMENT END -----------
  134. if arguments == 1:
  135. if sys.argv[1] == 'boards':
  136. # ------- LIST OF BOARDS START -----------
  137. headers = {'Accept': 'application/json'}
  138. print("=== BOARDS ===\n")
  139. listboards = 'https://api.trello.com/1/members/me/boards?key=' + key + '&token=' + token
  140. body = requests.get(listboards, headers=headers)
  141. data2 = body.text.replace('}',"}\n")
  142. print(data2)
  143. # ------- LIST OF BOARDS END -----------
  144. if sys.argv[1] == 'emoji':
  145. # ------- LIST OF EMOJI START -----------
  146. headers = {'Accept': 'application/json'}
  147. print("=== LIST OF ALL EMOJI ===\n")
  148. listemoji = 'https://api.trello.com/1/emoji?key=' + key + '&token=' + token
  149. body = requests.get(listemoji, headers=headers)
  150. data2 = body.text.replace('}',"}\n")
  151. print(data2)
  152. # ------- LIST OF EMOJI END -----------