Beispiel #1
0
def init_client(c=None):
    global client

    if c is None:
        c = putio.Client(app.config['PUTIO_TOKEN'], use_retry=True)
    client = c
    return c
def download_file(id, delete_after_download=True, download_dir="./"):
    putio_client = putiopy.Client(config_map['oauth'])
    file = putio_client.File.get(int(id))
    print('Downloading file: {}'.format(str(file)))
    file.download(dest=download_dir, delete_after_download=delete_after_download)
    print('Download complete!')
    return file.name
    def __init__(self, **kwargs):
        """Put.io Aria2c downloader.

        Attributes:
            keep_folder_structure (bool): keep the same structure as on put.io
            aria2c_secret_token (str): aria2c rpc secret token
            incomplete_dir (int): put.io folder to start recursing from
            complete_dir (str): Where to move completed downloads
            watch_list (dict): folders to watch in incomplete_dir on put.io
            putio_client: putio.py library client object
            aria_client: aria2c xmlrpc client object

        """
        self.logger = logging.getLogger('putio.' + __name__)

        self.keep_folder_structure = kwargs.get('keep_folder_structure', True)
        aria2c_secret_token = kwargs.get("aria2c_secret_token")
        self.aria2c_secret_token = f'token:{aria2c_secret_token}'
        self.incomplete_dir = kwargs.get('incomplete_dir', tempfile.mkdtemp())
        self.complete_dir = kwargs.get('complete_dir', os.getcwd())
        self.watch_list = kwargs.get('watch_folders', [])

        self.putio_client = putiopy.Client(kwargs.get('oauth_token'),
                                           use_retry=True,
                                           timeout=10)
        self.aria_client = xmlrpc.client.ServerProxy(kwargs.get('rpc_url'))
    def __init__(self, **kwargs):
        """Putio Aria2c Downloader

        Attributes:
            keep_folder_structure (bool): Whether or not to keep the same folder structure as
                on put.io
            aria2c_secret_token (str): aria2c rpc secret token
            root_download_dir (int): Which folder id (on put.io) to start recursing from
            post_process_dir (str): Where to move completed downloads
            watch_list (dict): folder names to watch inside root_download_dir on put.io
            putio_client: putiopy library client object
            aria_client: aria2c xmlrpc client object
        """
        self.logger = logging.getLogger('putio.' + __name__)

        self.keep_folder_structure = kwargs.get('keep_folder_structure', True)
        self.aria2c_secret_token = 'token:%s' % kwargs.get(
            'aria2c_secret_token', None)
        self.root_download_dir = kwargs.get('root_download_dir',
                                            tempfile.mkdtemp())
        self.post_process_dir = kwargs.get('post_process_dir', os.getcwd())
        self.watch_list = kwargs.get('watch_folders', [])

        self.putio_client = putiopy.Client(kwargs.get('oauth_token', None))
        self.aria_client = xmlrpc.client.ServerProxy(
            kwargs.get('rpc_url', None))
Beispiel #5
0
 def setUp(self):
     self.fd, app.config['DATABASE'] = tempfile.mkstemp()
     app.config['TESTING'] = True
     app.config['TORRENTS'] = tempfile.mkdtemp()
     self.app = app.test_client()
     init_db()
     self.client = init_client(putio.Client('123456'))
Beispiel #6
0
 def __init__(self):
     manager = Manager()
     self.pcloud = PyCloud(PCLOUD_USER, PCLOUD_PASS)
     self.putio = putiopy.Client(PUTIO_KEY)
     self.download_list = manager.list()
     self.upload_list = manager.list()
     self.files_left = manager.Value(1, 0)
     self.destination = None
Beispiel #7
0
def main(config):
    client = putiopy.Client(config['oauth_token'])
    root_files = client.File.list()

    for file in root_files:
        if file.name in config['public_directories']:
            purge_old_files(client, file, config['delete_after_days'])

        if file.name not in config['allowed_root_files']:
            print(f'Deleting unauthorized root file {file.name}')
            file.delete()
