client.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import dropbox
  2. import sys
  3. import json
  4. from pprint import pprint
  5. from hashlib import sha1
  6. import sqlite3
  7. # dropbox organization api key
  8. token = "api key"
  9. db = "{}.sqlite".format(sha1(token.encode("ascii")).hexdigest())
  10. client = dropbox.Dropbox(token)
  11. conn = sqlite3.connect(db)
  12. cur = conn.cursor()
  13. def init_db(conn, token):
  14. conn.execute('CREATE TABLE IF NOT EXISTS users (id TEXT PRIMARY KEY, team_member_id TEXT, email TEXT, external_id TEXT, name TEXT, groups TEXT)')
  15. members = client.members_list()
  16. for member in members["members"]:
  17. if "external_id" not in member["profile"]:
  18. member["profile"]["external_id"] = None
  19. cur.execute("INSERT INTO users (id, team_member_id, email, external_id, name, groups) VALUES (?, ?, ?, ?, ?, ?)", (member["profile"]["account_id"], member["profile"]["team_member_id"], member["profile"]["email"], member["profile"]["external_id"], member["profile"]["name"]["display_name"], json.dumps(member["profile"]["groups"])))
  20. conn.commit()
  21. def get_team_member_id(cur, email):
  22. return cur.execute("SELECT team_member_id FROM users where email = ?", (email,)).fetchone()
  23. def get_account_id(cur, email):
  24. return cur.execute("SELECT id FROM users where email = ?", (email,)).fetchone()
  25. def search_account(cur, query):
  26. query = "%{}%".format(query.replace(" ", "%"))
  27. return cur.execute("SELECT email,name FROM users where email like ? or name like ? or external_id like ? or id like ? or team_member_id like ?", (query, query, query, query, query)).fetchall()
  28. def get_ids(cur, email):
  29. id = get_account_id(conn, user)
  30. team_member_id = get_team_member_id(conn, user)
  31. if len(id) > 0 and len(team_member_id) > 0:
  32. id = id[0]
  33. team_member_id = team_member_id[0]
  34. return id, team_member_id
  35. else:
  36. return None, None
  37. try:
  38. cur.execute("SELECT count(id) FROM users")
  39. except:
  40. init_db(conn, token)
  41. if len(sys.argv) > 1:
  42. cmd = sys.argv[1]
  43. if cmd == "search":
  44. query = sys.argv[2]
  45. results = search_account(conn, query)
  46. if len(results) > 0:
  47. for result in results:
  48. print("{}\t\t{}".format(result[0], result[1]))
  49. elif cmd == "info":
  50. if len(sys.argv) == 2:
  51. pprint(client.get_info())
  52. elif len(sys.argv) == 3:
  53. user = user = sys.argv[2]
  54. id, team_member_id = get_ids(cur, user)
  55. pprint(client.get_current_account(team_member_id))
  56. else:
  57. user = user = sys.argv[2]
  58. id, team_member_id = get_ids(cur, user)
  59. operation = sys.argv[3]
  60. if operation == "activity":
  61. activity = client.get_events(id)
  62. for event in activity["events"]:
  63. for assets in event["assets"]:
  64. print(assets["path"]["contextual"])
  65. elif cmd == "file":
  66. user = sys.argv[2]
  67. operation = sys.argv[3]
  68. path = sys.argv[4]
  69. id, team_member_id = get_ids(cur, user)
  70. if operation == "ls":
  71. ls = client.list_folder(team_member_id, path, False)
  72. for file in ls["entries"]:
  73. print(file["path_display"])
  74. elif operation == "tree":
  75. tree = client.list_folder(team_member_id, path, True)
  76. for file in tree["entries"]:
  77. print(file["path_display"])
  78. elif operation == "download":
  79. file = client.download(team_member_id, path)
  80. filename = path.split('/')[-1]
  81. with open(filename, "wb") as f:
  82. f.write(file)
  83. elif operation == "info":
  84. print(client.get_metadata(team_member_id, path))
  85. elif cmd == "activity":
  86. # Returns the file activity of the last 10 days
  87. # This call wants the dbid (Dropbox ID) instead of the dbmid (Dropbox Team Member ID)
  88. user = sys.argv[2]
  89. id, team_member_id = get_ids(cur, user)
  90. activity = client.get_events(id)
  91. pprint(activity)
  92. for event in activity["events"]:
  93. for assets in event["assets"]:
  94. try:
  95. print(assets["path"]["contextual"])
  96. except:
  97. pass