File size: 1,454 Bytes
c5cbcbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import random
from copyleaks.copyleaks import Copyleaks
from copyleaks.exceptions.command_error import CommandError
from copyleaks.models.submit.document import FileDocument
from copyleaks.models.submit.properties.scan_properties import ScanProperties

class CopyleaksClient:
    def __init__(self, email_address, key):
        self.email_address = email_address
        self.key = key
        self.auth_token = None

    def login(self):
        try:
            self.auth_token = Copyleaks.login(self.email_address, self.key)
        except CommandError as ce:
            response = ce.get_response()
            print(f"An error occurred (HTTP status code {response.status_code}):")
            print(response.content)
            exit(1)

        print("Logged successfully!\nToken:")
        print(self.auth_token)

    def submit_file(self, file_content, filename, webhook_url):
        scan_id = random.randint(100, 100000)
        BASE64_FILE_CONTENT = base64.b64encode(file_content.encode('utf-8')).decode('utf-8')
        file_submission = FileDocument(BASE64_FILE_CONTENT, filename)

        scan_properties = ScanProperties(webhook_url)
        scan_properties.set_sandbox(True)
        file_submission.set_properties(scan_properties)

        Copyleaks.submit_file(self.auth_token, scan_id, file_submission)
        print("Sent to scanning")
        print("You will be notified using your webhook once the scan is completed.")