def auto_delete(oauth_token: str,
                max_age: "num",
                dryrun: bool,
                excluded_dirs: list = []):
    """Deletes files older than `max_age` days from the putio account associated with
    the `oauth_token`. The folders (NOT their contents) in `excluded_dirs` will be
    left even if they match the age criteria.
    Returns True if files have been found/deleted matching the age criteria.
    False if no files were found.
    """

    client = putiopy.Client(oauth_token)

    max_age_delta = datetime.timedelta(days=max_age)
    cutoff_date = datetime.datetime.today() - max_age_delta

    files = client.File.list(
        parent_id=-1, file_type="FILE,AUDIO,VIDEO,IMAGE,ARCHIVE,PDF,TEXT,SWF")
    old_files = [file_ for file_ in files if file_.created_at < cutoff_date]

    for file_ in old_files:
        if dryrun:
            print("      Old file Found: {}".format(file_.name))
        else:
            try:
                file_.delete(True)
            except:
                print("[x] Could not delete: {}".format(file_.name))
            else:
                print("        File Deleted: {}".format(file_.name))
            time.sleep(0.2)

    folders = client.File.list(parent_id=-1, file_type="FOLDER")
    old_folders = [
        folder for folder in folders if folder.created_at < cutoff_date
        and folder.size == 0 and folder.name not in excluded_dirs
    ]

    for folder in old_folders:
        if dryrun:
            print("    Old folder Found: {}".format(folder.name))
        else:
            try:
                folder.delete(True)
            except:
                print("[x] Could not delete: {}".format(folder.name))
            else:
                print("      Folder Deleted: {}".format(folder.name))
            time.sleep(0.2)

    if old_files or old_folders:
        return True
    else:
        return False
Beispiel #9
0
def get_client():
    global client
    if not os.path.isfile("holeio.cfg"):
        return None
    if client:
        return client
    config = ConfigParser.RawConfigParser()
    config.read("holeio.cfg")
    token = config.get('oauth', 'token')
    if token:
        client = putiopy.Client(token)
        return client
    logger.error('No oauth token set')
Beispiel #10
0
    def __init__(self, options):
        # update options from config file
        config = ConfigParser.RawConfigParser()
        config.read(os.path.expanduser(options['--config']))
        for section in config.sections():
            for key, value in config.items(section):
                key = section + '.' + key
                options[key] = value

        Base.__init__(self, options)

        # define putio client
        self.client = putiopy.Client(options['Settings.oauth-token'])
Beispiel #11
0
def MovieFinder(moviename):

    response = ''
    link = MovieSearch(moviename)

    if link == 'No results found':
        response = 'No results found for' + moviename
    elif "1080p" in link:
        try:
            client = putiopy.Client(pkey.OAUTH_KEY)
            client.Transfer.add_url(link)
            response = "You'll be able to watch " + moviename + " on Put IO soon..."
        except Exception:
            return 'Something went wrong adding file to Put IO.'
    else:
        # instantiate a Client using the appropriate WebUI configuration
        qbt_client = qbit.Client(host=qbt_auth['host'],
                                 username=qbt_auth['username'],
                                 password=qbt_auth['password'])

        # the Client will automatically acquire/maintain a logged in state in line with any request.
        # therefore, this is not necessary; however, you many want to test the provided login credentials.
        try:
            qbt_client.auth_log_in()
        except qbit.LoginFailed:
            return 'Could not authenticate. Qbt is probably down.'

        # add download link to qbt client
        # help(qbt_client.torrents_add)
        try:
            qbt_client.torrents_add(urls=link,
                                    save_path=path_to_save,
                                    is_paused=False)
            response = "You'll be able to watch " + moviename + " in 4K soon..."
        except Exception:
            return 'Something went wrong adding file to Qbt.'

    try:
        with open("/var/www/piAppSwitcher/MovieFinder/MFResponses.txt",
                  mode='a+') as txtfile:
            txtfile.write('On ' +
                          datetime.now().strftime("%Y-%m-%d %H:%M:%S") +
                          ' MovieFinder sent response "' + response + '".\n')
    except IOError:
        print("Couldn't write to file!")

    # Send response to Telegram Chat
    payload = dict(chat_id=tkeys.chat_id, text=response)
    requests.post(tkeys.url, data=payload)

    return response
def ConfigurePutIOClient(id_IN, secret_IN, token_IN, callback_IN,
                         authenticate_IN):
    # Just annoying numbers and values to keep track of...
    CLIENT_ID = id_IN
    CLIENT_SECRET = secret_IN
    OAUTH_TOKEN = token_IN
    CALLBACK_URL = callback_IN

    # Some authentication code I copy pasted from their github.
    # Only works if specified by function call.
    if (authenticate_IN):
        helper = putiopy.AuthHelper(CLIENT_ID,
                                    CLIENT_SECRET,
                                    CALLBACK_URL,
                                    type='token')
        helper.open_authentication_url()

    # Set client here.
    client = putiopy.Client(OAUTH_TOKEN)

    return client
