-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
123 lines (91 loc) · 3.68 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'''
Main executable Python Script.
'''
import requests as re
import json as js
import os
import datetime as dt
import base64 as bs
from requests import cookies
BASE_PATH = 'files/'
class FileReturn():
#I REAAALLLY hope I don't have to comment this:
def __init__(self, base_path):
self.BASE_PATH = base_path
#Method to read the UNIX timestamp when the files were last updated:
def timestamp_read(self):
with open('last_access.txt', 'rt') as ts:
timestamp = float(ts.read())
return timestamp
#Method to write a UNIX timestamp to a .txt file:
def timestamp_write(self):
time_stamp = dt.datetime.now().timestamp()
time_stamp = str(time_stamp)
with open('last_access.txt', 'wt') as ts:
ts.write(time_stamp)
#Method to encode the file contents in Base64 format:
def file_encode(self, input_string):
encoded_string = bs.b64encode(input_string)
return encoded_string
#Method to scrape the contents of the file:
def file_reader(self, filename):
BASE_PATH = "files/"
with open(BASE_PATH + filename, 'rb') as file:
file_bdata = file.read()
encoded_data = self.file_encode(file_bdata)
return encoded_data
#Method to construct & return the {file: filedata} dictionaries:
def file_handler(self, BASE_PATH):
#Empty dictionary to store output:
payload = {"results":[]}
#List out all the files in the given path:
files = os.listdir(BASE_PATH)
try:
for file in files:
#If the file was last modified after the last file list was updated and hence,
#the timestamp was taken:
if os.path.getmtime(BASE_PATH+file) > self.timestamp_read():
filedata = self.file_reader(file)
payload['results'].append({"file_name":file, "content_base64":filedata})
#Return the list of dictionaries -> filename: filedata:
return payload
except Exception as e:
return {'Error':f'Code #{e}. Could not retrive file data.'}
#Method to call all the functions:
def main(self):
file_tuples = self.file_handler(self.BASE_PATH)
print(file_tuples)
payload_login = {"username":"my_username", "password":"my_password"}
login_url = " https://7988-103-219-61-230.ngrok.io/login"
response = re.post(login_url, json=payload_login)
token = response.cookies.get_dict()['csrf_access_token']
#Headers for POST Request to URL.
headers = {
'accept': '*/*',
'X-CSRF-TOKEN': token,
'Content-Type': 'application/json',
}
#Parameters for POST Request to URL.
params = (
('project_id', 'files'),
('force_project_creation', 'false')
)
send_url = 'https://7988-103-219-61-230.ngrok.io/allure-docker-service/send-results'
#Cookies for POST Request to URL.
new_cookies = response.cookies
sent_response = re.post(
send_url,
headers = headers,
params = params,
json = file_tuples,
cookies = new_cookies
)
print(sent_response.content)
#Update the timestamp if request was successful:
if sent_response.status_code==200:
self.timestamp_write()
#Print the request response:
print(sent_response)
if __name__ == "__main__":
filer = FileReturn(BASE_PATH)
filer.main()