Пример #1
0
class GDrive(object):
    def __init__(self, settings, folder):
        self.settings = settings
        self.folder_id = folder
        self.auth = None
        self.drive = None

        self.team_drive = self.settings['team_drive']

    def get_name(self):
        return 'Google Drive'

    def connect(self, filename='credentials.json'):
        self.auth = GoogleAuth()
        self.auth.LoadCredentialsFile(filename)

        if self.auth.credentials is None:
            if sys.platform == 'win32':
                self.auth.LocalWebserverAuth()
            else:
                raise Exception('Google Drive credentials have expired.')
        elif self.auth.access_token_expired:
            self.auth.Refresh()
        else:
            self.auth.Authorize()

        self.auth.SaveCredentialsFile(filename)
        self.drive = GoogleDrive(self.auth)
        self.root_folders = self.drive.list_folders_in(self.folder_id)

    def create_folder(self, name, folder_id):
        folder = self.drive.CreateFile({
            'title':
            name,
            'parents': [{
                'kind': 'drive#fileLink',
                'teamDriveId': self.team_drive,
                'id': folder_id
            }],
            'mimeType':
            FOLDER_MIME
        })
        folder.Upload(param={'supportsTeamDrives': True})
        return folder

    def create_folder_in_root(self, name):
        folder = self.create_folder(name, self.folder_id)
        self.root_folders.append(folder)
        return folder

    def upload_file(self, source_filename, folder_path, filename):
        file = self.drive.CreateFile({
            'title':
            filename,
            'parents': [{
                'kind': 'drive#fileLink',
                'teamDriveId': self.team_drive,
                'id': folder_path
            }]
        })
        file.SetContentFile(source_filename)
        file.Upload(param={'supportsTeamDrives': True})
        return file

    def search_files(self, query):
        return self.drive.ListFile({
            'q': query,
            'corpora': 'teamDrive',
            'teamDriveId': self.team_drive,
            'includeTeamDriveItems': 'true',
            'supportsTeamDrives': 'true',
            'maxResults': 20000
        }).GetList()

    def list_folders_in(self, folder_id):
        return self.search_files(
            "'{0}' in parents and trashed=false and mimeType='{1}'".format(
                folder_id, FOLDER_MIME))

    def list_files_in(self, folder_id):
        return self.search_files(
            "'{0}' in parents and trashed=false".format(folder_id))

    def search_for_file_in(self, folder_id, filename):
        return self.search_files(
            "trashed=false and title='{0}'".format(filename))

    def find_file_in_list(self, files, filename):
        for file in files:
            if file['title'] == filename:
                return file

    def find_file_in_root(self, filename):
        return self.find_file_in_list(self.root_folders, filename)

    def get_file_size(self, file):
        return int(file[0]['fileSize'])

    def get_folder_path(self, folder):
        return folder['id']
Пример #2
0
 }).GetList()
 folderId = driveFilesList[0]['id']
 zipfilesInFolder = drive.ListFile({
     'q':
     "'{}' in parents".format(folderId)
 }).GetList()
 for zf in zipfilesInFolder:
     if ('zip' in zf['title'].lower()) & (
             zf['title']
             not in alreadyProcessedZipfiles['name'].tolist()):
         print('title: {}'.format(zf['title']))
         alreadyProcessedZipfiles = alreadyProcessedZipfiles.append(
             pd.DataFrame([{
                 'name': zf['title']
             }]))
         toUnzip = drive.CreateFile({'id': zf['id']})
         toUnzipStringContent = toUnzip.GetContentString(
             encoding='cp862')
         toUnzipBytesContent = BytesIO(
             toUnzipStringContent.encode('cp862'))
         readZipfile = zipfile.ZipFile(toUnzipBytesContent, "r")
         for fileInZipfileName in readZipfile.namelist():
             if '.xml' in fileInZipfileName.lower():
                 if ('-t' in fileInZipfileName.lower()) | (
                         '-m' in fileInZipfileName.lower()):
                     pass
                 else:
                     openedXml = readZipfile.open(
                         fileInZipfileName).read()
                     loadedXml = ET.fromstring(openedXml.decode())
                     firmHeaderDate = loadedXml.find(
class TableInterface:
    def __init__(self, credentials):
        self.client = pygsheets.authorize(credentials)
        g_auth = GoogleAuth()

        g_auth.LoadCredentialsFile("drive_creds.json")
        if g_auth.credentials is None:
            g_auth.LocalWebserverAuth()
        elif g_auth.access_token_expired:
            g_auth.Refresh()
        else:
            g_auth.Authorize()
        g_auth.SaveCredentialsFile("drive_creds.json")

        self.drive = GoogleDrive(g_auth)

    # table_style  -- list of table fields
    # group_list -- list of students
    # Returns new spreadsheet's link and id
    def create_spreadsheet(self, spreadsheet_title, spreadsheet_folder_title,
                           worksheet_title, table_style, group_list):

        style = gt.TableStyle(table_style)
        spreadsheet_template = {
            "properties": {
                "title": spreadsheet_title,
                "locale": "ru_RU"
            }
        }

        spreadsheet_folder_id = self.__get_folder_id(spreadsheet_folder_title)
        print("ID FOLDER =", spreadsheet_folder_id)

        if spreadsheet_folder_id is None:
            spreadsheet_folder_id = self.__create_folder(
                spreadsheet_folder_title)

        spreadsheet = self.client.create(spreadsheet_title,
                                         spreadsheet_template,
                                         spreadsheet_folder_id)

        # create new worksheet in new spreadsheet
        res = self.add_worksheet(spreadsheet.id, worksheet_title, table_style,
                                 group_list)
        if not res:
            print("Error while worksheet creating")
            return None

        # remove standard sheet1
        spreadsheet.del_worksheet(spreadsheet.sheet1)

        return {
            "link": spreadsheet.url,
            "id": spreadsheet.id,
            "worksheets": self.__get_worksheets(spreadsheet.id)
        }

    # Returns table's url
    def share_table(self, spreadsheet_id, user_mail, role):
        spreadsheet = self.client.open_by_key(spreadsheet_id)
        share_role = "reader" if role == "r" else "writer"
        spreadsheet.share(user_mail, share_role)

        return spreadsheet.url

    # Adds new worksheet in spreadsheet with specified id
    # Fills worksheet with fields table_style and students group_list
    def add_worksheet(self, spreadsheet_id, worksheet_title, table_style,
                      group_list):

        style = gt.TableStyle(table_style)
        spreadsheet = self.client.open_by_key(spreadsheet_id)
        spreadsheet.add_worksheet(worksheet_title,
                                  rows=len(group_list) + 1,
                                  cols=len(style.fields))

        new_worksheet = spreadsheet.worksheet("title", worksheet_title)

        # init table fields and students' names
        new_worksheet.update_row(1, [style.fields])
        new_worksheet.update_col(1, [group_list], row_offset=1)

        # format students' names column
        first_col = new_worksheet.get_col(1, returnas='range')
        first_col.apply_format(gt.CellStyle.student_names_format_cell)

        # format table field
        first_row = new_worksheet.get_row(1, returnas='range')
        first_row.apply_format(gt.CellStyle.fields_format_cell)

        new_worksheet.adjust_column_width(0, new_worksheet.cols)

        # set format of main table (with marks)
        main_field = new_worksheet.get_values(
            (2, 2), (new_worksheet.rows, new_worksheet.cols),
            returnas='range',
            include_tailing_empty_rows=True)
        main_field.apply_format(gt.CellStyle.main_table_cell)

        return True

    # Returns all spreadsheets {name, link, id} in folder_name directory
    def get_spreadsheets(self, folder_name: str):
        folder_id = self.__get_folder_id(folder_name)

        if folder_name is None or folder_id is None:
            return None

        files = self.drive.ListFile({"q": "mimeType='application/vnd.google-apps.spreadsheet' and trashed=false"}) \
            .GetList()

        files_info = [{
            "name": o["title"],
            "link": o["alternateLink"],
            "id": o["id"],
            "worksheets": self.__get_worksheets(o["id"])
        } for o in filter(lambda elem: elem["parents"][0]["id"] == folder_id,
                          files)]

        return files_info

    # Returns list of students first name and second name
    # Students must be in A2:An column
    def get_students_list(self, spreadsheet_id, worksheet_name):
        worksheet = self.client.open_by_key(spreadsheet_id).worksheet(
            "title", worksheet_name)
        students_range = worksheet.range("A2:A" + str(worksheet.rows),
                                         returnas='cell')
        students_list = [
            cell.value for cells in students_range for cell in cells
        ]
        return students_list

    # Deletes table by spreadsheet_id
    def del_spreadsheet(self, spreadsheet_id):
        self.client.drive.delete(spreadsheet_id)

    # Creates new column with attendance content
    # content[0] -- col name, content[1:n] -- attendance of student (+/-)
    def add_date_col(self, spreadsheet_id, worksheet_name, content):

        insert_index = self.__find_date_col_index(spreadsheet_id,
                                                  worksheet_name)
        print(insert_index)

        worksheet = self.client.open_by_key(spreadsheet_id).worksheet(
            "title", worksheet_name)
        worksheet.insert_cols(insert_index, values=content, inherit=True)
        worksheet.adjust_column_width(insert_index)

        now = datetime.datetime.now()
        pprint(worksheet.cell(addr=(1, insert_index + 1)))
        added_field_cell = worksheet.cell((1, insert_index + 1))
        added_field_cell.note = str(now.day) + "." + str(
            now.month) + "." + str(now.year)

        new_col_content = worksheet.get_values(
            (2, insert_index + 1), (worksheet.rows, insert_index + 1),
            returnas='range',
            include_tailing_empty_rows=True)
        new_col_content.apply_format(gt.CellStyle.main_table_cell)

        return True

    # Returns folder's id if directory consists.
    # Else returns None
    def __get_folder_id(self, folder_name: str):
        if folder_name is None:
            return None

        dirs = self.drive.ListFile({"q": "mimeType='application/vnd.google-apps.folder' and trashed=false"})\
            .GetList()
        dir_titles = [o["title"] for o in dirs]

        if str(folder_name) not in dir_titles:
            return None

        for elem in dirs:
            if elem["title"] == str(folder_name):
                return elem["id"]

    # Returns new folder's id
    def __create_folder(self, folder_name):
        new_dir = self.drive.CreateFile({
            "title":
            folder_name,
            "mimeType":
            "application/vnd.google-apps.folder"
        })
        new_dir.Upload()
        return new_dir["id"]

    def __get_worksheets(self, spreadsheet_id):
        spreadsheet = self.client.open_by_key(spreadsheet_id)
        return [o.title for o in spreadsheet.worksheets()]

    def __find_date_col_index(self, spreadsheet_id, worksheet_title):
        worksheet = self.client.open_by_key(spreadsheet_id).worksheet(
            "title", worksheet_title)
        fields = worksheet.get_row(1, returnas='cell')

        index = None
        for i in range(len(fields)):
            if fullmatch("\d{1,2}\.\d{1,2}\.\d{4}", str(fields[i].note)):
                print(str(fields[i].note))
                index = i

        return 1 if index is None else index + 1
Пример #4
0
class GoogleDriveFilesystem(Vfs):
    drive = None
    history = {}
    CLIENT_ID = ''
    CLIENT_SECRET = ''
    FOLDER_TYPE = 'application/vnd.google-apps.folder'

    def __init__(self, rootString):
        self.set_root(rootString)

        self.CLIENT_ID = utils.getSetting('google_drive_id')
        self.CLIENT_SECRET = utils.getSetting('google_drive_secret')

        self.setup()

    def setup(self):
        #create authorization helper and load default settings
        gauth = GoogleAuth(
            xbmc.validatePath(
                xbmc.translatePath(utils.addon_dir() +
                                   '/resources/lib/pydrive/settings.yaml')))
        gauth.LoadClientConfigSettings()

        #check if this user is already authorized
        if (not xbmcvfs.exists(
                xbmc.translatePath(utils.data_dir() + "google_drive.dat"))):
            settings = {
                "client_id": self.CLIENT_ID,
                'client_secret': self.CLIENT_SECRET
            }

            drive_url = gauth.GetAuthUrl(settings)

            utils.log("Google Drive Authorize URL: " + drive_url)

            code = xbmcgui.Dialog().input(
                'Google Drive Validation Code',
                'Input the Validation code after authorizing this app')

            gauth.Auth(code)
            gauth.SaveCredentialsFile(
                xbmc.validatePath(
                    xbmc.translatePath(utils.data_dir() + 'google_drive.dat')))
        else:
            gauth.LoadCredentialsFile(
                xbmc.validatePath(
                    xbmc.translatePath(utils.data_dir() + 'google_drive.dat')))

        #create the drive object
        self.drive = GoogleDrive(gauth)

        #make sure we have the folder we need
        xbmc_folder = self._getGoogleFile(self.root_path)
        print xbmc_folder
        if (xbmc_folder == None):
            self.mkdir(self.root_path)

    def listdir(self, directory):
        files = []
        dirs = []

        if (not directory.startswith('/')):
            directory = '/' + directory

        #get the id of this folder
        parentFolder = self._getGoogleFile(directory)

        #need to do this after
        if (not directory.endswith('/')):
            directory = directory + '/'

        if (parentFolder != None):

            fileList = self.drive.ListFile({
                'q':
                "'" + parentFolder['id'] + "' in parents and trashed = false"
            }).GetList()

            for aFile in fileList:
                if (aFile['mimeType'] == self.FOLDER_TYPE):
                    dirs.append(utils.encode(aFile['title']))
                else:
                    files.append(utils.encode(aFile['title']))

        return [dirs, files]

    def mkdir(self, directory):
        result = True

        if (not directory.startswith('/')):
            directory = '/' + directory

        if (directory.endswith('/')):
            directory = directory[:-1]

        #split the string by the directory separator
        pathList = os.path.split(directory)

        if (pathList[0] == '/'):

            #we're at the root, just make the folder
            newFolder = self.drive.CreateFile({
                'title': pathList[1],
                'parent': 'root',
                'mimeType': self.FOLDER_TYPE
            })
            newFolder.Upload()
        else:
            #get the id of the parent folder
            parentFolder = self._getGoogleFile(pathList[0])

            if (parentFolder != None):
                newFolder = self.drive.CreateFile({
                    'title':
                    pathList[1],
                    "parents": [{
                        'kind': 'drive#fileLink',
                        'id': parentFolder['id']
                    }],
                    'mimeType':
                    self.FOLDER_TYPE
                })
                newFolder.Upload()
            else:
                result = False

        return result

    def put(self, source, dest):
        result = True

        #make the name separate from the path
        if (not dest.startswith('/')):
            dest = '/' + dest

        pathList = os.path.split(dest)

        #get the parent location
        parentFolder = self._getGoogleFile(pathList[0])

        if (parentFolder != None):
            #create a new file in this folder
            newFile = self.drive.CreateFile({
                "title":
                pathList[1],
                "parents": [{
                    'kind': 'drive#fileLink',
                    'id': parentFolder['id']
                }]
            })
            newFile.SetContentFile(source)
            newFile.Upload()
        else:
            result = False

        return result

    def get_file(self, source, dest):
        result = True

        #get the id of this file
        file = self._getGoogleFile(source)

        if (file != None):
            file.GetContentFile(dest)
        else:
            result = False

        return result

    def rmdir(self, directory):
        result = True

        #check that the folder exists
        folder = self._getGoogleFile(directory)

        if (folder != None):
            #delete the folder
            folder.Delete()
        else:
            result = False

        return result

    def rmfile(self, aFile):
        #really just the same as the remove directory function
        return self.rmdir(aFile)

    def exists(self, aFile):
        #attempt to get this file
        foundFile = self._getGoogleFile(aFile)

        if (foundFile != None):
            return True
        else:
            return False

    def rename(self, aFile, newName):
        return True

    def _getGoogleFile(self, file):
        result = None

        #file must start with / and not end with one (even directory)
        if (not file.startswith('/')):
            file = '/' + file

        if (file.endswith('/')):
            file = file[:-1]

        if (self.history.has_key(file)):

            result = self.history[file]
        else:
            pathList = os.path.split(file)

            #end of recurision, we got the root
            if (pathList[0] == '/'):
                #get the id of this file (if it exists)
                file_list = self.drive.ListFile({
                    'q':
                    "title='" + pathList[1] +
                    "' and 'root' in parents and trashed=false"
                }).GetList()

                if (len(file_list) > 0):
                    result = file_list[0]
                    self.history[pathList[1]] = result
            else:
                #recurse down the tree
                current_file = pathList[1]

                parentId = self._getGoogleFile(pathList[0])

                if (parentId != None):
                    self.history[pathList[0]] = parentId

                    #attempt to get the id of this file, with this parent
                    file_list = file_list = self.drive.ListFile({
                        'q':
                        "title='" + current_file + "' and '" + parentId['id'] +
                        "' in parents and trashed=false"
                    }).GetList()

                    if (len(file_list) > 0):
                        result = file_list[0]
                        self.history[file] = result

        return result
Пример #5
0
            cv2.putText(img, str(id), (x + 5, y - 5), font, 1, (255, 255, 255),
                        2)
            cv2.putText(img, str(confidence), (x + 5, y + h - 5), font, 1,
                        (255, 255, 0), 1)

    if approvedUser == True & takeImg == True:
        read_ser = ser.readline()
        print(read_ser)
        print("Approved")
        cv2.imwrite(str(timeStamp) + ".jpg", imgEx)

        if drunk == False:
            f = drive.CreateFile(
                {"parents": [{
                    "kind": "drive#fileLink",
                    "id": id_path
                }]})
            f.SetContentFile(str(timeStamp) + ".jpg")

        else:
            drunk_image = cv2.imread(str(timeStamp) + ".jpg")
            blurImg = cv2.blur(drunk_image, (30, 30))  # Kernel size
            cv2.imwrite("blur" + str(timeStamp) + ".jpg", blurImg)
            f = drive.CreateFile(
                {"parents": [{
                    "kind": "drive#fileLink",
                    "id": id_path
                }]})
            f.SetContentFile("blur" + str(timeStamp) + ".jpg")
            drunk = False
Пример #6
0
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import os

g_login = GoogleAuth()
g_login.LocalWebserverAuth()
drive = GoogleDrive(g_login)

with open("Chandler/resources/chandler.png", 'r') as file:
    file_drive = drive.CreateFile(
        {'test10008000': os.path.basename(file.name)})
    file_drive.SetContentFile(file.read())
    file_drive.Upload()
Пример #7
0
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials


#autenticar
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

#downloadDataset
download = drive.CreateFile({'id': '1BZOv422XJvxFUnGh-0xVeSvgFgqVY45q'})

download.GetContentFile('train_LbELtWX.zip')
!unzip train_LbELtWX.zip

import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import to_categorical
from keras.preprocessing import image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
from tqdm import tqdm
Пример #8
0
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import sys

if len(sys.argv) < 2:
    exit()
path = sys.argv[1]
name = sys.argv[2]

gauth = GoogleAuth()
gauth.CommandLineAuth()
drive = GoogleDrive(gauth)

try:
    file1 = drive.CreateFile({
        "title":
        name,
        "parents": [{
            "kind": "drive#fileLink",
            "id": "1B4GktyQzQmZDRYTHYDUpW"
        }]
    })
    file1.SetContentFile(path)
    file1.Upload()
    print "Uploading succeeded!"
except:
    print "Uploading failed."
Пример #9
0
def gdrive_sync(gauth, os_root_path, gdrive_root_folder_id, archive_time):
    '''
    Function which performs the sync operation.
    '''

    with open('update_log.txt') as f:
        log_info = f.readlines()
        try:
            last_line = log_info[-1].strip()
            log_mod_time = last_line.split('|')
            log_mod_time = datetime.datetime.strptime(log_mod_time[0],
                                                      '%Y-%m-%d %H:%M:%S ')
        except IndexError:
            log_mod_time = datetime.datetime(1900, 1, 1)
    print(log_mod_time)
    drive = GoogleDrive(gauth)
    dir_dict = {os_root_path: gdrive_root_folder_id}
    item_list = drive.ListFile({
        'q':
        f'"{gdrive_root_folder_id}" in parents and trashed=false'
    }).GetList()

    for dirpath, dirnames, files in os.walk(os_root_path):
        print(dirnames)
        for directory in dirnames:
            dir_mod_time = os.path.getmtime(os.path.join(dirpath, directory))
            dir_mod_time = datetime.datetime.fromtimestamp(dir_mod_time)

            if dir_mod_time > log_mod_time:
                new_folder = drive.CreateFile({
                    'title':
                    directory,
                    'parents': [{
                        'id': dir_dict[dirpath]
                    }],
                    'mimeType':
                    'application/vnd.google-apps.folder'
                })
                new_folder.Upload()
                dir_dict[os.path.join(dirpath, directory)] = new_folder['id']

        for file in files:
            file_mod_time = os.path.getmtime(os.path.join(dirpath, file))
            file_mod_time = datetime.datetime.fromtimestamp(file_mod_time)
            print(file_mod_time)
            # If the file in the local directory was modified more recently than the last backup
            # was run, upload to Google Drive.
            if file_mod_time > log_mod_time:
                print('files should be updated')
                new_file = drive.CreateFile({
                    'title':
                    file,
                    'parents': [{
                        'kind': 'drive#fileLink',
                        'id': dir_dict[dirpath]
                    }]
                })

                #Check existing files for the same name file to overwrite if overwritten locally.
                old_file = [item['title'] == file for item in item_list]
                [
                    item.Delete() for i, item in enumerate(item_list)
                    if old_file[i] == True
                ]

                #Upload new file.
                new_file.SetContentFile(os.path.join(dirpath, file))
                new_file.Upload()

        # If option is selected, deletes all files older than a certain date.
        if archive_time != -1:
            cur_date = datetime.datetime.now()
            for file in item_list:
                mod_date = file['modifiedDate']
                mod_date = datetime.datetime.strptime(mod_date,
                                                      '%Y-%m-%dT%H:%M:%S.%fZ')
                if int((cur_date - mod_date).days) > int(archive_time):
                    file.Delete()
Пример #10
0
if gauth.credentials is None:
    # Authenticate if they're not there
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    # Refresh them if expired
    gauth.Refresh()
else:
    # Initialize the saved creds
    gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("token.json")

drive = GoogleDrive(gauth)

if __name__=="__main__":
	textfile = drive.CreateFile()
	textfile.SetContentFile('eng.txt')
	textfile.Upload()
	print (textfile)
	drive.CreateFile({'id':textfile['id']}).GetContentFile('eng-dl.txt')

def uploadfile(filename):
    file1 = drive.CreateFile()
    shutil.move(f"{filename}",f"downloads/{filename}")
    file1.SetContentFile(filename)
    file1.Upload()
    permission = file1.InsertPermission({
        'type': 'anyone',
        'value': 'anyone',
        'role': 'reader'})
    os.remove(filename)
Пример #11
0
**Make sure to follow the interactive instructions.**
"""

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

id = '1EoolSK32_U74I4FeLox88iuUB_SUUYsI'
downloaded = drive.CreateFile({'id': id})
downloaded.GetContentFile('web-Stanford.txt')
"""If you executed the cells above, you should be able to see the dataset we will use for this Colab under the "Files" tab on the left panel.

Next, we import some of the common libraries needed for our task.
"""

# Commented out IPython magic to ensure Python compatibility.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline
"""### Data Loading

For this Colab we will be using [NetworkX](https://networkx.github.io), a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.
Пример #12
0
from shutil import copy
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

os.makedirs('dataset124')
os.chdir('dataset124')

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

fileName = fileId + '.zip'
downloaded = drive.CreateFile({'id': fileId})
downloaded.GetContentFile(fileName)
ds = ZipFile(fileName)
ds.extractall()
os.remove(fileName)
print('Extracted zip file ' + fileName)

"""#Edit settings file
*  find and replace occurrences of "balloon" and "Balloon" with name of your object
*  set epochs number
"""

# %cd ~/Mask_RCNN

!cp ~/Mask_RCNN/samples/balloon/balloon.py ./dog.py
Instruction: https://pythonhosted.org/PyDrive/quickstart.html
"""

import glob
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

# access the drive
gauth = GoogleAuth()
drive = GoogleDrive(gauth)

# file to save links of files
drive_links = open('files_drive_links.txt', "w+")
for file in glob.glob("*.csv"):
    print(file)
    file_drive = drive.CreateFile()
    file_drive.SetContentFile(file)
    file_drive.Upload()
    permission = file_drive.InsertPermission({
        'type': 'anyone',
        'value': 'anyone',
        'role': 'reader'
    })
    link = file_drive['alternateLink']
    drive_links.write(f"{file} link: {link}\n")
    print(f"The file: {file} has been uploaded")
drive_links.close()

print("All files have been uploaded")
Пример #14
0
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth

# For using listdir()
import os

# Below code does the authentication
# part of the code
gauth = GoogleAuth()

# Creates local webserver and auto
# handles authentication.
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

# replace the value of this variable
# with the absolute path of the directory
path = r"C:\Users\703235761\Desktop\plans.txt"

f = drive.CreateFile({'title': 'plans.txt'})
f.SetContentFile(path)
f.Upload()

# Due to a known bug in pydrive if we
# don't empty the variable used to
# upload the files to Google Drive the
# file stays open in memory and causes a
# memory leak, therefore preventing its
# deletion
f = None
Пример #15
0
class GFile():
    def __init__(self):
        #self.logfile = LOGFILE_PATH
        gauth = self._authenticate()
        self.drive = GoogleDrive(gauth)

    def _authenticate(self):
        """
        Authenticate with google drive
        """
        # Check if client_secret file is present for authentication
        self._check_secrets_file()
        return GoogleAuth()

    def _check_secrets_file(self):
        """
        Check if client_secrets.json file is present or not
        """
        if not os.path.exists(os.path.join(SCRIPT_DIR, CLIENT_SECRETS_JSON)):
            raise ValueError("{} file not found at path {}".format(
                CLIENT_SECRETS_JSON, SCRIPT_DIR))

    def _get_file(self, filename):
        """
        Returns file object of 'filename' on google drive
        :param filename: Name of the file on google drive
        :return: GoogleDriveFile object
        """
        files_list = [
            gfile for gfile in self.drive.ListFile().GetList()
            if gfile['title'] == filename
        ]
        if len(files_list) == 0:
            raise ValueError(
                "File {} not found on google drive".format(filename))
        return files_list[0]

    def createFile(self, filename):
        """
        Returns file object of 'filename' on google drive
        :param filename: Name of the file to be created on google drive
        :return: GoogleDriveFile object
        """
        gfile = self.drive.CreateFile({'title': filename})
        gfile.Upload()  # Upload the file
        return gfile

    def updateFileContent(self, gfile, contentStr="", source_file=None):
        """
        Updates file contents on google drive
        :param filename: Name of the file on google drive
        :return: None
        """
        # Create new file if not exists
        try:
            gfileObj = self._get_file(gfile)
        except ValueError:
            gfileObj = self.createFile(gfile)
        if source_file:
            gfileObj.SetContentFile(source_file)
        else:
            print "contentStr:", contentStr
            gfileObj.SetContentString(contentStr)
        # Upload the file
        gfileObj.Upload()
Пример #16
0
def upload_files(images, csv_name, target_drive_dir='slideInfo_BioBasic'):

    logger.log('begin file upload')

    client_secrets_path = basest_dir + "/client_secrets.json"
    credentials_path = basest_dir + "/credentials.txt"

    GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = client_secrets_path

    # Create google account authentication objects
    gauth = GoogleAuth()

    logger.log('Looking for credentials')

    if os.path.exists(credentials_path):
        logger.log('found a credentials')
        gauth.LoadCredentialsFile(credentials_path)

    if gauth.credentials is None:
        logger.log('local connect to website')
        gauth.LocalWebserverAuth()

    elif gauth.access_token_expired:
        logger.log('refresh branch')
        gauth.Refresh()

    else:
        logger.log('authorize branch')
        gauth.Authorize()

    logger.log('creating connection to google drive')

    gauth.SaveCredentialsFile(credentials_path)
    drive = GoogleDrive(gauth)

    logger.log('connection established')

    # Upload the template files to the user
    if target_drive_dir == 'slideInfo_BioBasic':
        upload_template = drive.CreateFile({'title': 'TEMPLATE_bio_bas'})
        upload_template.SetContentFile(basest_dir + '/TEMPLATE_bio_bas.pptx')
        upload_template.Upload()

    elif target_drive_dir == 'slideInfo_BioAdv':
        upload_template = drive.CreateFile({'title': 'TEMPLATE_bio_adv'})
        upload_template.SetContentFile(basest_dir + '/TEMPLATE_bio_adv.pptx')
        upload_template.Upload()
    ''' Find the name of the folder we want to upload to '''
    # Define the folder we want to upload to
    target_folder_name = target_drive_dir
    target_folder_id = ''

    # Find the list of all of the files in the google drive
    file_list = drive.ListFile({
        'q': "'root' in parents and trashed=false"
    }).GetList()

    # Loop through all of the files in the
    for file_object in file_list:

        # Check if the current one is our target
        if file_object['title'] == target_folder_name:

            # Save the folder id
            target_folder_id = file_object['id']

    logger.log("folder id: " + target_folder_id)

    # upload the CSV containing only the info on the chosen animals for images
    upload_csv = drive.CreateFile({
        'title': csv_name,
        'parents': [{
            'id': target_folder_id
        }]
    })
    upload_csv.SetContentFile(basest_dir + "/" + csv_name)
    upload_csv.Upload()
    logger.log("uploaded chosen_mammals csv")

    # Loop through the images
    for image_name in images:
        upload_image = drive.CreateFile({
            'title': image_name,
            'parents': [{
                'id': target_folder_id
            }]
        })

        #upload_image.SetContentFile( "python_scripts/biodiversity/animal_images/" + image_name )

        logger.log(image_name)

        if __name__ == "__main__":
            upload_image.SetContentFile("animal_images/" + image_name + ".jpg")

        else:
            upload_image.SetContentFile(basest_dir + '/animal_images/' +
                                        image_name + ".jpg")

        upload_image.Upload()
Пример #17
0
def get_from_drive(idv):
  gauth = GoogleAuth()
  gauth.credentials = GoogleCredentials.get_application_default()
  drive = GoogleDrive(gauth)
  last_weight_file = drive.CreateFile({'id': idv}) 
  last_weight_file.GetContentFile('DenseNet-40-12-CIFAR10.h5')    
Пример #18
0
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)s

!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

downloaded = drive.CreateFile({'id':'1Y15XHsyLLNX93XLYFcmBqo3vsOO57eRG'}) # replace the id with id of file you want to access
downloaded.GetContentFile('training.1600000.processed.noemoticon.csv')

df = pd.read_csv('training.1600000.processed.noemoticon.csv', encoding='latin', header= None)

df.head()

df.columns = ['sentiment', 'id', 'date', 'query', 'user_id', 'text']

df.head(2)

# drop all the olumns except the text column

df.drop(['date', 'id', 'query', 'user_id'], axis=1, inplace=True)

df.head(3)
Пример #19
0
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth(
)  # Creates local webserver and auto handles authentication.

drive = GoogleDrive(gauth)
'''
fileList = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file in fileList:
  print('Title: %s, ID: %s' % (file['title'], file['id']))
  # Get the folder ID that you want
  if(file['title'] == "teste"):
      fileID = file['id']
'''
file1 = drive.CreateFile({
    'title':
    'Hello.txt',
    "parents": [{
        "kind": "drive#fileLink",
        "id": '1J4bGOObOfAEv2BJmiZLVycvuWCnTNPAd'
    }]
})  # Create GoogleDriveFile instance with title 'Hello.txt'.
file1.SetContentString(
    'Hello World!')  # Set content of the file from given string.
file1.Upload()
Пример #20
0
file_list = drive.ListFile({
    'q':
    f"'{os.environ['GDRIVE_FOURSQUARE']}' in parents and trashed=false"
}).GetList()
df = pd.DataFrame(file_list)
df.sort_values(by='createdDate', ascending=False, inplace=True)
df['date'] = df.title.apply(get_date)
available_dates = df.date.to_list()

# List dates that's already loaded
loaded = pd.read_sql(sql='''
    SELECT table_name 
    FROM information_schema.tables 
    WHERE table_schema = 'foursquare_datacube'
    AND table_name not in ('main', 'latest')
''',
                     con=engine)
loaded_dates = loaded.table_name.to_list()

for i in available_dates:
    if i not in loaded_dates:
        print(f'pulling date {i}')
        file_id = df.loc[df.date == i, 'id'].to_list()[0]
        file_name = f'{i}.tar.gz'
        target_file = drive.CreateFile({'id': file_id})
        target_file.FetchContent()
        content_string = target_file.content.getvalue()

        # Write content string to directory
        with open(f'input/{file_name}', 'wb') as fi:
            fi.write(content_string)
Пример #21
0
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive


gauth = GoogleAuth()
gauth.LoadCredentialsFile("mycreds.txt")

if gauth.credentials is None:
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    gauth.Refresh()
else:
    gauth.Authorize()

gauth.SaveCredentialsFile("mycreds.txt")

drive = GoogleDrive(gauth)

file1 = drive.CreateFile(
    {"title": "Automata.txt"}
)  # Create GoogleDriveFile instance with title 'Hello.txt'.
file1.SetContentString("Automataaa")  # Set content of the file from given string.
file1.Upload()
print(drive)
Пример #22
0
async def gdrive_helper(_client, message):
    if len(message.text.split()) == 1:
        gdriveclient = os.path.isfile("client_secrets.json")
        if HEROKU_API:
            if not gdrive_credentials:
                await message.reply(
                    "Hello, look like you're not logged in to google drive 🙂\nI can help you to "
                    "login.\n\nFirst of all, you need to activate your google drive API\n1. [Go here]("
                    "https://developers.google.com/drive/api/v3/quickstart/python), click **Enable the "
                    "drive API**\n2. Login to your google account (skip this if you're already logged "
                    "in)\n3. After logged in, click **Enable the drive API** again, and click "
                    "**Download Client Configuration** button, download that.\n4. After downloaded "
                    "that file, open that file then copy all of that content\n\n go to "
                    "dashboard.heroku.com, select your apps and go to settings, go to config vars then "
                    "add key with name `gdrive_credentials` with value your credentials\n\nAfter that, "
                    "you can go next guide by type /gdrive")
                return
            elif not gdriveclient:
                file = open("client_secrets.json", "w")
                file.write(gdrive_credentials)
                file.close()
                gdriveclient = os.path.isfile("client_secrets.json")
        if not gdriveclient:
            await message.reply(
                "Hello, look like you're not logged in to google drive 🙂\nI can help you to login.\n\nFirst of all, "
                "you need to activate your google drive API\n1. [Go here]("
                "https://developers.google.com/drive/api/v3/quickstart/python), click **Enable the drive API**\n2. "
                "Login to your google account (skip this if you're already logged in)\n3. After logged in, "
                "click **Enable the drive API** again, and click **Download Client Configuration** button, "
                "download that.\n4. After downloaded that file, open that file then copy all of that content, "
                "back to telegram then do .credentials (copy the content of that file)  do without bracket \n\nAfter "
                "that, you can go next guide by type /gdrive")
            return

        gauth.LoadCredentialsFile("nana/session/drive")
        if gauth.credentials is None:
            try:
                authurl = gauth.GetAuthUrl()
            except:
                await message.reply(
                    "Wrong Credentials! Check var ENV gdrive_credentials on heroku or do .credentials (your "
                    "credentials) for change your Credentials")
                return
            teks = "First, you must log in to your Google drive first.\n\n[Visit this link and login to your Google " \
                   "account]({})\n\nAfter that you will get a verification code, type `/gdrive (verification code)` " \
                   "without '(' or ')'.".format(authurl)
            await message.reply(teks)
            return
        await message.reply(
            "You're already logged in!\nTo logout type `/gdrive logout`")
    elif len(
            message.text.split()) == 2 and message.text.split()[1] == "logout":
        os.remove("nana/session/drive")
        await message.reply(
            "You have logged out of your account!\nTo login again, just type /gdrive"
        )
    elif len(message.text.split()) == 2:
        try:
            gauth.Auth(message.text.split()[1])
        except pydrive.auth.AuthenticationError:
            await message.reply("Your Authentication code is Wrong!")
            return
        gauth.SaveCredentialsFile("nana/session/drive")
        drive = GoogleDrive(gauth)
        file_list = drive.ListFile({
            'q': "'root' in parents and trashed=false"
        }).GetList()
        for drivefolders in file_list:
            if drivefolders['title'] == 'Nana Drive':
                await message.reply("Authentication successful!\nWelcome back!"
                                    )
                return
        mkdir = drive.CreateFile({
            'title':
            'Nana Drive',
            "mimeType":
            "application/vnd.google-apps.folder"
        })
        mkdir.Upload()
        await message.reply(
            "Authentication successful!\nThe 'Nana Drive' folder has been created automatically!"
        )
    else:
        await message.reply("Invaild args!\nCheck /gdrive for usage guide")
import keras.optimizers

# Code to read csv file into colaboratory:
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

#2.1 Get the file
downloaded = drive.CreateFile({'id':'1tygtRvSHxmtbWIUq4hb9v8IcPIqWBLCy'}) # replace the id with id of file you want to access
downloaded.GetContentFile('train.json')

import pandas as pd

data = pd.read_json('test.json',orient=None,
    typ='frame',
    dtype=None,
    convert_axes=None,
    convert_dates=True,
    keep_default_dates=True,
    numpy=False,
    precise_float=False,
    date_unit=None,
    encoding=None,
    lines=True,
    https://colab.research.google.com/drive/1gzu5RUAPJS9_Y1QryaqvE926WaXRInYC
"""

# Code to read csv file into colaboratory:
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

downloaded = drive.CreateFile({'id':'1RbG76av_2kFXsKv_RstsC3PTmSXkcPp9'}) # replace the id with id of file you want to access
downloaded.GetContentFile('SampleData.xlsx')

downloaded = drive.CreateFile({'id':'10xUlClZ2xbfFLrwUl40HAe4ELOvJV2BO'}) # replace the id with id of file you want to access
downloaded.GetContentFile('SampleData3.xlsx')

downloaded = drive.CreateFile({'id':'10xUlClZ2xbfFLrwUl40HAe4ELOvJV2BO'}) # replace the id with id of file you want to access
downloaded.GetContentFile('SampleData4.xlsx')

import pandas as pd

path0 = "SampleData.xlsx"
path1 = "SampleData3.xlsx"
path2 = "SampleData4.xlsx"

df0 = pd.read_excel(path0)
# Fetch weather forecast data
weather_query = '''
    select * from weather.forecast where woeid in 
        (select woeid from geo.places(1) 
            where text="Seattle, WA")'''
data = requests.get('https://query.yahooapis.com/v1/public/yql?format=json&q=' \
    + weather_query)
data_json = data.json()

# Open or create google drive file
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file_name = 'seattleweather.csv'
files = drive.ListFile({
    'q': "title='{}' and trashed=false".format(file_name)
}).GetList()
if files:
    gd_file = files[0]
else:
    gd_file = drive.CreateFile({'title': file_name, 'mimeType': 'text/csv'})

# Set content and upload
content = 'date,high,low\n'
for item in data_json['query']['results']['channel']['item']['forecast']:
    content += '{},{},{}\n'.format(item['date'], item['high'], item['low'])
gd_file.SetContentString(content)
gd_file.Upload()
print('File uploaded, title: {}, id: {}'.format(gd_file['title'],
                                                gd_file['id']))
Пример #26
0
async def gdrive_stuff(client, message):
    gauth.LoadCredentialsFile("nana/session/drive")
    if gauth.credentials is None:
        if HEROKU_API:
            if gdrive_credentials:
                file = open("client_secrets.json", "w")
                file.write(gdrive_credentials)
                file.close()
        await message.edit(
            "You are not logged in to your google drive account!\nYour assistant bot may help you to login google "
            "drive, check your assistant bot for more information!")
        gdriveclient = os.path.isfile("client_secrets.json")
        if not gdriveclient:
            await setbot.send_message(
                message.from_user.id,
                "Hello, look like you're not logged in to google drive 🙂\nI can help you to "
                "login.\n\nFirst of all, you need to activate your google drive API\n1. [Go "
                "here](https://developers.google.com/drive/api/v3/quickstart/python), "
                "click **Enable the drive API**\n2. Login to your google account (skip this if "
                "you're already logged in)\n3. After logged in, click **Enable the drive API** "
                "again, and click **Download Client Configuration** button, download that.\n4. "
                "After downloaded that file, open that file then copy all of that content, "
                "back to telegram then do .credentials (copy the content of that file)  do "
                "without bracket\n\nAfter that, you can go next guide by type /gdrive"
            )
        else:
            try:
                gauth.GetAuthUrl()
            except:
                await setbot.send_message(
                    message.from_user.id,
                    "Wrong Credentials! Check var ENV gdrive_credentials on heroku or do "
                    ".credentials (your credentials) for change your Credentials"
                )
                return
            await setbot.send_message(
                message.from_user.id,
                "Hello, look like you're not logged in to google drive :)\nI can help you to "
                "login.\n\n**To login Google Drive**\n1. `/gdrive` to get login URL\n2. After "
                "you're logged in, copy your Token.\n3. `/gdrive (token)` without `(` or `)` to "
                "login, and your session will saved to `nana/session/drive`.\n\nDon't share your "
                "session to someone, else they will hack your google drive account!"
            )
        return
    elif gauth.access_token_expired:
        # Refresh them if expired
        gauth.Refresh()
    else:
        # Initialize the saved creds
        gauth.Authorize()

    drive = GoogleDrive(gauth)
    drive_dir = await get_drivedir(drive)

    if len(message.text.split()) == 3 and message.text.split(
    )[1] == "download":
        await message.edit("Downloading...")
        driveid = await get_driveid(message.text.split()[2])
        if not driveid:
            await message.edit(
                "Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`"
            )
            return
        filename = await get_driveinfo(driveid)
        if not filename:
            await message.edit(
                "Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`"
            )
            return
        await message.edit(
            "Downloading for `{}`\nPlease wait...".format(filename))
        download = drive.CreateFile({'id': driveid})
        download.GetContentFile(filename)
        try:
            os.rename(filename, "nana/downloads/" + filename)
        except FileExistsError:
            os.rename(filename, "nana/downloads/" + filename + ".2")
        await message.edit(
            "Downloaded!\nFile saved to `{}`".format("nana/downloads/" +
                                                     filename))
    elif len(
            message.text.split()) == 3 and message.text.split()[1] == "upload":
        filerealname = message.text.split()[2].split(None, 1)[0]
        filename = "nana/downloads/{}".format(filerealname)
        checkfile = os.path.isfile(filename)
        if not checkfile:
            await message.edit("File `{}` was not found!".format(filerealname))
            return
        await message.edit("Uploading `{}`...".format(filerealname))
        upload = drive.CreateFile({
            "parents": [{
                "kind": "drive#fileLink",
                "id": drive_dir
            }],
            'title':
            filerealname
        })
        upload.SetContentFile(filename)
        upload.Upload()
        upload.InsertPermission({
            'type': 'anyone',
            'value': 'anyone',
            'role': 'reader'
        })
        await message.edit(
            "Uploaded!\nDownload link: [{}]({})\nDirect download link: [{}]({})"
            .format(filerealname, upload['alternateLink'], filerealname,
                    upload['downloadUrl']))
    elif len(
            message.text.split()) == 3 and message.text.split()[1] == "mirror":
        message.edit("Mirroring...")
        driveid = await get_driveid(message.text.split()[2])
        if not driveid:
            await message.edit(
                "Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`"
            )
            return
        filename = await get_driveinfo(driveid)
        if not filename:
            await message.edit(
                "Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`"
            )
            return
        mirror = drive.auth.service.files().copy(fileId=driveid,
                                                 body={
                                                     "parents": [{
                                                         "kind":
                                                         "drive#fileLink",
                                                         "id":
                                                         drive_dir
                                                     }],
                                                     'title':
                                                     filename
                                                 }).execute()
        new_permission = {
            'type': 'anyone',
            'value': 'anyone',
            'role': 'reader'
        }
        drive.auth.service.permissions().insert(fileId=mirror['id'],
                                                body=new_permission).execute()
        await message.edit(
            "Done!\nDownload link: [{}]({})\nDirect download link: [{}]({})".
            format(filename, mirror['alternateLink'], filename,
                   mirror['downloadUrl']))
    elif len(message.text.split()) == 2 and message.text.split(
    )[1] == "tgmirror":
        if message.reply_to_message:
            await message.edit("__Downloading...__")
            c_time = time.time()
            if message.reply_to_message.photo:
                nama = "photo_{}.png".format(
                    message.reply_to_message.photo.date)
                await client.download_media(
                    message.reply_to_message.photo,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: asyncio.get_event_loop().create_task(
                        progressdl(d, t, message, c_time, "Downloading...")))
            elif message.reply_to_message.animation:
                nama = "giphy_{}-{}.gif".format(
                    message.reply_to_message.animation.date,
                    message.reply_to_message.animation.file_size)
                await client.download_media(
                    message.reply_to_message.animation,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: asyncio.get_event_loop().create_task(
                        progressdl(d, t, message, c_time, "Downloading...")))
            elif message.reply_to_message.video:
                nama = "video_{}-{}.mp4".format(
                    message.reply_to_message.video.date,
                    message.reply_to_message.video.file_size)
                await client.download_media(
                    message.reply_to_message.video,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: asyncio.get_event_loop().create_task(
                        progressdl(d, t, message, c_time, "Downloading...")))
            elif message.reply_to_message.sticker:
                nama = "sticker_{}_{}.webp".format(
                    message.reply_to_message.sticker.date,
                    message.reply_to_message.sticker.set_name)
                await client.download_media(
                    message.reply_to_message.sticker,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: asyncio.get_event_loop().create_task(
                        progressdl(d, t, message, c_time, "Downloading...")))
            elif message.reply_to_message.audio:
                nama = "audio_{}.mp3".format(
                    message.reply_to_message.audio.date)
                await client.download_media(
                    message.reply_to_message.audio,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: asyncio.get_event_loop().create_task(
                        progressdl(d, t, message, c_time, "Downloading...")))
            elif message.reply_to_message.voice:
                nama = "audio_{}.ogg".format(
                    message.reply_to_message.voice.date)
                await client.download_media(
                    message.reply_to_message.voice,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: asyncio.get_event_loop().create_task(
                        progressdl(d, t, message, c_time, "Downloading...")))
            elif message.reply_to_message.document:
                nama = "{}".format(message.reply_to_message.document.file_name)
                await client.download_media(
                    message.reply_to_message.document,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: asyncio.get_event_loop().create_task(
                        progressdl(d, t, message, c_time, "Downloading...")))
            else:
                await message.edit("Unknown file!")
                return
            upload = drive.CreateFile({
                "parents": [{
                    "kind": "drive#fileLink",
                    "id": drive_dir
                }],
                'title':
                nama
            })
            upload.SetContentFile("nana/downloads/" + nama)
            upload.Upload()
            upload.InsertPermission({
                'type': 'anyone',
                'value': 'anyone',
                'role': 'reader'
            })
            await message.edit(
                "Done!\nDownload link: [{}]({})\nDirect download link: [{}]({})"
                .format(nama, upload['alternateLink'], nama,
                        upload['downloadUrl']))
            os.remove("nana/downloads/" + nama)
        else:
            await message.edit("Reply document to mirror it to gdrive")
    elif len(message.text.split()) == 3 and message.text.split(
    )[1] == "urlmirror":
        await message.edit("Downloading...")
        URL = message.text.split()[2]
        nama = URL.split("/")[-1]
        time_dl = await download_url(URL, nama)
        if "Downloaded" not in time_dl:
            await message.edit("Failed to download file, invaild url!")
            return
        await message.edit(f"Downloaded with {time_dl}.\nNow uploading...")
        upload = drive.CreateFile({
            "parents": [{
                "kind": "drive#fileLink",
                "id": drive_dir
            }],
            'title':
            nama
        })
        upload.SetContentFile("nana/downloads/" + nama)
        upload.Upload()
        upload.InsertPermission({
            'type': 'anyone',
            'value': 'anyone',
            'role': 'reader'
        })
        await message.edit(
            "Done!\nDownload link: [{}]({})\nDirect download link: [{}]({})".
            format(nama, upload['alternateLink'], nama, upload['downloadUrl']))
        os.remove("nana/downloads/" + nama)
    else:
        await message.edit(
            "Usage:\n-> `gdrive download <url/gid>`\n-> `gdrive upload <file>`\n-> `gdrive mirror <url/gid>`\n\nFor "
            "more information about this, go to your assistant.")
Пример #27
0
async def gdrive_stuff(client, message):
    gauth.LoadCredentialsFile("nana/session/drive")
    if gauth.credentials is None:
        if ENV and GDRIVE_CREDENTIALS:
            with open("client_secrets.json", "w") as file:
                file.write(GDRIVE_CREDENTIALS)
        await edit_or_reply(
            message,
            text="You are not logged in to your google drive account!\n"
            "Your assistant bot may help you to login google "
            "drive, check your assistant bot for more information!",
        )
        gdriveclient = os.path.isfile("client_secrets.json")
        if gdriveclient:
            try:
                gauth.GetAuthUrl()
            except Exception as e:
                print(e)
                await setbot.send_message(
                    message.from_user.id,
                    "Wrong Credentials! Check var ENV gdrive_credentials"
                    "on heroku or do "
                    ".credentials (your credentials)"
                    "for changing your Credentials",
                )
                return
            await setbot.send_message(
                message.from_user.id,
                tld("gdrive_credential_err_heroku"),
            )
        else:
            await setbot.send_message(message.from_user.id,
                                      tld("gdrive_credential_err"))
        return
    elif gauth.access_token_expired:
        # Refresh them if expired
        gauth.Refresh()
    else:
        # Initialize the saved creds
        gauth.Authorize()

    drive = GoogleDrive(gauth)
    drive_dir = await get_drivedir(drive)

    if len(message.text.split()) == 3 and message.text.split(
    )[1] == "download":
        await edit_or_reply(message, text="Downloading...")
        driveid = await get_driveid(message.text.split()[2])
        if not driveid:
            await edit_or_reply(
                message,
                text="Invaild URL!",
            )
            return
        filename = await get_driveinfo(driveid)
        if not filename:
            await edit_or_reply(
                message,
                text="Invaild URL!",
            )
            return
        await edit_or_reply(
            message,
            text="Downloading for `{}`\nPlease wait...".format(
                filename.replace(" ", "_")),
        )
        download = drive.CreateFile({"id": driveid})
        download.GetContentFile(filename)
        try:
            os.rename(filename, "nana/downloads/" + filename.replace(" ", "_"))
        except FileExistsError:
            os.rename(filename,
                      "nana/downloads/" + filename.replace(" ", "_") + ".2")
        await edit_or_reply(
            message,
            text="Downloaded!\nFile saved to `{}`".format(
                "nana/downloads/" + filename.replace(" ", "_")),
        )
    elif len(
            message.text.split()) == 3 and message.text.split()[1] == "upload":
        filerealname = message.text.split()[2].split(None, 1)[0]
        filename = "nana/downloads/{}".format(filerealname.replace(" ", "_"))
        checkfile = os.path.isfile(filename)
        if not checkfile:
            await edit_or_reply(
                message, text="File `{}` was not found!".format(filerealname))
            return
        await edit_or_reply(message,
                            text="Uploading `{}`...".format(filerealname))
        upload = drive.CreateFile({
            "parents": [{
                "kind": "drive#fileLink",
                "id": drive_dir
            }],
            "title":
            filerealname,
        })
        upload.SetContentFile(filename)
        upload.Upload()
        upload.InsertPermission({
            "type": "anyone",
            "value": "anyone",
            "role": "reader"
        })
        await edit_or_reply(
            message,
            text="Uploaded!\nDownload link: [{}]({})".format(
                filerealname,
                upload["alternateLink"],
            ),
        )
    elif len(
            message.text.split()) == 3 and message.text.split()[1] == "mirror":
        await edit_or_reply(message, text="Mirroring...")
        driveid = await get_driveid(message.text.split()[2])
        if not driveid:
            await edit_or_reply(
                message,
                text="Invaild URL!",
            )
            return
        filename = await get_driveinfo(driveid)
        if not filename:
            await edit_or_reply(
                message,
                text="Invaild URL!",
            )
            return
        mirror = (drive.auth.service.files().copy(
            fileId=driveid,
            body={
                "parents": [{
                    "kind": "drive#fileLink",
                    "id": drive_dir
                }],
                "title": filename,
            },
        ).execute())
        new_permission = {
            "type": "anyone",
            "value": "anyone",
            "role": "reader"
        }
        drive.auth.service.permissions().insert(fileId=mirror["id"],
                                                body=new_permission).execute()
        await edit_or_reply(
            message,
            text="Done!\nDownload link: [{}]({})".format(
                filename, mirror["alternateLink"]),
        )
    elif len(message.text.split()) == 2 and message.text.split(
    )[1] == "tgmirror":
        if message.reply_to_message:
            await edit_or_reply(message, text="__Downloading...__")
            c_time = time.time()
            if message.reply_to_message.photo:
                if message.reply_to_message.caption:
                    nama = f"{message.reply_to_message.caption}.png".replace(
                        " ", "_")
                else:
                    nama = f"photo_{message.reply_to_message.photo.date}.png"
                await client.download_media(
                    message.reply_to_message.photo,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: client.loop.create_task(
                        progressdl(d, t, message, c_time, "Downloading...")),
                )
            elif message.reply_to_message.animation:
                if message.reply_to_message.caption:
                    nama = f"{message.reply_to_message.caption}.gif".replace(
                        " ", "_")
                else:
                    nama = "giphy_{}-{}.gif".format(
                        message.reply_to_message.animation.date,
                        message.reply_to_message.animation.file_size,
                    )
                await client.download_media(
                    message.reply_to_message.animation,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: client.loop.create_task(
                        progressdl(d, t, message, c_time, "Downloading...")),
                )
            elif message.reply_to_message.video:
                if message.reply_to_message.caption:
                    nama = f"{message.reply_to_message.caption}.mp4".replace(
                        " ", "_").replace(".mkv", "")
                else:
                    nama = "video_{}-{}.mp4".format(
                        message.reply_to_message.video.date,
                        message.reply_to_message.video.file_size,
                    )
                await client.download_media(
                    message.reply_to_message.video,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: client.loop.create_task(
                        progressdl(d, t, message, c_time, "Downloading...")),
                )
            elif message.reply_to_message.sticker:
                if not message.reply_to_message.caption:
                    nama = "sticker_{}_{}.webp".format(
                        message.reply_to_message.sticker.date,
                        message.reply_to_message.sticker.set_name,
                    )
                else:
                    nama = f"{message.reply_to_message.caption}.webp".replace(
                        " ", "_")
                await client.download_media(
                    message.reply_to_message.sticker,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: client.loop.create_task(
                        progressdl(d, t, message, c_time, "Downloading...")),
                )
            elif message.reply_to_message.audio:
                if message.reply_to_message.caption:
                    nama = f"{message.reply_to_message.caption}.mp3".replace(
                        " ", "_")
                else:
                    nama = "audio_{}.mp3".format(
                        message.reply_to_message.audio.date)
                await client.download_media(
                    message.reply_to_message.audio,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: client.loop.create_task(
                        progressdl(d, t, message, c_time, "Downloading...")),
                )
            elif message.reply_to_message.voice:
                if message.reply_to_message.caption:
                    nama = f"{message.reply_to_message.caption}.ogg".replace(
                        " ", "_")
                else:
                    nama = "audio_{}.ogg".format(
                        message.reply_to_message.voice.date)
                await client.download_media(
                    message.reply_to_message.voice,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: client.loop.create_task(
                        progressdl(d, t, message, c_time, "Downloading...")),
                )
            elif message.reply_to_message.document:
                nama = "{}".format(message.reply_to_message.document.file_name)
                await client.download_media(
                    message.reply_to_message.document,
                    file_name="nana/downloads/" + nama,
                    progress=lambda d, t: client.loop.create_task(
                        progressdl(d, t, message, c_time, "Downloading...")),
                )
            else:
                await edit_or_reply(message, text="Unknown file!")
                return
            upload = drive.CreateFile({
                "parents": [{
                    "kind": "drive#fileLink",
                    "id": drive_dir
                }],
                "title":
                nama,
            })
            upload.SetContentFile("nana/downloads/" + nama)
            upload.Upload()
            upload.InsertPermission({
                "type": "anyone",
                "value": "anyone",
                "role": "reader"
            })
            await edit_or_reply(
                message,
                text="Done!\nDownload link: [{}]({})".format(
                    nama, upload["alternateLink"]),
            )
            os.remove("nana/downloads/" + nama)
        else:
            await edit_or_reply(message,
                                text="Reply document to mirror it to gdrive")
    elif len(message.text.split()) == 3 and message.text.split(
    )[1] == "urlmirror":
        await edit_or_reply(message, text="Downloading...")
        URL = message.text.split()[2]
        nama = URL.split("/")[-1]
        time_dl = await download_url(URL, nama)
        if "Downloaded" not in time_dl:
            await edit_or_reply(message,
                                text="Failed to download file, invaild url!")
            return
        await edit_or_reply(
            message, text=f"Downloaded with {time_dl}.\nNow uploading...")
        upload = drive.CreateFile({
            "parents": [{
                "kind": "drive#fileLink",
                "id": drive_dir
            }],
            "title":
            nama
        })
        upload.SetContentFile("nana/downloads/" + nama)
        upload.Upload()
        upload.InsertPermission({
            "type": "anyone",
            "value": "anyone",
            "role": "reader"
        })
        await edit_or_reply(
            message,
            text="Done!\nDownload link: [{}]({})".format(
                nama, upload["alternateLink"]),
        )
        os.remove("nana/downloads/" + nama)
    else:
        await edit_or_reply(
            message,
            text="Usage:\n-> `gdrive download <url/gid>`\n"
            "->`gdrive upload <file>`\n"
            "->`gdrive mirror <url/gid>`\n\nFor "
            "more information about this, go to your assistant.",
        )
Пример #28
0
def send_csv_to_drive(fileName, fileAlias, target_dir="slideInfo_BioBasic"):

    logger.log('begin file upload')

    client_secrets_path = BASE_DIR + "/client_secrets.json"
    credentials_path = BASE_DIR + "/credentials.txt"

    GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = client_secrets_path

    # Create google account authentication objects
    gauth = GoogleAuth()

    logger.log('Looking for credentials')

    if os.path.exists(credentials_path):
        logger.log('found a credentials')
        gauth.LoadCredentialsFile(credentials_path)

    if gauth.credentials is None:  # or gauth.access_token_expired:
        logger.log('local connect to website')
        gauth.LocalWebserverAuth()
        gauth.SaveCredentialsFile(credentials_path)

    elif gauth.access_token_expired:
        logger.log('refresh branch')
        gauth.Refresh()

    else:
        logger.log('authorize branch')
        gauth.Authorize()

    logger.log('creating connection to google drive')

    gauth.SaveCredentialsFile(credentials_path)

    drive = GoogleDrive(gauth)

    logger.log('connection established')
    ''' Find the name of the folder we want to upload to '''
    # Define the folder we want to upload to
    target_folder_name = target_dir
    target_folder_id = ''

    logger.log('finding drive folder: ' + target_dir)

    folder_not_found = True

    while (folder_not_found):

        # Find the list of all of the files in the google drive
        file_list = drive.ListFile({
            'q': "'root' in parents and trashed=false"
        }).GetList()

        # Loop through all of the files in the
        for file_object in file_list:

            # Check if the current one is our target
            if file_object['title'] == target_folder_name:

                # Save the folder id
                target_folder_id = file_object['id']

                # Exit the while loop
                folder_not_found = False

        # Check if the folder was found
        if target_folder_id == '':

            logger.log('folder not found. Creating one')

            # Create the folder we want
            folder = drive.CreateFile({
                'title':
                target_folder_name,
                'mimeType':
                'application/vnd.google-apps.folder'
            })

            # Upload the folder to the drive
            folder.Upload()

            # The loop will go again, but now it will find the folder

    logger.log("folder found. id: " + target_folder_id)

    upload_csv = drive.CreateFile({
        'title': fileAlias + '.csv',
        'parents': [{
            'id': target_folder_id
        }]
    })
    upload_csv.SetContentFile(fileName + '.csv')
    upload_csv.Upload()

    logger.log('file uploaded')
Пример #29
0
 print("GPU RAM Free: {0:.0f}MB | Used: {1:.0f}MB | Util {2:3.0f}% | Total {3:.0f}MB".format(gpu.memoryFree, gpu.memoryUsed, gpu.memoryUtil*100, gpu.memoryTotal))
printm()

# Code to read csv file into colaboratory:
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

downloaded = drive.CreateFile({'id':'1nSAqJYO5thSWS0gFWDgmTTtsN7DJtQKD'}) # replace the id with id of file you want to access
downloaded.GetContentFile('Testing_Set1.csv')

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
df = pd.read_csv('Testing_Set1.csv', sep = ';') 
y_test = df['HP Flare']
df = df.drop('HP Flare', axis = 1)
df = df.interpolate(method = 'linear', axis = 0).ffill().bfill()
pca = PCA(n_components = 0.999)
projected = pca.fit_transform(df)

fig = plt.figure(figsize=(7,6))
ax = fig.add_subplot(111, projection = '3d')
Пример #30
0
class UploadDrive:



    def __init__(self):
        """Create an instance of UploadDrive."""
        self.gauth = GoogleAuth()
        self.drive = GoogleDrive(self.gauth)

    def connect(self):
        """""
        flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
            'client_secrets.json',
            scopes=['https://www.googleapis.com/auth/drive'])
        # Use kwargs to set optional request parameters.
        authorization_url, state = flow.authorization_url(
            # Enable offline access so that you can refresh an access token without
            # re-prompting the user for permission. Recommended for web server apps.
            access_type='offline',
            # Enable incremental authorization. Recommended as a best practice.
            include_granted_scopes='true')
        
        self.gauth.LoadCredentialsFile("mycreds.txt")
        if self.gauth.credentials is None:
            # Authenticate if they're not there
            self.gauth.LocalWebserverAuth()
        elif self.gauth.access_token_expired:
            # Refresh them if expired
            self.gauth.Refresh()
        else:
            # Initialize the saved creds
            self.gauth.Authorize()
        # Save the current credentials to a file
        self.gauth.SaveCredentialsFile("mycreds.txt")

        from googleapiclient.discovery import build
        from oauth2client.client import GoogleCredentials
        cred = oauth2client.client.GoogleCredentials(access_token, client_id, client_secret,
                                                     refresh_token, expires_at,
                                                     "https://accounts.google.com/o/oauth2/token", some_user_agent)
        http = cred.authorize(httplib2.Http())
        cred.refresh(http)
        self.gmail_service = discovery.build('gmail', 'v1', credentials=cred)
"""""

    def write_file_on_cloud(self, path, title="values.csv"):
        """Create GoogleDriveFile instance
        :param path: path of the file
        :type path: str
        :param title: title in google drive
        :type title: str
        """
        self.connect()
        file1 = self.drive.CreateFile()
        file1.SetContentFile(path)
        file1['title'] = title
        file1.Upload()

    # Auto-iterate through all files that matches this query
    def find_file_title_on_cloud(self, title=''):
        """find a given file by its title on the cloud
        :param title:The title of the text file
        :type title: str
        :return file_id
        """
        self.connect()

        file_list = self.drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
        for file in file_list:
            print('title: %s, id: %s' % (file['title'], file['id']))
            if file['title'] == title:
                print('found : ' + title)
                return file['title']
        print('file not found : ' + title)
        return 404

    def find_file_id_on_cloud(self, title=''):
        """Find a file by its id on the cloud
        :param title:The title of the text file
        :type title: str
        :return file_id
        """
        # results=service.files().list().execute()
        # file_list=results.get('items',[])
        self.connect()

        file_list = self.drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
        for file in file_list:
            print('title: %s, id: %s' % (file['title'], file['id']))
            if file['title'] == title:
                print('found : ' + title)
                return file['id']
                print('file not found : ' + title)
        return 404

    def download_file_from_cloud(self, file_id, path):
        """Download a Drive file's content to the local filesystem.
        :param file_id: ID of the Drive file that will downloaded.
        :type file_id: str
        :param path: where the file is written
        :type path: str
        :return if the download succeeded
        """
        self.connect()

        if self.internet_on():
            local_fd = open(path + "commands.csv", "wb")
            request = self.drive.auth.service.files().get_media(fileId=file_id)
            media_request = http.MediaIoBaseDownload(local_fd, request)
            while True:
                try:
                    download_progress, done = media_request.next_chunk()
                except errors.HttpError as error:
                    print
                    'An error occurred: %s' % error
                    return False
                if download_progress:
                    print
                    'Download Progress: %d%%' % int(download_progress.progress() * 100)
                if done:
                    print
                    'Download Complete'
                    return True
        else:
            return False

    def delete_file_on_cloud(self, file_title):
        # HTTP request DELETE
        """Permanently delete a file, skipping the trash.
        :param file_title: ID of the file to delete.
        :type file_title: str
        :return if file didn't delete
        """
        self.connect()

        file_list = self.drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
        for file in file_list:
            print('title: %s, id: %s' % (file['title'], file['id']))
            if file['title'] == file_title:
                print('found : ' + file_title)
                self.drive.auth.service.files().delete(fileId=file['id']).execute()
        print('file not found : ' + file_title)
        return 404

    @staticmethod
    def internet_on():
        """Check if the connection to the internet works
        :return if there is a internet connexion
        """
        try:
            urlopen('http://216.58.192.142', timeout=1)
            return True
        except URLError as err:
            return False