back-end/threats_monitoring/phishtank-alev.py
def extract_collection_copy(db):
    """ For a given data base, retrieves all data from a collection and export them in json and csv files
        @param
            db:     (Mongo Client)  this is the connection returned by Pymongo Client,
                                    we take it from connect_to_mongodb() function
    """

    json_filename = server_path + 'dataset-phishing.json'
    csv_filename = server_path + 'dataset-phishing.csv'
    try:
        cursor = db.threats.phishtank.find({}, {"_id": 0, "mongoDate": 0, "mongoDate-CTI": 0})

        ''' Store in json file '''
        # add a bracket [ and start inserting docs in json file
        with open(json_filename, 'w') as json_file:
            json_file.write('[\n')
            for doc in cursor:
                json.dump(doc, json_file, separators=(',', ': '), indent=4)
                json_file.write(',\n')
        # delete last comma and new line
        with open(json_filename, 'ab+') as json_file:
            json_file.seek(-3, os.SEEK_END)  # puts cursor's position over the last character of the file
            # delete last \n
            json_file.truncate()
        # insert bracket ] for a valid json
        with open(json_filename, 'a') as json_file:
            json_file.write('\n]')

        ''' Store in csv file '''
        with open(csv_filename, "w", newline='') as csvfile:
            csv_header = ["Phish-URL", "Submitted-by", "Valid?", "Online?", "DatetimeUTC", "TimestampUTC",
                          "DatetimeUTC-CTI", "TimestampUTC-CTI", "Entity-type", "Category"]
            writer = csv.DictWriter(csvfile, fieldnames=csv_header)
            writer.writeheader()
            for doc in cursor.rewind():
                writer.writerow(doc)

    except Exception as e:
            print("Unexpected error (collection export):", type(e), e)
 For a given data base, retrieves all data from a collection and export them in json and csv files
        @param
            db:     (Mongo Client)  this is the connection returned by Pymongo Client,
                                    we take it from connect_to_mongodb() function
    
For a given data base, retrieves all data from a collection and export them in json and csv files
For a given data base retrieves all data from a collection and export them in json and csv files
def extract_collection_copy(db):
    json_filename = server_path + 'dataset-phishing.json'
    csv_filename = server_path + 'dataset-phishing.csv'
    try:
        cursor = db.threats.phishtank.find({}, {"_id": 0, "mongoDate": 0, "mongoDate-CTI": 0})
        with open(json_filename, 'w') as json_file:
            json_file.write('[\n')
            for doc in cursor:
                json.dump(doc, json_file, separators=(',', ': '), indent=4)
                json_file.write(',\n')
        with open(json_filename, 'ab+') as json_file:
            json_file.seek(-3, os.SEEK_END)  
            json_file.truncate()
        with open(json_filename, 'a') as json_file:
            json_file.write('\n]')
        with open(csv_filename, "w", newline='') as csvfile:
            csv_header = ["Phish-URL", "Submitted-by", "Valid?", "Online?", "DatetimeUTC", "TimestampUTC",
                          "DatetimeUTC-CTI", "TimestampUTC-CTI", "Entity-type", "Category"]
            writer = csv.DictWriter(csvfile, fieldnames=csv_header)
            writer.writeheader()
            for doc in cursor.rewind():
                writer.writerow(doc)
    except Exception as e:
            print("Unexpected error (collection export):", type(e), e)
def extract_collection_copy ( db ) : json_filename = server_path + 'dataset-phishing.json' csv_filename = server_path + 'dataset-phishing.csv' try : cursor = db . threats . phishtank . find ( { } , { "_id" : 0 , "mongoDate" : 0 , "mongoDate-CTI" : 0 } ) ''' Store in json file ''' with open ( json_filename , 'w' ) as json_file : json_file . write ( '[\n' ) for doc in cursor : json . dump ( doc , json_file , separators = ( ',' , ': ' ) , indent = 4 ) json_file . write ( ',\n' ) with open ( json_filename , 'ab+' ) as json_file : json_file . seek ( - 3 , os . SEEK_END ) json_file . truncate ( ) with open ( json_filename , 'a' ) as json_file : json_file . write ( '\n]' ) ''' Store in csv file ''' with open ( csv_filename , "w" , newline = '' ) as csvfile : csv_header = [ "Phish-URL" , "Submitted-by" , "Valid?" , "Online?" , "DatetimeUTC" , "TimestampUTC" , "DatetimeUTC-CTI" , "TimestampUTC-CTI" , "Entity-type" , "Category" ] writer = csv . DictWriter ( csvfile , fieldnames = csv_header ) writer . writeheader ( ) for doc in cursor . rewind ( ) : writer . writerow ( doc ) except Exception as e : print ( "Unexpected error (collection export):" , type ( e ) , e )
For a given data base, retrieves all data from a collection and export them in json and csv files
For a given data base retrieves all data from a collection and export them in json and csv files
""" For a given data base, retrieves all data from a collection and export them in json and csv files
        @param
            db:     (Mongo Client)  this is the connection returned by Pymongo Client,
                                    we take it from connect_to_mongodb() function
    """ ''' Store in json file ''' # add a bracket [ and start inserting docs in json file
 # delete last comma and new line
 # puts cursor's position over the last character of the file
 # delete last \n
 # insert bracket ] for a valid json
 ''' Store in csv file '''
db
(Mongo Client)  this is the connection returned by Pymongo Client,
we take it from connect_to_mongodb() function
( Mongo Client ) this is the connection returned by Pymongo Client we take it from connect_to_mongodb () function