Beispiel #13
0
    def add_entries(self, task, config):
        """ Main function. """
        client = putiopy.Client(config.get("token"))
        for entry in task.accepted:
            form_data = {}
	    if entry.get('dir'):
	        form_data['dir'] = entry.render(entry['dir'])
                dir_id = self.get_directory_id(client, form_data, config)
                if dir_id == 0:
	            self.create_folder(client, form_data, config)
                    dir_id = self.get_directory_id(client, form_data, config)
                log.info("Destination directory is %s", form_data.get("dir"))
            else:
   	        dir_id = config.get("root_dir_id")
  	
	    log.info("Destination directory ID is %s", dir_id)

            """ Adds a new transfer. """
            self.add_torrent_url(client, entry['url'], dir_id)
	    
            """ Clean completed transfer. """
            if config.get('clean_completed'):
                self.clean_completed(client)
Beispiel #14
0
    def __init__(self):
        global config

        self.putio = putiopy.Client(config["token"])
        self.config = config
Beispiel #15
0
import putiopy
from ring_doorbell import Ring
import config
#Have the ring videos downloaded to put.io


helper = putiopy.AuthHelper(config.client, config.application_secret,
                            "https://webhook.site/6e8bc62f-477b-44e6-a67f-9c39795eefb9", type='token')

client = putiopy.Client(config.token)
helper.open_authentication_url()




#apples = client.File.search("6740617731310688069", 1)
#print(apples)
List_files_on_putio = client.File.list()
print(List_files_on_putio)

#print(List_files_on_putio[2:5])
mylist = List_files_on_putio.copy()


testlist = ["6742976248176777029", "6742974031973652293", "6742972232382355269", "6742971635381901125", "6742971343324124997"]


#TODO: get rid of the char del, its no longer needed since getting mylist[x].id no longer cuts off part of the id.
#remove the last two characters in the string for each.
newtestlist = [x[:-2] for x in testlist]
#print("New test list is now " + str(newtestlist))
Beispiel #16
0
def ringtoputio():

    ringdict = defaultdict(list)

    videocount = total_video_count(
    )  # video count holds the total amount of videos
    numberofvideos = int(
        input("How many videos do you want to be sent to put.io?\n\n" +
              "The larger the number of videos "
              "requestd, the longer it will take "
              "to complete :"))
    downloasdurl = []  # this will hold all of the download urls.

    eventidlist = []  # the list that will hold the video ID's
    myring = Ring(
        config.username,
        config.password)  # enters the password and username for ring.
    doorbell = myring.doorbells[
        0]  # gets the first doorbell found in the ring list.

    # if the number of videos asked for is less than 100
    if numberofvideos < 100:

        print("number of videos requested is less than 100.\n\n")

        for doorbell in myring.doorbells:

            # list the number of events requested by user
            for event in doorbell.history(limit=numberofvideos):
                eventidlist.append(
                    event['id'])  # appends the eventids to the eventidlist.
                ringdict["Date"].append(event['created_at'])
                ringdict["ID"].append(event['id'])
                print(ringdict)
            print("ringIDs in the dict is " + str(ringdict["ID"]))
            print("the length of eventid list is " + str(
                len(eventidlist)))  # prints the length of list id eventidlist
            print("eventidlist is " + str(eventidlist)
                  )  # prints out all of the items in the eventID list.

    else:

        for doorbell in myring.doorbells:

            # listing the last 100 events of any kind
            for event in doorbell.history(limit=100):
                eventidlist.append(
                    event['id'])  # appends the eventids to the eventidlist.
            print("the length of eventid list is " + str(len(eventidlist)))
            print("eventidlist is ...\n\n" + str(eventidlist))
            histroy = doorbell.history(limit=100, older_than=eventidlist[-1])

        while (len(eventidlist) < numberofvideos):
            histroy = doorbell.history(limit=100, older_than=eventidlist[-1])

            for event in histroy:
                # print('ID:       %s' % event['id'])
                eventidlist.append(event['id'])  # adds the IDs to the list.
                eventidlist = list(dict.fromkeys(
                    eventidlist))  # removes any duplicates in the list.
            print("the length of eventid list is " + str(len(eventidlist)))
            print("event id list is " + str(eventidlist))

    print(
        "Time to start getting the video links for the ring videos\n\n This will take a while"
    )

    for x in eventidlist:
        adddownloadurl = doorbell.recording_url(x)
        downloasdurl.append(adddownloadurl)
        print(downloasdurl)
        print("Number of Video links obtained " + str(len(downloasdurl)) +
              " / " + str(numberofvideos))

    #Have the ring videos downloaded to put.io

    helper = putiopy.AuthHelper(
        config.client,
        config.application_secret,
        "https://webhook.site/6e8bc62f-477b-44e6-a67f-9c39795eefb9",
        type='token')

    client = putiopy.Client(config.token)
    helper.open_authentication_url()

    lengthofdownloadlist = len(downloasdurl)
    for i in range(lengthofdownloadlist):

        transfer = client.Transfer.add_url(str(downloasdurl[i]))
        print("progress on sending links to put.io" + str(i + 1) + "/" +
              str(len(downloasdurl))
              )  # shows the number of links sent to put.io so far.

    List_files_on_putio = client.File.list()
    print(List_files_on_putio)

    # print(List_files_on_putio[2:5])
    mylist = List_files_on_putio.copy()
    #print("The min and high list value of the mylist" + min(mylist), max(mylist))

    #TODO: Should try using this one instead.  Add a delay that will make sure all of the transfers are done then check the files.

    #for x in range(0, int(len(ringdict))):
    #any(ringdict["ID"] in ringdict for ringdict["ID"] in mylist)
    #print("I FOUND SOMETHING" + str(mylist[x].name))

    for index, item in enumerate(mylist):
        for id in ringdict["ID"]:
            if str(id) in item["name"]:
                print(f"I found {id} at index {index}")
