Best Python code snippet using avocado_python
slavetorrent.py
Source:slavetorrent.py  
1"""\2BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB3B                                                                     B4B          BBBBBB     BBBBBBBB  BBk BBB  BBBBBBBB    BBBBBK           B5B          kBBBBBB  OKBBBBBBBB  BB  BBB  BBBBBBBBKO  BBRBBB           B6B          kBB  BB  BBB.  .BBB  BB BBO   BBB.  .BBB  BB  BBB          B7B          BBB BBB  BBk    .BB  BB BB    BB.    kBB  BB  .B           B8B          BBBBB    BB   O  BB  BBBB     BB  O   BB  BBOBBB           B9B           BBBBB   BBB. _ .BB  BBBB     BB. _ .BBB  BBBBK            B10B          BBB BBB  BBB,  ,BB   BB BB     BB.  ,BBB  BBBBBB           B11B          BBB  BB  BBBBBBBBB   BB kBB    BBBBBBBBB  RB  BB           B12B          BBBBBBB  BBBBBBBB    BB  BB     BBBBBBBB  BB  BB           B13B          BBBBBBB    BBBB      BBB BBB      BBBB    BB  BB           B14B                                                                     B15BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB16This program is free software; you can redistribute it and/or17modify it under the terms of the GNU General Public License18as published by the Free Software Foundation; either version 319of the License, or (at your option) any later version.20This program is distributed in the hope that it will be useful,21but WITHOUT ANY WARRANTY; without even the implied warranty of22MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the23GNU General Public License for more details.24You should have received a copy of the GNU General Public License25along with this program; if not, write to the Free Software Foundation,26Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.27@file feature/feature.py28@author Olivier ANDRE <zitune@bearstech.com>29@date 201430@brief process orders for bokor slave for rtorrent management31"""32import logging33import os34import signal35import subprocess36import atexit37import socket38import tempfile39import shutil40import xmlrpclib41import glob42import webbrowser43from time import sleep44from bokor.utils.tools import getfile, lastLine45from bokor.utils.hashtorrent import *46from bokor.executor.executor import Executor47from bokor.constants.error import *48from bokor.constants.system import *49from bokor.executor.executor import  aFeature, anExecutor, aPreProcess, aPostProcess50from bokor.configuration.configurable import Configurable51from bokor.interface.unsafesocket import UnsafeSocketClient52from bokor.executor.plugins.slavetorrent.rtorrent_configuration import write_conf_rtorrent53from bokor.executor.plugins.slavetorrent.constants import *54from bokor.executor.plugins.slavetorrent import bwtest as bwt55from bokor.executor.slavepost import *56from bokor.executor.plugins.slavetorrent import xmlrpc2scgi as xs57@anExecutor58class SlaveTorrent(Configurable):59    dependances = { "configuration" : [] ,60                    "interface" : [UnsafeSocketClient],61                    "transmission" : [],62                    "feature" : [SlavePost]}63    needed_configuration = { "rtorrent" :64                             {65                                 "bin": {'mandatory' : True, 'type' : 'file' , 'permission' : 'rx', 'exist' : True},66                                 "path_session": {'mandatory' : True, 'type' : 'file' , 'permission' : 'drwx', 'exist' : True},67                                 "path_dl": {'mandatory' : True, 'type' : 'file' , 'permission' : 'drwx', 'exist' : True},68                                 "path_log": {'mandatory' : True, 'type' : 'file' , 'permission' : 'drwx', 'exist' : True},69                                 "post_dl": {'mandatory' : True, 'type' : 'file' , 'permission' : 'r', 'exist' : True},70                                 "ci_up": {'mandatory' : True, 'type' : 'int'},71                                 "ci_down": {'mandatory' : True, 'type' : 'int'},72                                 "bind": {'mandatory' : True, 'type' : 'host'},73                                 "port_max": {'mandatory' : True, 'type' : 'int'},74                                 "port_min": {'mandatory' : True, 'type' : 'int'},75                                 "scgi_ip": {'mandatory' : True, 'type' : 'host'},76                                 "scgi_port": {'mandatory' : True, 'type' : 'int'},77                                 "down_kbps": {'mandatory' : True, 'type' : 'int'},78                                 "up_kbps": {'mandatory' : True, 'type' : 'int'},79                                 "max_seed": {'mandatory' : True, 'type' : 'int'},80                                 "max_leech": {'mandatory' : True, 'type' : 'int'},81                                 "max_dl_size":{'mandatory' : False, 'type' : 'int'},82                             },83                             "slave" :84                             {85                                 "url_conf" : {'mandatory' : True, 'type' : 'string'}86                             },87                         }88    return_codes = { socket.error : NO_CONNECTION_TO_RTORRENT,89                     xmlrpclib.Fault : BAD_INFO_HASH}90    def __init__(self, bokor):91        self.bokor = bokor92        self.check()93        #init some values94        #pid of rtorrent95        self.pid = None96        #process97        self.rprocess = None98        # post dl executor99        self.post = self.bokor.get_executor("SlavePost")100        #init rtorrent101        self._init_rtorrent()102        # move103        os.chdir(self.path_session)104        self.start_rtorrent()105    def _init_rtorrent(self):106        self.db = self.get('memory', 'sqlitedir', '/tmp')  + '/' + 'post.db'107        # we keep pathes and make sure they have a final /:108        self.path_session = self.get("rtorrent", "path_session").rstrip('/') + '/'109        self.path_dl = self.get("rtorrent", "path_dl").rstrip('/') + '/'110        self.path_log = self.get("rtorrent", "path_log").rstrip('/') + '/'111        #we keep the script post_dl112        self.post_dl = self.get("rtorrent", "post_dl")113        # get bin114        self.bin = self.get("rtorrent", "bin")115        # get rtorrentrc (in path_session)116        self.rtorrentrc = self.path_session + "/rtorrent.rc"117        #max_size_dl : -1 if none118        self.max_dl_size = int(self.get("rtorrent", "max_dl_size", -1))119        120        # get command to launch rtorrent121        self.rtorrent_cmd = [self.bin, '-n', '-o', 'import=%s' %122                         self.rtorrentrc]123        self.host = 'scgi://%s:%s' % (124            self.get("rtorrent", "scgi_ip"),125            self.get("rtorrent", "scgi_port"))126        #get link to rtorrent127        self.rtc = xs.RTorrentXMLRPCClient(self.host)128        self.rtorrent_conf = write_conf_rtorrent(129            self.rtorrentrc,130            self.db,131            self.get("rtorrent", "ci_up"),132            self.get("rtorrent", "ci_down"), self.bokor.token,133            self.get("rtorrent", "bind"),134            self.get("rtorrent", "port_max"),135            self.get("rtorrent", "port_min"),136            self.get("rtorrent", "scgi_ip"),137            self.get("rtorrent", "scgi_port"),138            self.path_session, self.path_dl, self.path_log,139            self.post_dl,140            self.get("rtorrent", "down_kbps"),141            self.get("rtorrent", "up_kbps"),142            self.get("rtorrent", "max_seed"),143            self.get("rtorrent", "max_leech"),144        )145#------------------------------------------------------------------------------146# Rtorrent software management147    @aFeature148    def get_rtorrent_conf(self):149        """Get conf file content of rtorrent as memorised by SlaveTorrent know150        it151        @return s string of the Conf"""152        return self.rtorrent_conf153    @aFeature154    def status_rtorrent(self):155        """Request Rtorrent status156        @return: Bool; True if rtorrent running157        """158        if not self.pid :159            return False160        else :161            #FIXME can be false162            return True163    @aFeature164    def rmlock(self):165        """rm rtorrent lock file166        @return: True167        """        168        os.remove(self.path_session + "/rtorrent.lock")169        return True170    @aFeature171    def stop_rtorrent(self):172        """Stop rtorrent173        @return: True174        """175        if OS.startswith('CYGWIN'):176            subprocess.Popen(["taskkill.exe", "/F", "/IM", "rtorrent.exe"])177            self.rmlock()178        else:179            if self.pid :180                os.kill(self.pid, signal.SIGTERM)181        self.pid = None182        print 'RTORRENT IS STOPPED'183        logging.info('RTORRENT IS STOPPED')184        return True185    @aFeature186    def start_rtorrent(self) :187        """Launch Rtorrent188        @return: Rtorrent pid189        """190        if self.pid:191            return self.pid192        self.rprocess = subprocess.Popen(self.rtorrent_cmd)193        self.pid = self.rprocess.pid194        atexit.register(self.stop_rtorrent)195        # rtorrent need to be ready, so we wait a little196        sleep(0.5)197        print 'RTORRENT IS STARTED'198        logging.info('RTORRENT IS STARTED')199        return self.pid200#------------------------------------------------------------------------------201# Rtorrent system function :202    @aFeature203    def version(self) :204        """Rtorrent version205        @return: Rtorrent version206        """207        return self.rtc.system.client_version()208#------------------------------------------------------------------------------209# Rtorrent download functions :210    def get_hash_files(self, hash_file):211        """Get_hash_files gives a list of hash_files given a hash file, a list212        of hash files or []213        @type  hash_file: list214        @param hash_file: list of infohashes215        @return: List of paths to torrent files216        """217        hash_files = hash_file218        if hash_files and not isinstance(hash_files, list) :219            hash_files = [hash_files]220        if not hash_files :221            hash_files = self.download_list()222        return hash_files223    @aFeature224    def download_list(self, view = ''):225        """Request the download list226        @type  view: str227        @param view: filter on download list228        @return: download list229        """230        return self.rtc.download_list('', view)231# ------ NEW DOWNLOAD ----232    @aFeature233    def create_download(self, hash_file, address_torrent, relative_path = ""):234        """Add a torrent235        @type  address_torrent: str236        @param address_torrent: URL of the torrent file237        @type  hash_file: str238        @param hash_file: Torrent infohash239        @type  relative_path: str240        @param relative_path: Final path for download241        @return: True or None (False ??)242        """243        logging.info("adding %s from %s (relative path : %s)" % (hash_file,244                                                                 address_torrent, relative_path))245        rp = None;246        # is the hash_file already known ?247        if hash_file in self.rtc.download_list():248            if relative_path:249                completes = [ dl['hash_file'] for dl in self.status_view('complete')]250                status = 'todo'251                if hash_file not in completes :252                    status = 'downloading'253                self.post.create_relative_path_if_needed(hash_file, relative_path, self.get_path_file(hash_file)[0], status)254            if rp :255                return rp['id']256            return 0257        tmpfile = tempfile.NamedTemporaryFile(suffix=".torrent_download")258        logging.info('Get %s.torrent' % hash_file)259        getfile(address_torrent, tmpfile.file)260        logging.info('%s.torrent fetched' % hash_file)261        print 'Create download for %s' % (hash_file)262        computeDl = getHashFromFd(tmpfile.file)263        size = getSizeFromFd(tmpfile.file)264        if not hash_file or hash_file != computeDl:265            logging.error('Error Infohash for %s' % hash_file)266            print 'Error Infohash for %s' % (hash_file)267            return "File downloaded but infohash not corresponding, %s VS %s" % (hash_file, computeDl)268        s = os.statvfs(self.path_dl)269        if self.max_dl_size > 0 : #we have a limit to respect270            freespace = self.free_dl()271        else :272            freespace = s.f_bavail * s.f_frsize273        if size >= freespace:274            self.lastError = "%s has a size of %s, but only %s remaining" % (hash_file, size, freespace)275            self._bokor_code = FREE_SPACE_ERROR276            logging.error('Error Free Space for %s' % hash_file)277            print 'Not Enough Free Space for %s' % (hash_file)278            return -1 # specific case279        dst_torrent = self.path_session + "/" + computeDl + ".torrent_download"280        shutil.copy2(tmpfile.name, dst_torrent)281        if not self.load(dst_torrent, hash_file) :282            self.lastError = "could not load %s" % (hash_file)283            self._bokor_code = LOAD_ERROR_RTORRENT284            logging.error('Error Load for %s' % hash_file)285            print 'Error Load for %s' % (hash_file)286            return 0 # error287        if relative_path:288            self.set_relative_path_for_dl(hash_file, relative_path)289        self.rtc.d.start(hash_file)290        #preapre post dl operations291        rp = self.post.create_relative_path(hash_file, relative_path, self.get_path_file(hash_file)[0], 'downloading')292        print 'Create download ended for %s' % (hash_file)293        logging.info('Create download ended for %s' % hash_file)294        return rp['id'] 295    @aFeature296    def validate_dl(self, hash_file, relative_path):297        """validate the relative_path of  a new torrent298        @type  request: dict299        @param request: Should only contain 'token_file' and 'relative_path'300        @type  relative_path: str301        @param relative_path: Final path for download302        @return: call __error_torrent() if no answer,303        C_SUCCESS(=0) and Rtorrent answer otherwise304        """305        self.stop_download(hash_file)306        self.set_relative_path_for_dl(hash_file, relative_path)307        self.start_download(hash_file)308        return True309    @aFeature310    def validate(self, hash_pathes):311        """validate the relative_path of  a new torrent312        @type  hash_pathes: dict313        @param request: Should only contain 'token_file' and 'relative_path'314        @return: true or false315        """316        ares = {}317        tres = True318        for plan in hash_pathes:319            hashf, relp = plan['hashfile'], plan['relative_path']320            res = self.validate_dl(hashf, relp)321            ares[hashf] = res322            tres = tres and res323        if not tres:324            return ares325        return ares326# ----------------------327    @aFeature328    def set_relative_path_for_dl(self, hash_file, relative_path):329        """change download directory330        @type  relative_path: str331        @param relative_path: relative path332        @type  hash_file: str333        @param hash_file: Torrent infohash334        @return: True or None (False ??)335        """336        relative_path = self.path_dl + '/' + relative_path337        if not os.path.isdir(relative_path):338            logging.info("creating %s" % (relative_path))339            os.makedirs(relative_path)340        self.rtc.d.set_directory(hash_file, relative_path)341        logging.info("set directory base to %s for %s" % (relative_path, hash_file))342        loaded_from = self.get_loaded_file(hash_file)343        tied = self.get_tied_file(hash_file)344        if not tied.endswith(".torrent"):  # already treated345            to_tied = ".".join(loaded_from.split(".")[:-1]) + ".torrent"346            self.set_tied_file(hash_file, to_tied)347            shutil.move(loaded_from, to_tied)348        return349    @aFeature350    def load(self, path, hash_file):351        """Load a .torrent and wait for it to be loaded352        @type  path: str353        @param path: path to torrent file354        @type  hash_file: str355        @param hash_file: torrent infohash356        """357        res = self.rtc.load_verbose(path)358        sleeped = 1359        while hash_file not in self.download_list() :360            logging.info("searching %s in %s" % (str(hash_file), str(self.download_list())))361            sleep(1)362            sleeped += 1363            if sleeped > 15 :364                return False365        return res == 0366    @aFeature367    def get_loaded_file(self, hash_file):368        """get the file the hash_file was loaded from (a .torrent file)369        @type  hash_file: str370        @param hash_file: hash of the file371        @return: path relative to session372        """373        return self.rtc.d.get_loaded_file(hash_file)374    @aFeature375    def get_tied_file(self, hash_file):376        """get the file the hash_file is tied to (a .torrent file)377        @type  hash_file: str378        @param hash_file: hash of the file379        @return: path relative to session380        """381        return self.rtc.d.get_tied_to_file(hash_file)382    @aFeature383    def set_tied_file(self, hash_file, path):384        """Set the file the hash_file is tied to (a .torrent file)385        @type  hash_file: str386        @param hash_file: hash of the file387        @type  path: str388        @param path: file to tied to389        @return: path relative to session390        """391        return self.rtc.d.set_tied_to_file(hash_file, path)392    @aFeature393    def xmlrpc(self, function, args = None):394        """Direct call to xmlrpc function395        @type  function: str396        @param function: function name397        @type  args: str398        @param args: function arguments399        @return xmlrpc return400        """401        rtc = xs.RTorrentXMLRPCClient(self.host)402        if args :403            args = [ arg.replace('|',',').strip() for arg in args.split(',')]404            return rtc.__getattr__(function)(*args)405        else:406            return rtc.__getattr__(function)()407    @aFeature408    def status_exchange_legacy(self, hash_file = []):409        """Get status of a hash_file410        @type  hash_file: list411        @param hash_file: list of infohashes412        @return: a description of the download413        """414        hash_files = self.get_hash_files(hash_file)415        res = []416        for hash_file in hash_files :417            #self._reread_torrent_trace()418            tmpres = {'hash_file': hash_file,419                      'size': self.get_size(hash_file),420                      'downloaded': self.get_size_done(hash_file),421                      'started': self.is_started(hash_file),422                      'priority': self.get_priority(hash_file),423                      'status': "pending",424                      'up': self.get_up_rate(hash_file),425                      'down': self.get_down_rate(hash_file),426                      'ratio': float(self.get_completed_chunk(hash_file)) / self.get_size_in_chunk(hash_file) * 100,427                      'ETA': self.get_eta(hash_file),428                      'file': self.get_path_file(hash_file),429                      'mesg': self.msg(hash_file),430                      'coherence': self.coherence(hash_file),431                      'loaded_from' : self.get_loaded_file(hash_file),432                      'tied_to' : self.get_tied_file(hash_file),433                      'peers': self.get_peers(hash_file),434            # 'duplicates': json.loads(self.torrent_trace.get("DUPLICATE", hash_file.lower())) if self.torrent_trace.has_option("DUPLICATE", hash_file.lower()) else [],435            # 'copies': json.loads(self.torrent_trace.get("COPY", hash_file.lower())) if self.torrent_trace.has_option("COPY", hash_file.lower()) else [],436            # 'post': json.loads(self.torrent_trace.get("POST", hash_file.lower())) if self.torrent_trace.has_option("POST", hash_file.lower()) else [],437                  }438            if self.is_hash_checking(hash_file):439                tmpres['status'] = "preparating"440            if self.is_hash_checked(hash_file):441                tmpres['status'] = "ready"442            if tmpres['down'] > 0:443                tmpres['status'] = "downloading"444            if tmpres['up'] > 0:445                tmpres['status'] = "uploading"446            if tmpres['down'] > 0 and tmpres['up'] > 0:447                tmpres['status'] = "exchanging"448            tmpres['age'] = os.path.getmtime(tmpres['file'][0])449            res.append(tmpres)450        return res451    @aFeature452    def status_view(self, view = ''):453        fields = ["hash_file", 'size', 'downloaded', 'started', 'priority', 'up', 'down', 'completed_chunk',454                  'chunk', 'msg', 'loaded_from', 'tied_to', 'age', "is_checking", "is_checked", 'file', 'peers']455        values = self.rtc.d.multicall(view, 'd.hash=', 'd.get_size_bytes=', 'd.get_bytes_done=',456                                      'd.is_open=', 'd.get_priority=', 'd.get_up_rate=', 'd.get_down_rate=',457                                      'd.get_completed_chunks=', 'd.get_size_chunks=',458                                      'd.get_message=', 'd.get_loaded_file=', 'd.get_tied_to_file=', 'd.get_creation_date=',459                                      'd.is_hash_checking=', 'd.is_hash_checked=', 'f.multicall=,,f.get_frozen_path=,f.is_created=',460                                      'p.multicall=,,p.get_address=,p.get_down_rate=, p.get_up_rate=')461        # 'duplicates': json.loads(self.torrent_trace.get("DUPLICATE", hash_file.lower())) if self.torrent_trace.has_option("DUPLICATE", hash_file.lower()) else [],462        # 'copies': json.loads(self.torrent_trace.get("COPY", hash_file.lower())) if self.torrent_trace.has_option("COPY", hash_file.lower()) else [],463        # 'post': json.loads(self.torrent_trace.get("POST", hash_file.lower())) if self.torrent_trace.has_option("POST", hash_file.lower()) else [],464        res = []465        for dl in values :466            # don't take last 2 elements, they are list for files or peer467            desc = dict(zip(fields, dl))468            desc['started'] = (desc['started'] == 1)469            desc['status'] = "pending"470            desc['ratio'] = float(desc['completed_chunk'])/float(desc['chunk']) * 100471            # treat coherence472            if desc['started'] == 0: #file not open, no coherence problem possible473                desc['coherence'] = True474                # if open, the file should exist :475            else :476                desc['coherence'] = desc['file'][0][-1] == 1477            478            # get file (in a proper format479            desc['file'] = desc['file'][0][1]480            # treats peers481            peers = desc['peers']482            desc['peers'] = []483            for peer in peers :484                desc['peers'].append(dict(zip(['token', 'ip', 'upload_rate', 'download_rate'], peer)))485            desc['ETA'] = -1486            if int(desc['down']) == 0:487                if desc['size'] == desc['downloaded']:488                    desc['ETA'] = 0489            else:490                desc['ETA'] = (float(desc['size']) - float(desc['downloaded'])) / float(desc['down'])491            if desc['is_checking']:492                desc['status'] = "preparating"493            if desc['is_checked']:494                desc['status'] = "ready"495            if int(desc['down']) > 0:496                desc['status'] = "downloading"497            if int(desc['up']) > 0:498                desc['status'] = "uploading"499            if int(desc['down']) > 0 and int(desc['up']) > 0:500                desc['status'] = "exchanging"501            res.append(desc)502        return res503    @aFeature504    def status_exchange(self, hash_file = []):505        """Get status from infohashes506        @type  hash_file: str, or list507        @param hash_file: hash_file name508        @return: a description of the download509        """510        hash_files = self.get_hash_files(hash_file)511        all_res = self.status_view()512        res = []513        if not hash_files :514            return all_res515        for desc in all_res :516            if desc['hash_file'] in hash_files :517                res.append(desc)518        return res519    @aFeature520    def get_creation_date(self, hash_file) :521        """522        """523        return self.rtc.d.get_creation_date(hash_file)524    @aFeature525    def status_download(self):526        """Get status of hash_files that are downloadding527        @return: a description of the download528        """529        return self.status_view('leeching')530    @aFeature531    def status_upload(self):532        """Get status of hash_files that are uploading533        @return: a description of the download534        """535        return self.status_view('seeding')536    @aFeature537    def status_paused(self):538        """Get status of hash_files that are paused539        @return: a description of the download540        """541        res = []542        return self.status_view('stopped')543    @aFeature544    def list_ended_files(self):545        """Get status of hash_files that are ended546        @return: a description of the download547        """548        return self.status_view('complete')549    @aFeature550    def get_path_file(self, hash_file):551        """Get the real path or the file downloaded552        @type  hash_file: str553        @param hash_file: hash of the file554        @return: path of file555        """556        return self.rtc.f.multicall(hash_file, '', 'f.get_frozen_path=')[0][0],557    @aFeature558    def get_size(self, hash_file):559        """Get the total size in bytes of a specific download560        @type  hash_file: str561        @param hash_file: hash of the file562        @return: Download size in bytes563        """564        return self.rtc.d.get_size_bytes(hash_file)565    @aFeature566    def get_size_done(self, hash_file):567        """Get the total size in bytes of a specific download568        @type  hash_file: str569        @param hash_file: hash of the file570        @return: Download size in bytes571        """572        return self.rtc.d.get_bytes_done(hash_file)573    @aFeature574    def get_peers(self, hash_file):575        """Get list of peers that are used for a torrent576        @type  hash_file: str577        @param hash_file: hash of the file578        @return: a list of peer579        """580        peers = self.rtc.p.multicall(hash_file, "", "p.get_address=",581                                     "p.get_down_rate=", "p.get_up_rate=")582        res = []583        for peer in peers:584            res.append({'ip': peer[0],585                        'upload_rate': peer[1],586                        'download_rate': peer[2],587                        'token_site': ''})588        return res589    @aFeature590    def get_size_in_chunk(self, hash_file):591        """Get the total size of a specific download592        @type  hash_file: str593        @param hash_file: hash of the file594        @return: Download size595        """596        return self.rtc.d.get_size_chunks(hash_file)597    @aFeature598    def get_completed_chunk(self, hash_file):599        """Get number of complete downloaded chunks600        @type  hash_file: str601        @param hash_file: hash of the file602        @return: Number of complete downloaded chunks603        """604        return self.rtc.d.get_completed_chunks(hash_file)605    @aFeature606    def get_ratio(self, hash_file):607        """Get download ratio608        @type  hash_file: str609        @param hash_file: hash of the file610        @return: Ratio download (float)611        """612        return float(self.get_completed_chunk(hash_file)) / \613            self.get_size_in_chunk(hash_file) * 100614    @aFeature615    def get_eta(self, hash_file):616        """Get estimated downloading time left617        @type  hash_file: str618        @param hash_file: hash of the file619        @return: Estimated time left in seconds620        """621        down_rate = self.rtc.d.get_down_rate(hash_file)622        if down_rate == 0:623            if self.rtc.d.get_bytes_done(hash_file) == self.rtc.d.get_size_bytes(hash_file):624                return 0625            return -1626        return (self.rtc.d.get_size_bytes(hash_file) - self.rtc.d.get_bytes_done(hash_file)) / self.rtc.d.get_down_rate(hash_file)627    @aFeature628    def get_up_rate(self, hash_file):629        """Get upload rate for a specific download630        @type  hash_file: str631        @param hash_file: hash of the file632        @return: Upload rate in ???633        """634        return self.rtc.d.get_up_rate(hash_file)635    @aFeature636    def get_down_rate(self, hash_file):637        """Get download rate for a specific download638        @type  hash_file: str639        @param hash_file: hash of the file640        @return: Download rate in ???641        """642        return self.rtc.d.get_down_rate(hash_file)643    @aFeature644    def is_hash_checked(self, hash_file):645        """Verify if a specific download hash has been checked646        @type  hash_file: str647        @param hash_file: hash of the file648        @return: True or False649        """650        return self.rtc.d.is_hash_checked(hash_file) == 1651    @aFeature652    def is_hash_checking(self, hash_file):653        """Verify if a specific download hash is being checked654        @type  hash_file: str655        @param hash_file: hash of the file656        @return: True or False657        """658        return self.rtc.d.is_hash_checking(hash_file) == 1659    @aFeature660    def is_started(self, hash_file):661        """Verify if a specific download hash is being checked662        @type  hash_file: str663        @param hash_file: hash of the file664        @return: True or False665        """666        return self.rtc.d.is_open(hash_file) == 1667    @aFeature668    def msg(self, hash_file):669        """get the messages associated with hash_file670        @type  hash_file: str671        @param hash_file: hash of the file672        @return: str, message673        """674        return self.rtc.d.get_message(hash_file)675    @aFeature676    def coherence(self, hash_file):677        """Verify if a specific download hash is coherent, ie its file are here and well678        @type  hash_file: str679        @param hash_file: hash of the file680        @return: True or False681        """682        # if not open, no notion of coherence683        if self.rtc.d.is_open(hash_file) == 0:684            return True685        # if open, the file should exist :686        return self.rtc.f.multicall(hash_file, '', 'f.is_created=')[0][0] == 1687    @aFeature688    def add_peer(self, hash_file, peer):689        """Add a specific peer to a specific download690        @type  hash_file: str691        @param hash_file: hash of the file692        @type  peer: str693        @param peer: Peer IP694        @return: True or False695        """696        return self.rtc.add_peer(hash_file, peer) == 1697    @aFeature698    def get_priority(self, hash_file):699        """Get download priority (0-3)700        @type  hash_file: str701        @param hash_file: hash of the file702        @return: Priority703        """704        return self.rtc.d.get_priority(hash_file)705    @aFeature706    def set_priority(self, hash_file, value):707        """Set download priority708        @type  hash_file: str709        @param hash_file: hash of the file710        @type  value: int711        @param value: priority (0-3)712        @return: True or False713        """714        res = self.rtc.d.set_priority(hash_file, int(value)) == 0715        self.rtc.d.update_priorities(hash_file)716        return res717    @aFeature718    def get_priority_str(self, hash_file):719        """Get download priority as a string (0=off, 1=low, 2=normal, 3=high)720        @type  hash_file: str721        @param hash_file: hash of the file722        @return: Priority as a string723        """724        return self.rtc.d.get_priority_str(hash_file)725    @aFeature726    def start_download(self, hash_file):727        """Start a dl given its hash_file728        @type  hash_file: str729        @param hash_file: hash of the file730        @return: True if dl started731        """732        return self.rtc.d.start(hash_file)733    @aFeature734    def stop_download(self, hash_file):735        """Stop a dl given its hash_file736        @type  hash_file: str737        @param hash_file: hash of the file738        @return: True if dl started739        """740        return self.rtc.d.stop(hash_file)741    @aFeature742    def start_all(self):743        """Start all known downloads744        @return: True745        """746        self.rtc.d.multicall('', 'd.start=')747        return True748    @aFeature749    def stop_all(self):750        """Stop all known downloads751        @return: True752        """753        self.rtc.d.multicall('', 'd.stop=')754        return True755    @aFeature756    def is_all_started(self):757        """Check if all downloads are started758        @return: True if all downloads are started759        """760        #we test the size of the list of stopped download761        # if it's empty, all is started762        return len(self.download_list('stopped')) == 0763    @aFeature764    def is_all_stopped(self):765        """Check if all downloads are stopped766        @return: True if all downloads are stopped767        """768        #we test the size of the list of started download769        # if it's empty, all is stopped770        return len(self.download_list('started')) == 0771    @aFeature772    def is_present(self, hash_file) :773        """Check if a specific download is present774        @type  hash_file: str775        @param hash_file: infohash to check776        @return: True if download is present777        """778        return (hash_file in self.download_list())779    @aFeature780    def erase(self, hash_file):781        """Erase as hash_file (this will not remove file)782        @type  hash_file: str783        @param hash_file: infohash to erase784        @return True or False785        """786        return self.rtc.d.erase(hash_file)787    @aFeature788    def remove_file(self, hash_file, follow = False):789        """Erase as hash_file (this WILL remove the file)790        @type  hash_file: str791        @param hash_file: infohash to erase792        @return True or False793        """794        files = self.get_path_file(hash_file)795        errors = []796        link = ''797        for f in files:798            try:799                link = ''800                if follow and os.path.islink(f) :801                    link = os.readlink(f)802                os.remove(f)803                if follow and link :804                    os.remove(link)805                if not os.listdir(os.path.dirname(f)) :806                    os.rmdir(os.path.dirname(f))807            except:808                errors.append(f)809        self.rtc.d.erase(hash_file)810        return {'errors' : errors}811    812# -------- 813    @aFeature814    def statfs(self):815        """Prepare a new torrent given a path and tocken816        @type  request: dict817        @param request: Should be empty818        """819        l = {"path_session" : self.path_session, "path_dl" : self.path_dl, "path_log" : self.path_log}820        res = {}821        for n in l:822            s = os.statvfs(l[n])823            res[n] = (s.f_bavail * s.f_frsize)824        limit_size = self.max_dl_size825        if self.max_dl_size < 0 :826            return res827        res['free_dl'] = self.free_dl()828        return res829    @aFeature830    def free_dl(self):831        """Prepare a new torrent given a path and tocken832        @type  request: dict833        @param request: Should be empty834        """835        limit_size =  self.max_dl_size836        if limit_size < 0 :837            return -1838        total_size = 0839        for dirpath, dirnames, filenames in os.walk(self.path_dl):840            for f in filenames:841                fp = os.path.join(dirpath, f)842                total_size += os.path.getsize(fp)843        freespace = limit_size - total_size844        return freespace845# ----- logs management846    @aFeature847    def get_log(self, type_log = 'client', nb_line = 10):848        """Get log file849        @type  request: dict850        @param request: can have nb and file params, must have type851        @return: C_SUCCESS(=0)852        and the conntent of the last nb lignes of the curent853        """854        if type_log not in ['rtorrent', 'client']:855            return C_PARAM_ERROR, "log type %s doesn't exist" % type_log856        res = []857        if type_log == 'client':858            res = lastLine(self.bokor.configuration.get_log_file(), int(nb_line))859        if type_log == 'rtorrent':860            res = lastLine(self.path_log + "rtorrent.log", int(nb_line))861        return res862    @aFeature863    def list_logs(self, type_log = None):864        """Request the list of all log files865        @type  request: dict866        @param request: can contain type as 'rtorrent', 'client' or 'prepare']867        @return: return list of logs file868        (with a filter on type if type is present)869        """870        res = []871        if type_log not in [None, 'rtorrent', 'client']:872            type_log = None873        lfile = []874        if type_log :875            if type_log == "client":876                lfile = glob.glob('%s/%s*' %877                                   (os.path.dirname(self.bokor.configuration.get_log_file()),878                                   type_log))879            if type_log == "rtorrent":880                lfile = glob.glob('%s/%s*' %881                                  (self.path_log,882                                  type_log))883        else :884            lfile = [self.path_log + '/' + x885                     for x in os.listdir(self.path_log) ]886            if self.bokor.configuration.get_log_file() :887                lfile += [ self.bokor.configuration.get_log_file() + '/' + x888                           for x in os.listdir(os.path.dirname(self.bokor.configuration.get_log_file()))]889        lfile = sorted(set(lfile))890        for f in lfile:891            if os.path.isfile(f) and os.access(f, os.R_OK):892                res.append({'file': f,893                            'age': os.path.getmtime(f),894                            'size': os.stat(f).st_size})895        return res896# bw test897    @aFeature898    def bwtest(self, slow = False):899        """Test the speed of the bandwidth down, up, ping900        @type request: request should be empty901        @param request: empty902        @return: C_SUCCESS(=0) and a dict with three key "download_speed, upload_speed, ping"903        """904        if self.status_rtorrent():905            self._bokor_code =  BW_ERROR 906            return "rtorrent running"907        exclude = []908        if slow :909            exclude = ['/5M', '/10M']910        ret = {}911        try:912            s_dl = bwt.download(exclude)913            s_up = bwt.upload()914            s_pg = bwt.ping()915        except:916            self._bokor_code =  BW_ERROR 917            return "Something went wrong with bandwith test"918        ret["download_speed"] = s_dl / 1024919        ret["upload_speed"] = s_up / 1024920        ret["ping"] = s_pg921        return ret922# -----------------------------------------------------------------------------923    @aFeature924    def external_configuration(self, token_tmp = None):925        """open a web browser to begin or continue configuration926        """927        if token_tmp != self.bokor.token:928            self.bokor.token = token_tmp929        webbrowser.open_new_tab(self.get("slave", "url_conf") +930                                '?token=%s&init=False' %931                                (self.bokor.token))932        return True933    @aFeature934    def update(self, conf):935        """Update configuration file based on a new one936        @type  request: dict937        @param request: should only contains 'conf', path of the new conf938        @return: C_SUCCESS(=0)939        """940        for section in conf:941            for option in conf[section]:942                if section == "upload" and option == "config":943                    continue944                self.bokor.configuration.set(section, option, conf[section][option])945                logging.info("update : set (%s, %s) to  %s" %946                             (section, option, conf[section][option]))947        self.bokor.configuration.write_conf()...create.py
Source:create.py  
1import os2import random3import datetime4import random5import time6import json7import getpass8from pathlib import Path9import paramiko10import xlwt11import xml.etree.cElementTree as ET12import sqlite313from cryptography.fernet import Fernet14from docx import Document15from fpdf import FPDF16from etc.api.googledrv.gdrive_folder import *17from etc.serverx.config.config_server import *18from etc.aws.bucket_s3 import *19# Global variables/Variáveis globais.20path_atual_ct = str(pathlib.Path(__file__).parent.absolute())21path_ct_final = path_atual_ct.replace('\\etc','')22Path(f'C:/Users/{getpass.getuser()}/Documents/Oblivion').mkdir(parents=True, exist_ok=True)23def ssh_enviar_arquivo(arquivo, destino, key=None):24    """25    Sends file by SSH/Envia arquivo por SSH.26    :param arquivo: File/Arquivo.27    :param destino: Destiny/Destino.28    """29    with open(f'{path_ct_final}/etc/serverx/config/ssh_hosts.txt', 'r') as hosts_ssh:30        final_ssh = []31        conteudo_hosts = hosts_ssh.read()32        conteudo_hosts = conteudo_hosts.split('\n')33        for e in conteudo_hosts:34            if 'amazonaws' in e:35                try:36                    e = e.split(';')37                    if e[5] != key:38                        break39                    # If you want that the Oblivion creates the folder, uncommnet this:40                    # Se vc quiser que o Oblivion crie uma pasta, descomente isso:41                    #42                    # stdin, stdout, stderr = ssh.exec_command("mkdir leak")43                    ssh = paramiko.SSHClient()44                    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())45                    ssh.connect(str(e[0]),46                                username=str(e[2]),47                                key_filename=f'''{path_ct_final}/etc/serverx/pems/{e[6]}''')48                    sftp = ssh.open_sftp()49                    sftp.put(arquivo, f'{e[4]}/{destino}')50                    sftp.close()51                    ssh.close()52                except:53                    pass54            else:55                try:56                    e = e.split(';')57                    if e[5] != key:58                        break59                    s = paramiko.SSHClient()60                    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())61                    s.connect(e[0], int(e[1]), e[2], e[3])62                    sftp = s.open_sftp()63                    sftp.put(arquivo, f'{e[4]}/{destino}')64                    sftp.close()65                    s.close()66                except:67                    pass68def ocultar_senhas(senhas):69    """70    Ocults the last characters of password/Oculta os últimos caractéres da senha.71    :param senhas: Password/Senha.72    """73    if senhas != 'None':74        #senhas = senhas.encode('utf-16', 'replace').decode('utf-16')75        tamanho = int(len(senhas)/2)76        tamanho = -tamanho77        repor = senhas[tamanho:]78        senhas = senhas.replace(repor,'*****')79        return senhas80    return senhas81def criptografar(arquivo):82    """83    Crypt a file/Criptografa um arquivo.84    :param arquivo: Path of file/Local do arquivo.85    """86    #key = b'mrYXrs2rnYanAx88wDdqvgFc6aapqU5a-CnHSp_AAmk='87    with open(f'{path_ct_final}/etc/key_crypt.txt', 'r') as pegar_key:88        key = pegar_key.read()89    input_file = arquivo90    output_file = arquivo + '.encrypted'91    with open(input_file, 'rb') as f:92        data = f.read()93    fernet = Fernet(key)94    encrypted = fernet.encrypt(data)95    with open(output_file, 'wb') as f:96        f.write(encrypted)97def descriptografar(arquivo):98    """99    Decrypt a file/Descriptografa um arquivo.100    :param arquivo: Path of file/Local do arquivo.101    """102    key = b'mrYXrs2rnYanAx88wDdqvgFc6aapqU5a-CnHSp_AAmk='103    input_file = arquivo + '.encrypted'104    output_file = arquivo105    with open(input_file, 'rb') as f:106        data = f.read()107    fernet = Fernet(key)108    try:109        decrypted = fernet.decrypt(data)110        with open(output_file, 'wb') as f:111            f.write(decrypted)112    except:113        pass114def string_grande(criar=None, criar_ocult=None, criar_cript=None, documentos=None, nome=None, data_atual=None,115                  binario=None, ssh_enviar=None, id_pasta=None, gdrive=None, aws_s3_gerar=None, serverx=None,116                  key=None):117    """118    Creates a txt file with the string if the string is big too much (> len 500 aprox)/Cria uma arquivo txt caso a string em questão119    for muito grande (> len 500 aprox).120    """121    if criar == 'PySide2.QtCore.Qt.CheckState.Checked':122        hash_file = random.getrandbits(23)123        if criar_ocult == 'PySide2.QtCore.Qt.CheckState.Checked':124            with open(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt', 'w+', encoding='utf-16') as arquivo:125                #binario = binario.encode('utf-16', 'replace').decode('utf-16')126                arquivo.write(f'{binario}\n')127        if criar_ocult == 'PySide2.QtCore.Qt.CheckState.Unchecked':128            with open(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt', 'w+', encoding='utf-16') as arquivo:129                #binario = binario.encode('utf-16', 'replace').decode('utf-16')130                arquivo.write(f'{binario}\n')131        if criar_cript == 'PySide2.QtCore.Qt.CheckState.Checked':132            criptografar(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt')133            os.remove(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt')134            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':135                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.txt.encrypted', 'text/plain',136                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt.encrypted')137            if ssh_enviar and serverx:138                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt.encrypted',139                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.txt.encrypted', key=key)140            if aws_s3_gerar and serverx:141                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)142        else:143            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':144                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.txt', 'text/plain',145                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt')146            if ssh_enviar and serverx:147                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt',148                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.txt', key=key)149            if aws_s3_gerar and serverx:150                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)151def gerar_resultados(152        serverx=False, key=None,153        ssh_enviar=False, aws_s3_gerar=False,154        nome=None, gdrive=False, conteudo=None,155        txt_f=False, txt_cript=False, txt_ocult=False,156        docx_f=False, docx_cript=False, docx_ocult=False,157        pdf_f=False, pdf_cript=False, pdf_ocult=False,158        xlsx_f=False, xlsx_cript=False, xlsx_ocult=False,159        json_f=False, json_cript=False, json_ocult=False,160        html_f=False, html_cript=False, html_ocult=False,161        xsl_f=False, xsl_cript=False, xsl_ocult=False,162        db_f=False, db_cript=False, db_ocult=False,163        gd_txt_f=False, gd_txt_cript=False, gd_txt_ocult=False,164        gd_docx_f=False, gd_docx_cript=False, gd_docx_ocult=False,165        gd_pdf_f=False, gd_pdf_cript=False, gd_pdf_ocult=False,166        gd_xlsx_f=False, gd_xlsx_cript=False, gd_xlsx_ocult=False,167        gd_json_f=False, gd_json_cript=False, gd_json_ocult=False,168        gd_html_f=False, gd_html_cript=False, gd_html_ocult=False,169        gd_xsl_f=False, gd_xsl_cript=False, gd_xsl_ocult=False,170        gd_db_f=False, gd_db_cript=False, gd_db_ocult=False171):172    """173    Generates final file/Gerar arquivo final.174    :param serverx: If is Oblivion Server or Oblivion Client/Se é o Cliente Oblivion ou o Server Oblivion.175    :param ssh_gerar: Sends file by SSH/Enviar arquivo por SSH.176    :param aws_s3_gerar: Sends file to S3 Bucket of AWS/Enviar arquivo para o Bucket S3 da AWS.177    :param nome: Name of file/Nome do arquivo.178    :param gdr: Uploads to Google Drive/Sobe arquivo para o Google Drive.179    :param conteudo: Data/Dados.180    :param ..._f: Generates file/Gera arquivo.181    :param ..._cript: Generates encrypted file/Gera um arquivo criptografado.182    :param ..._ocult: Generates a file with oculted passwords/Gera um arquivo com a senhas ocultas.183    Examples:184    ..._f + ..._cript = Creates encrypted file/Cria arquivo criptografado.185    ..._f + ..._cript + ..._ocult = Creates encrypted file with oculted password/Cria arquivo criptografado com as senhas186    ocultas.187    """188    data_atual = datetime.date.today()189    definir = os.environ190    with open(f'{path_ct_final}/etc/api/googledrv/id_folder.txt', 'r') as pegar_id_pasta:191        id_pasta = str(pegar_id_pasta.read())192    for valor, item in definir.items():193        if valor == 'APPDATA':194            lista_item = item.split('\\')195            usuario = lista_item[2]196    documentos = f'C:/Users/{usuario}/Documents'197    if txt_f == 'PySide2.QtCore.Qt.CheckState.Checked':198        hash_file = random.getrandbits(23)199        if txt_ocult == 'PySide2.QtCore.Qt.CheckState.Checked':200            with open(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt', 'w+', encoding='utf-16') as arquivo:201                for binario in conteudo:202                    if not 'N/A' in binario:203                        #binario = binario.encode('utf-16', 'replace').decode('utf-16')204                        binario_repor = binario.split(':')205                        senha = binario_repor[1]206                        senha_ocult = ocultar_senhas(senha)207                        binario = binario.replace(senha, senha_ocult)208                        arquivo.write(f'{binario}\n')209        if txt_ocult == 'PySide2.QtCore.Qt.CheckState.Unchecked':210            with open(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt', 'w+', encoding='utf-16') as arquivo:211                for binario in conteudo:212                    #binario = binario.encode('utf-16', 'replace').decode('utf-16')213                    arquivo.write(f'{binario}\n')214        if txt_cript == 'PySide2.QtCore.Qt.CheckState.Checked':215            criptografar(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt')216            os.remove(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt')217            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':218                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.txt.encrypted', 'text/plain',219                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt.encrypted')220            if ssh_enviar and serverx:221                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt.encrypted',222                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.txt.encrypted', key=key)223            if aws_s3_gerar and serverx:224                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)225        else:226            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':227                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.txt', 'text/plain',228                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt')229            if ssh_enviar and serverx:230                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.txt',231                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.txt', key=key)232            if aws_s3_gerar and serverx:233                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)234    if docx_f == 'PySide2.QtCore.Qt.CheckState.Checked':235        hash_file = random.getrandbits(23)236        if docx_ocult == 'PySide2.QtCore.Qt.CheckState.Checked':237            document = Document()238            for binario in conteudo:239                if not 'N/A' in binario:240                    binario = binario.encode('utf-16', 'replace').decode('utf-16')241                    binario_repor = binario.split(':')242                    senha = binario_repor[1]243                    senha_ocult = ocultar_senhas(senha)244                    binario = binario.replace(senha, senha_ocult)245                    document.add_paragraph(binario)246            document.save(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.docx')247        if docx_ocult == 'PySide2.QtCore.Qt.CheckState.Unchecked':248            document = Document()249            for binario in conteudo:250                binario = binario.encode('utf-16', 'replace').decode('utf-16')251                document.add_paragraph(binario)252            document.save(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.docx')253        if docx_cript == 'PySide2.QtCore.Qt.CheckState.Checked':254            criptografar(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.docx')255            os.remove(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.docx')256            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':257                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.docx.encrypted',258                                         'text/plain',259                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.docx.encrypted')260            if ssh_enviar and serverx:261                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.docx.encrypted',262                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.docx.encrypted', key=key)263            if aws_s3_gerar and serverx:264                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)265        else:266            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':267                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.docx',268                                         'text/plain',269                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.docx')270            if ssh_enviar and serverx:271                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.docx',272                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.docx', key=key)273            if aws_s3_gerar and serverx:274                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)275    if pdf_f == 'PySide2.QtCore.Qt.CheckState.Checked':276        hash_file = random.getrandbits(23)277        if pdf_ocult == 'PySide2.QtCore.Qt.CheckState.Checked':278            dummytext = ""279            for binario in conteudo:280                if not 'N/A' in binario:281                    binario = binario.encode('latin-1', 'replace').decode('latin-1')282                    binario_repor = binario.split(':')283                    senha = binario_repor[1]284                    senha_ocult = ocultar_senhas(senha)285                    binario = binario.replace(senha, senha_ocult)286                    dummytext += f'{binario}\n\n'287            pdf = FPDF()288            pdf.add_page()289            pdf.set_xy(5, 5)290            pdf.set_font('arial', 'B', 12.0)291            pdf.multi_cell(0, 5, dummytext)292            pdf.output(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf', 'F')293        if pdf_ocult == 'PySide2.QtCore.Qt.CheckState.Unchecked':294            dummytext = ""295            for binario in conteudo:296                binario = binario.encode('latin-1', 'replace').decode('latin-1')297                dummytext += f'{binario}\n\n'298            pdf = FPDF()299            pdf.add_page()300            pdf.set_xy(5, 5)301            pdf.set_font('arial', 'B', 12.0)302            pdf.multi_cell(0, 5, dummytext)303            pdf.output(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf', 'F')304        if pdf_cript == 'PySide2.QtCore.Qt.CheckState.Checked':305            criptografar(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf')306            os.remove(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf')307            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':308                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf.encrypted',309                                         'text/plain',310                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf.encrypted')311            if ssh_enviar and serverx:312                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf.encrypted',313                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf.encrypted', key=key)314            if aws_s3_gerar and serverx:315                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)316        else:317            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':318                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf',319                                         'text/plain',320                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf')321            if ssh_enviar and serverx:322                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf',323                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.pdf', key=key)324            if aws_s3_gerar and serverx:325                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)326    if xlsx_f == 'PySide2.QtCore.Qt.CheckState.Checked':327        hash_file = random.getrandbits(23)328        if xlsx_ocult == 'PySide2.QtCore.Qt.CheckState.Checked':329            x = 0330            y = 0331            style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',332                                 num_format_str='#,##0.00')333            style1 = xlwt.easyxf(num_format_str='D-MMM-YY')334            wb = xlwt.Workbook()335            ws = wb.add_sheet('A Test Sheet')336            try:337                for binario in conteudo:338                        if not 'N/A' in binario:339                            binario = binario.encode('utf-16', 'replace').decode('utf-16')340                            binario_repor = binario.split(':')341                            senha = binario_repor[1]342                            senha_ocult = ocultar_senhas(senha)343                            binario = binario.replace(senha, senha_ocult)344                        binario_x = binario.split(':')345                        if y >= 3:346                            y = 0347                        for kls in binario_x:348                            ws.write(x, y, kls)349                            y += 1350                        x += 1351                        wb.save(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xls')352            except:353                string_grande(criar=xlsx_f, criar_ocult=xlsx_ocult, criar_cript=xlsx_cript, documentos=documentos,354                              nome=nome, data_atual=data_atual, binario=conteudo, ssh_enviar=ssh_enviar,355                              id_pasta=id_pasta,356                              gdrive=gdrive, aws_s3_gerar=aws_s3_gerar, serverx=serverx, key=key)357        if xlsx_ocult == 'PySide2.QtCore.Qt.CheckState.Unchecked':358            hash_file = random.getrandbits(23)359            x = 0360            y = 0361            style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',362                                 num_format_str='#,##0.00')363            style1 = xlwt.easyxf(num_format_str='D-MMM-YY')364            wb = xlwt.Workbook()365            ws = wb.add_sheet('A Test Sheet')366            try:367                for binario in conteudo:368                    binario = binario.encode('utf-16', 'replace').decode('utf-16')369                    binario_x = binario.split(':')370                    if y >= 3:371                        y = 0372                    for kls in binario_x:373                        ws.write(x, y, kls)374                        y += 1375                    x += 1376                    wb.save(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xls')377            except:378                string_grande(criar=xlsx_f, criar_ocult=xlsx_ocult, criar_cript=xlsx_cript, documentos=documentos,379                              nome=nome, data_atual=data_atual, binario=conteudo, ssh_enviar=ssh_enviar,380                              id_pasta=id_pasta,381                              gdrive=gdrive, aws_s3_gerar=aws_s3_gerar, serverx=serverx, key=key)382        if xlsx_cript == 'PySide2.QtCore.Qt.CheckState.Checked':383            criptografar(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xls')384            os.remove(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xls')385            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':386                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.xls.encrypted',387                                         'text/plain',388                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xls.encrypted')389            if ssh_enviar and serverx:390                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xls.encrypted',391                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.xls.encrypted', key=key)392            if aws_s3_gerar and serverx:393                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)394        else:395            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':396                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.xls',397                                         'text/plain',398                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xls')399            if ssh_enviar and serverx:400                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xls',401                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.xls', key=key)402            if aws_s3_gerar and serverx:403                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)404    if json_f == 'PySide2.QtCore.Qt.CheckState.Checked':405        hash_file = random.getrandbits(23)406        if json_ocult == 'PySide2.QtCore.Qt.CheckState.Checked':407            with open(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.json', 'w', encoding='utf-16') as arquivo:408                temp_lista = []409                for binario in conteudo:410                    if not 'N/A' in binario:411                        #binario = binario.encode('utf-16', 'replace').decode('utf-16')412                        binario_repor = binario.split(':')413                        senha = binario_repor[1]414                        senha_ocult = ocultar_senhas(senha)415                        binario = binario.replace(senha, senha_ocult)416                        tmp_lista = []417                        final_list = []418                        tmp_lista.append(binario)419                        temp_lista.append(tmp_lista)420                json.dump(temp_lista, arquivo)421        if json_ocult == 'PySide2.QtCore.Qt.CheckState.Unchecked':422            with open(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.json', 'w', encoding='utf-16') as arquivo:423                temp_lista = []424                for binario in conteudo:425                    if not 'N/A' in binario:426                        #binario = binario.encode('utf-16', 'replace').decode('utf-16')427                        tmp_lista = []428                        final_list = []429                        tmp_lista.append(binario)430                        temp_lista.append(tmp_lista)431                json.dump(temp_lista, arquivo)432        if json_cript == 'PySide2.QtCore.Qt.CheckState.Checked':433            criptografar(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.json')434            os.remove(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.json')435            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':436                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.json.encrypted',437                                         'text/plain',438                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.json.encrypted')439            if ssh_enviar and serverx:440                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.json.encrypted',441                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.json.encrypted', key=key)442            if aws_s3_gerar and serverx:443                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)444        else:445            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':446                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.json',447                                         'text/plain',448                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.json')449            if ssh_enviar and serverx:450                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.json',451                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.json', key=key)452            if aws_s3_gerar and serverx:453                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)454    if html_f == 'PySide2.QtCore.Qt.CheckState.Checked':455        hash_file = random.getrandbits(23)456        if html_ocult == 'PySide2.QtCore.Qt.CheckState.Checked':457            with open(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.html', 'w+', encoding='utf-16') as arquivo:458                arquivo.write(f'<!DOCTYPE html>')459                for binario in conteudo:460                    if not 'N/A' in binario:461                        binario = binario.encode('utf-16', 'replace').decode('utf-16')462                        binario_repor = binario.split(':')463                        senha = binario_repor[1]464                        senha_ocult = ocultar_senhas(senha)465                        binario = binario.replace(senha, senha_ocult)466                        arquivo.write(f'<div>{binario}</div>')467                        arquivo.write(f'</br>')468        if html_ocult == 'PySide2.QtCore.Qt.CheckState.Unchecked':469            with open(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.html', 'w+', encoding='utf-16') as arquivo:470                arquivo.write(f'<!DOCTYPE html>')471                for binario in conteudo:472                    binario = binario.encode('utf-16', 'replace').decode('utf-16')473                    arquivo.write(f'<div>{binario}</div>')474                    arquivo.write(f'</br>')475        if html_cript == 'PySide2.QtCore.Qt.CheckState.Checked':476            criptografar(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.html')477            os.remove(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.html')478            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':479                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.html.encrypted',480                                         'text/plain',481                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.html.encrypted')482            if ssh_enviar and serverx:483                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.html.encrypted',484                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.html.encrypted', key=key)485            if aws_s3_gerar and serverx:486                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)487        else:488            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':489                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.html',490                                         'text/plain',491                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.html')492            if ssh_enviar and serverx:493                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.html',494                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.html', key=key)495            if aws_s3_gerar and serverx:496                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)497    if xsl_f == 'PySide2.QtCore.Qt.CheckState.Checked':498        hash_file = random.getrandbits(23)499        if xsl_ocult == 'PySide2.QtCore.Qt.CheckState.Checked':500            root = ET.Element("oblivion")501            doc = ET.SubElement(root, "leak")502            for binario in conteudo:503                if not 'N/A' in binario:504                    binario = binario.encode('utf-16', 'replace').decode('utf-16')505                    binario_repor = binario.split(':')506                    senha = binario_repor[1]507                    senha_ocult = ocultar_senhas(senha)508                    binario = binario.replace(senha, senha_ocult)509                    ET.SubElement(doc, f"field", name="leak").text = binario510            tree = ET.ElementTree(root)511            tree.write(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xml')512        if xsl_ocult == 'PySide2.QtCore.Qt.CheckState.Unchecked':513            root = ET.Element("oblivion")514            doc = ET.SubElement(root, "leak")515            for binario in conteudo:516                binario = binario.encode('utf-16', 'replace').decode('utf-16')517                ET.SubElement(doc, f"field", name="leak").text = binario518            tree = ET.ElementTree(root)519            tree.write(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xml')520        if xsl_cript == 'PySide2.QtCore.Qt.CheckState.Checked':521            criptografar(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xml')522            os.remove(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xml')523            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':524                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.xml.encrypted',525                                         'text/plain',526                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xml.encrypted')527            if ssh_enviar and serverx:528                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xml.encrypted',529                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.xml.encrypted', key=key)530            if aws_s3_gerar and serverx:531                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)532        else:533            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':534                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.xml',535                                         'text/plain',536                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xml')537            if ssh_enviar and serverx:538                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.xml',539                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.xml', key=key)540            if aws_s3_gerar and serverx:541                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)542    if db_f == 'PySide2.QtCore.Qt.CheckState.Checked':543        hash_file = random.getrandbits(23)544        if db_ocult == 'PySide2.QtCore.Qt.CheckState.Checked':545            x = 0546            conn = sqlite3.connect(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.db')547            c = conn.cursor()548            try:549                c.execute('''CREATE TABLE IF NOT EXISTS leaks550                                          (email text, password text, origin text, data text, source text)''')551                for binario in conteudo:552                    if not 'N/A' in binario:553                        binario = binario.encode('utf-16', 'replace').decode('utf-16')554                        binario_repor = binario.split(':')555                        senha = binario_repor[1]556                        senha_ocult = ocultar_senhas(senha)557                        binario = binario.replace(senha, senha_ocult)558                    binario_lit = binario.split(':')559                    y = (binario_lit[0])560                    c.execute(f'''INSERT INTO leaks561                                             VALUES('{binario_lit[0]}', '{binario_lit[1]}', '{binario_lit[2]}', '{binario_lit[-1]}', '{binario}')''')562                conn.commit()563                conn.close()564            except:565                string_grande(criar=db_f, criar_ocult=db_ocult, criar_cript=db_cript, documentos=documentos,566                              nome=nome, data_atual=data_atual, binario=conteudo, ssh_enviar=ssh_enviar,567                              id_pasta=id_pasta,568                              gdrive=gdrive, aws_s3_gerar=aws_s3_gerar, serverx=serverx, key=key)569        if db_ocult == 'PySide2.QtCore.Qt.CheckState.Unchecked':570            try:571                x = 0572                conn = sqlite3.connect(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.db')573                c = conn.cursor()574                c.execute('''CREATE TABLE IF NOT EXISTS leaks575                              (email text, password text, origin text, data text, source text)''')576                for binario in conteudo:577                    binario_lit = binario.split(':')578                    y = (binario_lit[0])579                    c.execute(f'''INSERT INTO leaks580                                 VALUES('{binario_lit[0]}', '{binario_lit[1]}', '{binario_lit[2]}', '{binario_lit[-1]}', '{binario}')''')581                conn.commit()582                conn.close()583            except:584                string_grande(criar=xlsx_f, criar_ocult=xlsx_ocult, criar_cript=xlsx_cript, documentos=documentos,585                              nome=nome, data_atual=data_atual, binario=conteudo, ssh_enviar=ssh_enviar,586                              id_pasta=id_pasta,587                              gdrive=gdrive, aws_s3_gerar=aws_s3_gerar, serverx=serverx, key=key)588        if db_cript == 'PySide2.QtCore.Qt.CheckState.Checked':589            criptografar(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.db')590            os.remove(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.db')591            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':592                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.db.encrypted',593                                         'text/plain',594                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.db.encrypted')595            if ssh_enviar and serverx:596                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.db.encrypted',597                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.db.encrypted', key=key)598            if aws_s3_gerar and serverx:599                subir_bucket_s3(f'{documentos}/Oblivion/', key=key)600        else:601            if gdrive == 'PySide2.QtCore.Qt.CheckState.Checked':602                subir_arquivo_drive_raiz(id_pasta, f'{nome}resultados_oblivion_{data_atual}_{hash_file}.db',603                                         'text/plain',604                                         f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.db')605            if ssh_enviar and serverx:606                ssh_enviar_arquivo(f'{documentos}/Oblivion/{nome}resultados_oblivion_{data_atual}_{hash_file}.db',607                                   f'{nome}resultados_oblivion_{data_atual}_{hash_file}.db', key=key)608            if aws_s3_gerar and serverx:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
