archive_group_1.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. def archive_group(groupName, mode="update"):
  2. log("\nArchiving group '" + groupName + "', mode: " + mode + " , on " + time.strftime("%c"), groupName)
  3. startTime = time.time()
  4. msgsArchived = 0
  5. if mode == "retry":
  6. #don't archive any messages we already have
  7. #but try to archive ones that we don't, and may have
  8. #already attempted to archive
  9. min = 1
  10. elif mode == "update":
  11. #start archiving at the last+1 message message we archived
  12. mostRecent = 1
  13. if os.path.exists(groupName):
  14. oldDir = os.getcwd()
  15. os.chdir(groupName)
  16. for file in glob.glob("*.json"):
  17. if int(file[0:-5]) > mostRecent:
  18. mostRecent = int(file[0:-5])
  19. os.chdir(oldDir)
  20. min = mostRecent
  21. elif mode == "restart":
  22. #delete all previous archival attempts and archive everything again
  23. if os.path.exists(groupName):
  24. shutil.rmtree(groupName)
  25. min = 1
  26. else:
  27. print ("You have specified an invalid mode (" + mode + ").")
  28. print ("Valid modes are:\nupdate - add any new messages to the archive\nretry - attempt to get all messages that are not in the archive\nrestart - delete archive and start from scratch")
  29. sys.exit()
  30. if not os.path.exists(groupName):
  31. os.makedirs(groupName)
  32. max = group_messages_max(groupName)
  33. for x in range(min,max+1):
  34. if not os.path.isfile(groupName + '/' + str(x) + ".json"):
  35. print ("Archiving message " + str(x) + " of " + str(max))
  36. sucsess = archive_message(groupName, x)
  37. if sucsess == True:
  38. msgsArchived = msgsArchived + 1
  39. log("Archive finished, archived " + str(msgsArchived) + ", time taken is " + str(time.time() - startTime) + " seconds", groupName)