Advertisement
Guest User

Untitled

a guest
Jun 27th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import requests, json
  2. fbheaders = {'content-type': 'application/json'}
  3.  
  4.  
  5. """
  6. Logins to a site with provided credentials, obtains blank separator and divider templates, and PUTs
  7. them into a project. Update the projectID before deploying into actual production
  8.  
  9. Also, Separators must be defined before PUTing Dividers onto the site that are assigned to a separator
  10.  
  11. """
  12.  
  13.  
  14. site = 'https://{}'.format(input('https://'))
  15. api = '{}/api/'.format(site)
  16.  
  17. #testing with a projectid of 215
  18. projectid = '215'
  19.  
  20. def login():
  21. u = input('Username: ')
  22. p = input('Password: ')
  23. data = {
  24. 'username': u,
  25. 'password': p
  26. }
  27. login = '{}login'.format(api)
  28. print(login)
  29.  
  30. try:
  31. #r = requests.post(login, data, verify=False)
  32. r = requests.post(login, data)
  33. guid = r.json()
  34. return guid
  35. except requests.exceptions.Timeout:
  36. print('Connection timed out. Please try again.')
  37. except requests.exceptions.TooManyRedirects:
  38. print('Too many redirects. Check your URL and try again.')
  39. except requests.exceptions.RequestException as e:
  40. print('Catastrophic error. Bailing.')
  41. print(e)
  42. sys.exit(1)
  43.  
  44.  
  45. def get_template(template_type):
  46. empty = '{}empty?template={}&guid={}'.format(api, template_type, guid)
  47. print(empty)
  48. r = requests.get(empty)
  49. return r.json()
  50.  
  51.  
  52. def put_divider(divider, projectid):
  53. dumped_divider = json.dumps(divider)
  54. put_string = '{}projects/{}/dividers?guid={}'.format(api, projectid, guid)
  55. print(put_string)
  56. r = requests.put(put_string, dumped_divider, headers = fbheaders)
  57. print(r.text)
  58.  
  59. def put_separator(separator, projectid):
  60. dumped_separator = json.dumps(separator)
  61. put_string = '{}projects/{}/separators?guid={}'.format(api, projectid, guid)
  62. print(put_string)
  63. r = requests.put(put_string, dumped_separator, headers = fbheaders)
  64. print(r.text)
  65.  
  66.  
  67. divider_list = ['ayy', 'bee', 'cee', 'dee', 'eee', 'eff', 'gee']
  68. separator_list = ['one', 'two', 'three']
  69.  
  70.  
  71. if __name__ == '__main__':
  72. print('Running code as main')
  73. guid = login()
  74. divider_template = get_template('divider')
  75. separator_template = get_template('separator')
  76. count = 0
  77.  
  78. for sep in separator_list:
  79. new_sep = separator_template.copy()
  80. new_sep['name'] = sep
  81. put_separator(new_sep, projectid)
  82.  
  83. for div in divider_list:
  84. new_div = divider_template.copy()
  85. new_div['name'] = div
  86. new_div['separator'] = separator_list[count%3]
  87. count += 1
  88. put_divider(new_div, projectid)
  89.  
  90.  
  91. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement