def getAudioExtension(base64str): b = base64.b64decode(base64str) fout = open("tempaudio", "wb") fout.write(b) fout.close() with open("tempaudio", "rb") as file: info = fleep.get(file.read(128)) return info.extension
def f(self, p): for x in p.glob('*'): if x.is_file(): with open(x, "rb") as file: info = fleep.get(file.read(128)) r = json.dumps(info.__dict__) print(x.name, r) if x.is_dir(): self.f(Path(x))
def action(graph, namespaces, node_path): for node, path in node_path.items(): try: with open(path, 'rb') as f: info = fleep.get(f.read(128)) type, extension = info.type[0], info.extension[0] graph.add( (node, namespaces['a'], namespaces['nfo'] + type.title())) graph.set((node, namespaces[''] + 'fileFormat', namespaces[''] + extension.upper())) except IndexError as e: print( "ERROR: Agent file_format could not guess type or extension for", path) continue
def ask_user(library): folder = easygui.diropenbox(default=library) locations = [] for subdir, _, files in os.walk(folder): for file in files: location = os.path.join(subdir, file) with open(location, "rb") as file: info = fleep.get(file.read(128)) if 'audio' in info.type: locations.append(location) print(locations) return locations
def file_is_text(fname): try: with open(fname, "rb") as file: info = fleep.get(file.read(128)) file.close() except: print ('Le fichier "' + fname + '" n\'existe pas.') return 0 # print(info.type) # print(info.extension) # print(info.mime) if (len(info.type) == 0): return 1 else: print ('Erreur: Le fichier ne semble pas être un fichier texte.') return 0
def store_image_data(image_data: str) -> str: image_string = base64.b64decode(image_data) file_extensions = fleep.get(image_string).extension allowable_extensions = ['png', 'jpg'] if len(file_extensions ) == 0 or file_extensions[0] not in allowable_extensions: raise BadFormatException( f"file should be one of these: {', '.join(allowable_extensions)}") post_key = generate_post_key(image_data) file_path = os.path.join(dir_path, 'content_storage', f'{post_key}.{file_extensions[0]}') with open(file_path, 'wb') as f: f.write(image_string) return post_key, file_extensions[0]
def test_xlsx_extension_libre(): """ Test a xlsx datafile with a comment section, labels, dates, time and floats. Saved with LibreOffice. """ test_file = os.path.join(TEST_DIR, 'input_files', 'data.xlsx') with open(test_file, "rb") as file: info = fleep.get(file.read(128)) assert info.type == ['document', 'archive', 'executable'] assert info.extension == ['pages', 'key', 'numbers', 'epub', 'zip', 'jar'] assert info.mime == [ 'application/zip', 'application/epub+zip', 'application/java-archive' ]
def data(self, value): if isinstance(value, bytes): config_parser = get_config_parser("environment.ini") path = config_parser["paths"]["images"] if not os.path.exists(path): os.mkdir(path) name = str(uuid.uuid1()) format = fleep.get(value).extension[0] full_path = f"{path}{name}.{format}" with open(full_path, "wb+") as f: f.write(value) self._data = full_path elif value is None: self._data = None else: raise TypeError("Must provide bytes as data")
def send_ask(self, file_path, icon=None): ask_body = { 'SenderComputerName': self.config.computer_name, 'BundleID': 'com.apple.finder', 'SenderModelName': self.config.computer_model, 'SenderID': self.config.service_id, 'ConvertMediaFormats': False, } if self.config.legacy: ask_body['SenderEmailHash'] = AirDropUtil.doubleSHA1Hash( self.config.email) ask_body['SenderPhoneHash'] = AirDropUtil.doubleSHA1Hash( self.config.phone) if self.config.record_data: ask_body['SenderRecordData'] = self.config.record_data if isinstance(file_path, str): file_path = [file_path] # generate icon for first file with open(file_path[0], 'rb') as f: file_header = f.read(128) flp = fleep.get(file_header) if not icon and len(flp.mime) > 0 and 'image' in flp.mime[0]: icon = AirDropUtil.generate_file_icon(f.name) if icon: ask_body['FileIcon'] = icon def file_entries(files): for file in files: file_name = os.path.basename(file) file_entry = { 'FileName': file_name, 'FileType': AirDropUtil.get_uti_type(flp), 'FileBomPath': os.path.join('.', file_name), 'FileIsDirectory': os.path.isdir(file_name), 'ConvertMediaFormats': 0 } yield file_entry ask_body['Files'] = [e for e in file_entries(file_path)] ask_body['Items'] = [] ask_binary = plistlib.dumps(ask_body, fmt=plistlib.FMT_BINARY) success, _ = self.send_POST('/Ask', ask_binary) return success
def rename(episodes): i = 0 #folderpath defaults to where program is run, otherwise grabs first input arg folderpath = os.path.dirname(os.path.abspath(__file__)) if sys.argv[1] is not None: folderpath = sys.argv[1] try: files = os.listdir(folderpath) except FileNotFoundError: print("ERROR: the given file path could not be found") print("check the following path is correct: ") print(str(folderpath)) sys.exit() files.sort() #grabs video files for filename in files: #checking if object is directory as fleep cannot handle directories if os.path.isfile(os.path.join(folderpath, filename)): with open(os.path.join(folderpath, filename), "rb") as file: info = fleep.get(file.read(128)) #print("filetype: ",info.type) #debug #fleep sometimes alters the file extension #so keeping the original file extension using os module fileExtension = os.path.splitext(filename)[1] #some smaller files or odd extensions don't have an file type if (len(info.type)) > 0: if info.type[0] == 'video': #print(info.type[0]) #debug #print("episode " + str(i+1)) #debug if i + 1 > len(episodes): #bounds checking print("filename: ", filename) return else: print("filename: ", filename, end=' --> ') #debug os.rename( os.path.join(folderpath, filename), os.path.join( folderpath, str(i + 1) + '_' + episodes[i] + str(fileExtension))) print(episodes[i] + str(i + 1) + '_' + str(fileExtension)) i = i + 1
def upload(attachment, id=None): logging.info("Creating Media ") f = connexion.request.files["attachment"] filename = secure_filename(f.filename) uid = uuid.uuid4().hex ext = Path(f.filename).suffix path = "./static/uploads/{}{}".format(uid, ext) f.save(path) with open(path, "rb") as f: info = fleep.get(f.read(128)) print(info.type) m = Media(id, path, filename, info.type[0]) # name = os.path.basename(path) db_session.add(m) db_session.commit() print(m.id) return m.dump(), 201
def send_ask(self, file_path, icon=None): ask_body = { "SenderComputerName": self.config.computer_name, "BundleID": "com.apple.finder", "SenderModelName": self.config.computer_model, "SenderID": self.config.service_id, "ConvertMediaFormats": False, } if self.config.record_data: ask_body["SenderRecordData"] = self.config.record_data if isinstance(file_path, str): file_path = [file_path] # generate icon for first file with open(file_path[0], "rb") as f: file_header = f.read(128) flp = fleep.get(file_header) if not icon and len(flp.mime) > 0 and "image" in flp.mime[0]: icon = AirDropUtil.generate_file_icon(f.name) if icon: ask_body["FileIcon"] = icon def file_entries(files): for file in files: file_name = os.path.basename(file) file_entry = { "FileName": file_name, "FileType": AirDropUtil.get_uti_type(flp), "FileBomPath": os.path.join(".", file_name), "FileIsDirectory": os.path.isdir(file_name), "ConvertMediaFormats": 0, } yield file_entry ask_body["Files"] = [e for e in file_entries(file_path)] ask_body["Items"] = [] ask_binary = plistlib.dumps( ask_body, fmt=plistlib.FMT_BINARY # pylint: disable=no-member ) success, _ = self.send_POST("/Ask", ask_binary) return success
def identify_file_type(name_of_file): global name_of_dir fname, fext = os.path.splitext(name_of_file) with open(name_of_file, "rb") as file: info = fleep.get(file.read(128)) if (info.extension_matches(fext[1:])): for i in sub_dir: flag = 0 for j in info.mime: if j.find(i) != -1: flag = 1 name_of_dir = i print("directory name is", name_of_dir) break if flag == 1: break else: name_of_dir = fext[1:]
def get_filetype(fname): '''Takes a file name and returns its MIME type.''' """ # brief output, MIME version file_args = ['file', '-b'] if sys.platform == 'darwin': file_args.append('-I') else: file_args.append('-i') file_args.append(fname) # return one item per line p_file = sp.Popen(file_args, stdout=sp.PIPE) return p_file.communicate()[0].strip() """ with open(fname, "rb") as file: info = fleep.get(file.read(128)) return info.mime
def set_announce_sound(discord_id, sound_file): identity = Schema(database_url) identity.register_event(discord_id, "ANNOUNCE_SOUND_SET", ip_addr=request.remote_addr) identity.close() # Check file isn't too big sound_file.seek(0, os.SEEK_END) size = sound_file.tell() if size > 500000: raise BadRequest("Uploaded file is too large!") # Read file sound_file.seek(0) sound_bytes = sound_file.read() # Verify file contents info = fleep.get(sound_bytes) mime = info.mime[0] if info.mime else None if mime == "audio/mpeg": extension = "mp3" elif mime == "audio/ogg": extension = "ogg" else: raise UnsupportedMediaType( "Uploaded file must be audio/mpeg or audio/ogg!") # Check audio length temp_path = path.join(directory_sounds, f"{discord_id}_temp.{extension}" ) # TinyTag sucks and only takes filenames with open(temp_path, "wb") as temp_file: temp_file.write(sound_bytes) tag = TinyTag.get(temp_path) if tag.duration > 5: raise BadRequest("Uploaded sound is too long!") # Remove any existing file for ext in sound_extensions: try: os.remove(path.join(directory_sounds, f"{discord_id}.{ext}")) except FileNotFoundError: pass # Make our temporary file permanent! os.rename(temp_path, path.join(directory_sounds, f"{discord_id}.{extension}"))
def readFiles(self, folderPath): # Array holding all the paths result = [] print("Path is: " + folderPath) # Recursively walk through folders for root, dirs, files in os.walk(folderPath): for file in files: # Get the absolute path absolPath = os.path.join(root, file) try: # Check whether the file is an audio file with open(absolPath, "rb") as f: fleepInfo = fleep.get(f.read(128)) # If it is if (fleepInfo.type_matches("audio")): print("Found audio: " + absolPath) # Add to result array result.append(absolPath) except: # Check for potential errors print("got some error for this file: " + absolPath) # Return array with all paths. return result
def test_xlsx_extension_ms(): """ Test a xlsx datafile with a comment section, labels, dates, time and floats. Saved with MS. """ test_file = os.path.join(TEST_DIR, 'input_files', 'data_ms.xlsx') with open(test_file, "rb") as file: info = fleep.get(file.read(128)) assert info.type == ['document', 'archive', 'executable'] assert info.extension == [ 'doc', 'pps', 'ppt', 'xls', 'docx', 'pptx', 'xlsx', 'pages', 'key', 'numbers', 'epub', 'zip', 'jar' ] assert info.mime == [ 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/epub+zip', 'application/java-archive' ]
def MagicParser(targetFile): # Defining tables extTable = PrettyTable() extTable.field_names = [f"Extracted {green}File Extensions{white}"] mimTable = PrettyTable() mimTable.field_names = [f"Extracted {green}Mime Types{white}"] filTable = PrettyTable() filTable.field_names = [f"Extracted {green}File Types{white}"] # Getting data from file with open(targetFile, "rb") as scope: extract = fl.get(scope.read(128)) # Defining lists extensions = list(extract.extension) mimeTypes = list(extract.mime) fileTypes = list(extract.type) # For file extensions if extensions != []: for ex in extensions: extTable.add_row([f"{red}{ex}{white}"]) print(extTable) # For mime types if mimeTypes != []: for mt in mimeTypes: mimTable.add_row([f"{red}{mt}{white}"]) print(mimTable) # For file types if fileTypes != []: for ft in fileTypes: filTable.add_row([f"{red}{ft}{white}"]) print(filTable)
def upload_post(bot: Bot, nodes, args): def download_media(url): data = urllib.request.urlopen(url).read() print(fleep.get(data[:300]).extension) return data def binary_data(node): is_url = isinstance(node, Arg) and 'http' in node.value is_media = isinstance(node, Media) is_path = isinstance(node, Arg) and not 'http' in node.value switch = { is_url: lambda: download_media(node.value), is_path: lambda: load(node.value), is_media: lambda: download_media(node.images[0]), } return switch[True]() def get_geotag_data(name): return bot.api.location_search(bot.latitude, bot.longitude, query=name,)['venues'][0] max = args.get('max') or 1 caption = args.get('caption') or '' geotag = args.get('geotag') disable_comments = bool(args.get('disable_comments')) nodes = take(max, nodes) if len(nodes) == 0: bot.logger.error('no medias to upload') if max == 1: # only 1 media node = nodes[0] # print(node.carousel_media) # print(node.sources[0]) kwargs = dict( caption=caption, to_reel=False, ) if geotag: kwargs.update(dict(location=get_geotag_data(geotag),)) if disable_comments is not None: kwargs.update(dict(disable_comments=disable_comments)) data = binary_data(node) with temporary_write(data) as path: extensions = fleep.get(data[:128]).extension ext = extensions[0] if len(extensions) > 0 else None if ext in SUPPORTED_IMAGE_EXT: bot.logger.info('uploading img') kwargs.update(make_photo_args(data, path)) res = bot.api.post_photo(**kwargs) uploaded_media = Media(**res.get('media',{})) # print(json.dumps(res, indent=4)) elif ext in SUPPORTED_VIDEO_EXT: bot.logger.info('uploading video') kwargs.update(make_video_args(data, path)) res = bot.api.post_video(**kwargs) uploaded_media = Media(**res.get('media',{})) else: raise RuntimeError(f'unsupportd media type {fleep.get(data[:128]).extension} for {node}') bot.logger.info(f'uploaded media {uploaded_media}') return [uploaded_media], {} else: # album upload uploads= [] for node in nodes: data = binary_data(node) with temporary_write(data) as path: extensions = fleep.get(data[:128]).extension ext = extensions[0] if len(extensions) > 0 else None if ext in SUPPORTED_IMAGE_EXT: uploads.append(make_photo_args(data, path)) elif ext in SUPPORTED_VIDEO_EXT: uploads.append(make_video_args(data, path)) else: raise RuntimeError(f'unsupportd media type {fleep.get(data[:128]).extension} for {node}') kwargs = dict( caption=caption, medias=uploads, ) if geotag: kwargs.update(dict(location=get_geotag_data(geotag),)) # if disable_comments is not None: # kwargs.update(dict(disable_comments=bool(disable_comments))) res = bot.api.post_album(**kwargs) uploaded_media = Media(**res.get('media',{})) bot.logger.info(f'uploaded album {uploaded_media}') return [uploaded_media], {}
The **data_file** is linked to the containing **project** """ data_file = {} data_file['data'] = {} data_file['data']['type'] = 'data_files' data_file['data']['attributes'] = {} data_file['data']['attributes']['title'] = input( 'Please enter the name for the data_file: ') """Read the filepath to the local file and determine its mime_type""" local_filepath = os.path.abspath( input('Please enter the path to the local file: ')) local_file = open(local_filepath, mode='rb') info = fleep.get(local_file.read(128)) mime_type = None if info.mime: mime_type = info.mime[0] """Create a placeholder for the local data""" local_blob = { 'original_filename': os.path.basename(local_filepath), 'content_type': mime_type } """Update the **data_file** with the placeholder""" data_file['data']['attributes']['content_blobs'] = [local_blob] data_file['data']['relationships'] = {} data_file['data']['relationships']['projects'] = {}
def generate_videos_from_events(response, video_model): """Creates the video containers/representations for this given response. We should only really invoke this as part of a migration as of right now (2/8/2019), but it's quite possible we'll have the need for dynamic upsertion later. """ seen_ids = set() video_objects = [] Video = video_model # Using a constructive approach here, but with an ancillary seen_ids list b/c Django models without # primary keys are unhashable for some dumb reason (even though they have unique fields...) for frame_id, event_data in response.exp_data.items(): if event_data.get("videoList", None) and event_data.get( "videoId", None): # We've officially captured video here! events = event_data.get("eventTimings", []) for event in events: video_id = event["videoId"] pipe_name = event[ "pipeId"] # what we call "ID" they call "name" stream_time = event["streamTime"] if (video_id not in seen_ids and pipe_name and stream_time and stream_time > 0): # Try looking for the regular ID first. file_obj = S3_RESOURCE.Object(settings.BUCKET_NAME, f"{video_id}.mp4") try: s3_response = file_obj.get() except ClientError: try: # If that doesn't work, use the pipe name. file_obj = S3_RESOURCE.Object( settings.BUCKET_NAME, f"{pipe_name}.mp4") s3_response = file_obj.get() except ClientError: logger.warning( f"could not find {video_id} or {pipe_name} in S3!" ) continue # Read first 32 bytes from streaming body (file header) to get actual filetype. streaming_body = s3_response["Body"] file_header_buffer: bytes = streaming_body.read(32) file_info = fleep.get(file_header_buffer) streaming_body.close() video_objects.append( Video( pipe_name=pipe_name, created_at=date_parser.parse(event["timestamp"]), date_modified=s3_response["LastModified"], # Can't get the *actual* pipe id property, it's in the webhook payload... frame_id=frame_id, full_name=f"{video_id}.{file_info.extension[0]}", study=response.study, response=response, is_consent_footage=frame_id in VALID_CONSENT_FRAMES, )) seen_ids.add(video_id) return Video.objects.bulk_create(video_objects)
def fleepget(source): return fleep.get(read_bytes(source))
def test_one(self): driver = self.driver current1 = strftime("%Y-%m-%d %H:%M:%S") if driver.name == 'chrome': driver.maximize_window() driver.get(self.base_url) driver.find_element_by_name('q').click() driver.find_element_by_name('q').send_keys("pixitmedia" + Keys.ENTER) list_pixitmedia = driver.find_elements_by_xpath("//*[contains(text(),'Pixit Media')]") self.assertGreaterEqual(len(list_pixitmedia), 1) # OLD code - not working anymore # for element in list_pixitmedia: # if element.text == "https://www.pixitmedia.com/": # element.click() # if driver.find_element_by_xpath("//*[contains(text(),'https://www.pixitmedia.com/')]").text == "https://www.pixitmedia.com/": # driver.get("https://www.pixitmedia.com/") list_pixitmedia[0].click() basepixitmediaurl = driver.current_url print('PIXITMEDIA URL = ' + str(basepixitmediaurl)) elements_pixstor = driver.find_elements_by_xpath("//*[contains(text(),'PixStor')]") self.assertGreaterEqual(len(elements_pixstor), 1) element_pixstor = elements_pixstor[0] hoover(driver).move_to_element(element_pixstor).perform() features_el = driver.find_element_by_xpath("//*[contains(text(),'Features')]") hoover(driver).move_to_element(features_el).perform() powersearch_el = driver.find_element_by_xpath("//*[contains(text(),'Powerful Search')]") hoover(driver).move_to_element(powersearch_el).perform() powersearch_el.click() driver.get(basepixitmediaurl) element_comp = driver.find_elements_by_xpath("//*[contains(text(),'Company')]")[0] hoover(driver).move_to_element(element_comp).perform() resc_el = driver.find_element_by_xpath("//*[contains(text(),'Resources')]") hoover(driver).move_to_element(resc_el).perform() resc_el.click() if driver.find_elements_by_xpath("//*[contains(text(),'Data Sheets')]"): driver.execute_script("window.scrollTo(0, " + str(self.displayheight()[2]) + ");") driver.find_elements_by_xpath("//*[contains(text(),'Data Sheets')]")[0].click() else: driver.find_elements_by_xpath("//*[contains(text(),'Data Sheets')]")[0].click() driver.execute_script("window.scrollTo(0, " + str(self.displayheight()[1]) + ");") sleep(2) pixstor_overview = driver.find_elements_by_xpath("//*[contains(text(),'PixStor Overview')]") if len(pixstor_overview) >= 1: print('PixStor Overview PDF FOUND!!!') print('Element text = ' + str(pixstor_overview[0].text)) pixstor_overview[0].click() # View Datasheet - PDF file if driver.find_element_by_xpath("//*[contains(text(),'DOWNLOAD')]"): driver.execute_script("window.scrollTo(0, " + str(self.displayheight()[2]) + ");") pdffileurl = driver.find_element_by_xpath("//*[contains(text(),'DOWNLOAD')]").get_attribute('href') print('PDF FILE URL = ' + str(pdffileurl)) # validate PDF file wget.download(pdffileurl, out=self.tmpfilename) file1 = open(self.tmpfilename, "rb") chkfile = fleep.get(file1.read(128)) # print('TYPE = ' + str(chkfile.type)) print('EXTN = ' + str(chkfile.extension)) print('MIME = ' + str(chkfile.mime)) self.assertIn('pdf', str(chkfile.mime)) file1.close() else: print('PixStor Overview PDF NOT FOUND') # Contact Us Form driver.get(basepixitmediaurl) driver.find_element_by_xpath("//*[contains(text(),'Contact Us')]").click() # print("URL Contact Us = " + str(driver.current_url)) driver.execute_script("window.scrollTo(0, " + str(self.displayheight()[1]) + ");") # All data to send name1 = 'Cromox' name2 = 'One' email1 = '*****@*****.**' comp1 = 'Ranorexxx' phone1 = "07769916425" subject1 = 'Test1' print('NAME : ' + name1 + ' ' + name2) print('EMAIL : ' + email1 + ' / PHONE : ' + phone1) print('COMPANY : ' + comp1) print('SUBJECT : ' + str(subject1)) # fill-in the form element_firstname = driver.find_element_by_name('input_1.3') hoover(driver).move_to_element(element_firstname).perform() element_firstname.clear() element_firstname.send_keys(name1) element_lastname = driver.find_element_by_id("input_2_1_6") hoover(driver).move_to_element(element_lastname).perform() # element_lastname.click() element_lastname.clear() element_lastname.send_keys(name2) element_email = driver.find_element_by_id("input_2_2") hoover(driver).move_to_element(element_email).perform() # element_email.click() element_email.clear() element_email.send_keys(email1) element_company = driver.find_element_by_id("input_2_3") hoover(driver).move_to_element(element_company).perform() element_company.clear() element_company.send_keys(comp1) element_phone = driver.find_element_by_id("input_2_7") hoover(driver).move_to_element(element_phone).perform() element_phone.clear() element_phone.send_keys(phone1) element_subject = driver.find_element_by_id("input_2_4") hoover(driver).move_to_element(element_subject).perform() element_subject.clear() element_subject.send_keys(subject1) element_message = driver.find_element_by_id("input_2_6") hoover(driver).move_to_element(element_message).perform() element_message.clear() current2 = strftime("%Y-%m-%d %H:%M:%S") message1 = "This is a test for Jez \n\nToday is " + str(current2) + \ '\nExact testing time : From ' + str(current1) + ' to ' + str(current2) + \ "\nThis Earth of Mankind (Bumi Manusia) -> is it Jez?? " \ "\nI don't think so. Look at the inconsiderate you!!" print('MESSAGE :\n' + str(message1)) element_message.send_keys(message1) # Half page scroll down driver.execute_script("window.scrollTo(0, " + str(self.displayheight()[1]) + ");") driver.find_element_by_name('input_8.1').click() element_send = driver.find_element_by_xpath("//*[@value='Send']") self.assertIsNotNone(element_send) hoover(driver).move_to_element(element_send).perform() element_send.click()
def type_file(file): with open(file, "rb") as file: info = fleep.get(file.read(128)) # print('111',info.extension) return info.extension
# tmpfilename = 'PixStor-Overview-1219-final.pdf' tmpfilename = 'tmptest2345.pdf' import fleep file1 = open(tmpfilename, "rb") baca = fleep.get(file1.read(128)) print('TYPE = ' + str(baca.type)) print('EXTN = ' + str(baca.extension)) print('MIME = ' + str(baca.mime)) file1.close()
def process(self): if self.list_langs: tool = self._get_tool() avail_langs = tool.get_available_languages() if self.output_format == 'json': self.msg(json.dumps({'langs': avail_langs})) else: self.msg('Available Languages:') self.msg(', '.join(avail_langs)) return True elif self.convert: if Path(self.input).is_file(): with open(self.input, "rb") as file: info = fleep.get(file.read(128)) is_raster_image = info.type_matches('raster-image') is_pdf = info.extension_matches('pdf') if not is_raster_image and not is_pdf: if len(info.mime) > 0: type = str(info.mime[0]) else: type = 'Unknown' raise AttributeError('Unsupported input file type: ' + type) tool = self._get_tool() FINAL_TEXT = [] if is_raster_image: txt = tool.image_to_string( PI.open(self.input), lang=self.lang, builder=pyocr.builders.LineBoxBuilder() ) FINAL_TEXT.append(txt) elif is_pdf: self.msg('TODO') RESULT_OBJ = [] for a in FINAL_TEXT: for b in a: line = { 'content': b.content, 'position': b.position, 'wordBoxes': [] } for wb in b.word_boxes: line['wordBoxes'].append({ 'content': wb.content, 'position': wb.position, 'confidence': wb.confidence }) RESULT_OBJ.append(line) self.msg(json.dumps({'result': RESULT_OBJ})) return True else: raise AttributeError('Input file does not exist: ' + str(self.input)) return False
import fleep import os file1 = open("D:\Documents\leep_res.txt", "w") file1.close() def walkdir(folder): #Walk through each files in a directory for dirpath, dirs, files in os.walk(folder): for filename in files: yield os.path.abspath(os.path.join(dirpath, filename)) input_dir = input("Give an input directory: ") for filepath in walkdir(input_dir): with open(filepath, "rb") as fh: info = fleep.get(fh.read(128)) extension = info.extension file_write = open("D:\Documents\leep_res.txt", "a") file_write.write("\n" + filepath + " >>>> " + str(extension)) file_write.close()
def getFileExt(filePath): with open(filePath, "rb") as file: info = fleep.get(file.read(128)) return info.extension
library[i]["title"] = library[i]["title"].replace(c, "") full_path = f'{library_path[i]}/{library[i]["title"]}' full_path.encode("utf-8") cont = False if not os.path.exists(full_path): for e in extensions: if os.path.exists(full_path + e): print(full_path + e + " already exists.") cont = True break if cont: continue filename, audio = mm.download_song(library[i]["id"]) r = library[i]["title"][library[i]["title"].rfind("."):] if r == -1 or r not in extensions: full_path += "." + fleep.get(audio).extension[0] else: print(full_path + " already exists.") continue print(f'[{i}]: {full_path}') try: with open(full_path, 'wb') as f: f.write(audio) except FileNotFoundError: print("PROBLEM WITH PATH: " + full_path)
print('Finished recording') # Save the recorded data as a WAV file wf = wave.open(filename, 'wb') wf.setnchannels(channels) wf.setsampwidth(p.get_sample_size(sample_format)) wf.setframerate(fs) wf.writeframes(b''.join(frames)) wf.close() #conversion from other formats to wav file_in = r"m4aSamplefile.m4a" file_in_video = r"videoMp4.mp4" file_out = 'converted_video' song_out = AudioSegment.from_file(file_in_video).export(file_out + ".wav", format="wav") #get file type with open(file_in_video, "rb") as file: info = fleep.get(file.read(128)) print(info.type) print(info.extension) #print(info.mime) if info.type[0] == "audio": print('Audio file accepted') elif info.type[0] == "video": print('Video file input')
def Analyzer(): threatScore = 0 allFuncs = 0 tables = PrettyTable() secTable = PrettyTable() segTable = PrettyTable() extTable = PrettyTable() mimeTable = PrettyTable() ftypeTable = PrettyTable() statistics = PrettyTable() for key in dictArr: for elem in dictArr[key]: if elem in allStrings: if elem != "": Categs[key].append(elem) allFuncs += 1 for key in Categs: if Categs[key] != []: if key == "Information Gathering" or key == "System/Persistence" or key == "Cryptography": print( f"\n{yellow}[{red}!{yellow}]__WARNING__[{red}!{yellow}]{white}" ) # Printing zone tables.field_names = [ f"Functions or Strings about {green}{key}{white}" ] for i in Categs[key]: if i == "": pass else: tables.add_row([f"{red}{i}{white}"]) # Threat score if key == "Networking": threatScore += 10 scoreDict[key] += 1 elif key == "File": threatScore += 10 scoreDict[key] += 1 elif key == "Process": threatScore += 15 scoreDict[key] += 1 elif key == "Memory Management": threatScore += 10 scoreDict[key] += 1 elif key == "Information Gathering": threatScore += 20 scoreDict[key] += 1 elif key == "System/Persistence": threatScore += 20 scoreDict[key] += 1 elif key == "Cryptography": threatScore += 25 scoreDict[key] += 1 elif key == "Other/Unknown": threatScore += 5 scoreDict[key] += 1 else: pass print(tables) tables.clear_rows() # Gathering sections and segments secTable.field_names = [f"{green}Sections{white}"] segTable.field_names = [f"{green}Segments{white}"] # Sections sec_indicator = 0 for se1 in sections: if se1 in allThings: if se1 != "": secTable.add_row([f"{red}{se1}{white}"]) sec_indicator += 1 if sec_indicator != 0: print(secTable) # Segments seg_indicator = 0 for se2 in segments: if se2 in allThings: if se2 != "": segTable.add_row([f"{red}{se2}{white}"]) seg_indicator += 1 if seg_indicator != 0: print(segTable) # Resource scanner zone extTable.field_names = [f"Extracted {green}File Extensions{white}"] mimeTable.field_names = [f"Extracted {green}Mime Types{white}"] ftypeTable.field_names = [f"Extracted {green}File Types{white}"] with open(fileName, "rb") as targFile: extract = fl.get(targFile.read(128)) extArr = list(extract.extension) mimeAr = list(extract.mime) ftypes = list(extract.type) if extArr != []: for ex in extArr: extTable.add_row([f"{red}{ex}{white}"]) print(extTable) if mimeAr != []: for mt in mimeAr: mimeTable.add_row([f"{red}{mt}{white}"]) print(mimeTable) if ftypes != []: for ft in ftypes: ftypeTable.add_row([f"{red}{ft}{white}"]) print(ftypeTable) # Statistics zone print(f"\n{green}->{white} Statistics for: {green}{fileName}{white}") # Printing zone statistics.field_names = ["Categories", "Number of Functions"] statistics.add_row( [f"{green}All Functions{white}", f"{green}{allFuncs}{white}"]) for key in scoreDict: if scoreDict[key] == 0: pass else: if key == "System/Persistence" or key == "Cryptography" or key == "Information Gathering": statistics.add_row( [f"{yellow}{key}{white}", f"{red}{scoreDict[key]}{white}"]) else: statistics.add_row( [f"{white}{key}", f"{scoreDict[key]}{white}"]) print(statistics) # Warning about obfuscated file if allFuncs < 10: print( f"\n{errorS} This file might be obfuscated or encrypted. Try {green}--packer{white} to scan this file for packers.\n" ) sys.exit(0) # score table print( f"\n{errorS} ATTENTION: There might be false positives in threat scaling system." ) if threatScore < 100: print(f"{thLevel}: {green}Clean{white}.\n") elif threatScore >= 100 and threatScore <= 300: print( f"{errorS} Attention: Use {green}--vtFile{white} argument to scan that file with VirusTotal. Do not trust that file." ) print(f"{thLevel}: {yellow}Suspicious{white}.\n") else: print(f"{thLevel}: {red}Potentially Malicious{white}.\n")
def download_media(url): data = urllib.request.urlopen(url).read() print(fleep.get(data[:300]).extension) return data