def upload_torrent_to_putio(token, filename, parent_id):
    client = putiopy.Client(token)
    client.Transfer.add_torrent(filename, parent_id)
    os.remove(filename)
Beispiel #18
0
def get(token: str, file_id: int):
    def log(*args):
        print(f'[{token[:4]}]', *args)

    client = putiopy.Client(token)
    files: Dict[int, File] = {}
    processed = 0
    try:
        root = client.File.get(file_id)
    except putiopy.ClientError as e:
        if e.response.status_code == 401 and file_id == 0:
            raise ExpiredToken
        raise

    total_size = root.size
    log("total size of file(%d): %d gb" % (file_id, total_size // 2**30))

    def append_children_recursive(putio_file, total_sizes: Queue,
                                  level: int) -> None:
        nonlocal processed
        children = list_children(putio_file.id, client)
        threads = []
        children_sizes: Queue[int] = Queue()
        for child in children:
            if child.folder_type == 'SHARED_ROOT':
                continue

            if (level < MAX_LEVEL
                    and child.content_type == 'application/x-directory'):
                t = threading.Thread(target=append_children_recursive,
                                     args=(child, children_sizes, level + 1))
                t.start()
                threads.append(t)
            else:
                children_sizes.put(child.size)
                files[child.id] = File(child, child.size)
                processed += child.size
                log("processed %d of %d gb" %
                    (processed // 2**30, total_size // 2**30))

        for t in threads:
            t.join()

        children_size = 0
        while True:
            try:
                children_size += children_sizes.get_nowait()
            except Empty:
                break

        dir_size = putio_file.size
        if children_size > dir_size:
            dir_size = children_size

        total_sizes.put(dir_size)
        files[putio_file.id] = File(putio_file, dir_size)

    start = time.time()
    append_children_recursive(root, Queue(), level=1)
    end = time.time()
    log('file tree traversed in %s seconds' % (end - start))

    ids = []
    labels = []
    values = []
    parents = []

    for id, file in files.items():
        ids.append(file.id)
        labels.append(file.name)
        values.append(file.size)
        try:
            parents.append(files[file.parent_id].id)
        except KeyError:
            parents.append('')

    fig = go.Figure(
        go.Treemap(
            ids=ids,
            labels=labels,
            values=values,
            parents=parents,
            branchvalues='total',
            texttemplate='%{label}',
            hovertemplate='%{label}<br>%{value:.2s}<extra></extra>',
        ))
    return fig.to_html(include_plotlyjs='cdn')
Beispiel #19
0
def start_sync(args):

    formatter = logging.Formatter(
        '%(asctime)s | %(name)-12s | %(levelname)-8s | %(message)s')

    log_level = logging.ERROR
    if args.log_level is not None:
        if args.log_level == "debug":
            log_level = logging.DEBUG
        elif args.log_level == "info":
            log_level = logging.INFO
        elif args.log_level == "warning":
            log_level = logging.WARNING
        elif args.log_level == "error":
            log_level = logging.ERROR
        elif args.log_level == "critical":
            log_level = logging.CRITICAL
        else:
            print("Invalid log-level argument")

    ch = logging.StreamHandler()
    ch.setLevel(log_level)
    ch.setFormatter(formatter)

    if args.log is not None:
        fh = logging.FileHandler(args.log)
        fh.setLevel(log_level)
        fh.setFormatter(formatter)
        logger.addHandler(fh)
    else:
        logger.addHandler(ch)

    log_webif = logging.getLogger('werkzeug')
    log_webif.setLevel(log_level)
    log_webif.disabled = True

    if args.log_webif is not None:
        fh = logging.FileHandler(args.log_webif)
        fh.setLevel(log_level)
        fh.setFormatter(formatter)
        log_webif.addHandler(fh)
    else:
        log_webif.addHandler(ch)

    # Restore or obtain a valid token
    token_manager = TokenManager()
    token = token_manager.get_token()
    while not token_manager.is_valid_token(token):
        print("No valid token found!  Please provide one.")
        token = token_manager.obtain_token()
    token_manager.save_token(token)

    # Let's start syncing!
    putio_client = putiopy.Client(token)
    db_manager = DatabaseManager()
    download_manager = DownloadManager(token=token)
    if args.post_process_command is not None:
        download_manager.add_download_completion_callback(
            build_postprocess_download_completion_callback(
                args.post_process_command))

    if args.watch_directory is not None:
        torrent_watcher = TorrentWatcher(args.watch_directory, putio_client)
        torrent_watcher.start()

    filter_compiled = None
    if args.filter is not None:
        try:
            filter_compiled = re.compile(args.filter)
        except re.error as e:
            print("Invalid filter regex: {0}".format(e))
            exit(1)

    force_keep_compiled = None
    if args.force_keep is not None:
        try:
            force_keep_compiled = re.compile(args.force_keep)
        except re.error as e:
            print("Invalid force_keep regex: {0}".format(e))
            exit(1)

    download_manager.start()
    synchronizer = PutioSynchronizer(
        download_directory=args.download_directory,
        putio_client=putio_client,
        db_manager=db_manager,
        download_manager=download_manager,
        keep_files=args.keep,
        poll_frequency=args.poll_frequency,
        download_filter=filter_compiled,
        force_keep=force_keep_compiled,
        disable_progress=args.log is not None)
    t = threading.Thread(target=synchronizer.run_forever)
    t.setDaemon(True)
    t.start()
    web_interface = WebInterface(db_manager,
                                 download_manager,
                                 putio_client,
                                 synchronizer,
                                 launch_browser=(not args.quiet),
                                 host=args.host,
                                 port=args.port)
    web_interface.run()
Beispiel #20
0
def create_client(putio_token):
    return putiopy.Client(putio_token)
Beispiel #21
0
 def __init__(self, token):
     self._client = putiopy.Client(token)
Beispiel #22
0
import json
import logging
import os
import putiopy
import requests

from dumbdb import DumbDB

logging.basicConfig(
    level=getattr(logging,
                  os.environ.get('LOG_LEVEL', 'warning').upper()))
log = logging.getLogger()

client = putiopy.Client(os.environ['PUTIO_OAUTH_TOKEN'])
downloaded_files = DumbDB('downloaded_before.txt')


def get_missing_files(file_resources):
    missing_files = []
    for file_resource in file_resources:
        log.debug(' ↦ Analysing %s %d %s', file_resource.file_type,
                  file_resource.id, file_resource.name)

        log.debug('   ↦ Maybe wanted %d %s', file_resource.id,
                  file_resource.name)

        if downloaded_files.exists(file_resource.id):
            continue

        missing_files.append(file_resource)
    CallbackQueryHandler,
    # ConversationHandler,
    # MessageHandler,
    # Filters,
    # RegexHandler,
)

# LOGGER for warnings, errors, debug outputs etc.
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO)

# fundamentals
logger = logging.getLogger(__name__)
bot_token = os.environ['BOT_TOKEN']
client = putiopy.Client(os.environ['PUT_IO_TOKEN'])

# put.io folder to collect user files (/BOT_USERS/)
USERS_DIR_ID = 111111111


# catch errors
def error(bot, update, error):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, error)


# /start function
def start(bot, update, user_data):
    # create user instance in the db if doesn't exist
    markup = InlineKeyboardMarkup(MAIN_MENU)
Beispiel #24
0
 def setUp(self):
     self.client = putiopy.Client('123456')
     self.destination = tempfile.mkdtemp()
Beispiel #25
0
def create_client(app):
    "Create a Put.IO client"
    if 'PUTIO_TOKEN' in app.config:
        client = putio.Client(app.config['PUTIO_TOKEN'], use_retry=True)

        return client