Exemple de scripturi API

DigiStorage pentru Developeri

Pentru a rula aceste scripturi aveţi nevoie de următorul utilitar și de modulele python de mai jos:

  • apt-get install python-pip
  • pip install requests
  • pip install hurry.filesize
  • pip install progressbar

Exemplu de utilizare API DigiStorage (script dezvoltat în python):

import requests
import json
import getpass

email = raw_input('Email: ')
password = getpass.getpass()

api_base = 'https://storage.rcs-rds.ro'

s = requests.Session()

# get auth token

token = s.get(api_base + '/token', headers = {
    'X-Koofr-Email': email,
    'X-Koofr-Password': password
}).headers['X-Koofr-Token']

s.headers['Authorization'] = 'Token ' + token

# get mount (Digi Cloud, Dropbox...)

mounts = s.get(api_base + '/api/v2/mounts').json()['mounts']

mount = [x for x in mounts if x['name'] == 'Digi Cloud'][0]

print mount['name']

# list files

files = s.get(api_base + '/api/v2/mounts/' + mount['id'] + '/files/list', params = {'path': '/'}).json()['files']

for file in files:
    print file['name']

# get file

print s.get(api_base + '/content/api/v2/mounts/' + mount['id'] + '/files/get', params = {'path': '/test.txt'}).content

# upload file

print s.post(api_base + '/content/api/v2/mounts/' + mount['id'] + '/files/put', params = {'path': '/'}, files = {
    'file': ('filename.txt', 'file content')
}).json()[0]['name']

# create directory

s.post(api_base + '/api/v2/mounts/' + mount['id'] + '/files/folder', params = {'path': '/'},
    data = json.dumps({
        "name": "new directory"
    }),
    headers = {
        'content-type': 'application/json'
    }
)

Exemplu script digiupload.py pentru uploadul unui fișier sau al unui director in DigiStorage (script dezvoltat în python):

import requests
import json
import getpass
from hurry.filesize import size
import os
import sys
from progressbar import *

if len(sys.argv) < 3 or len(sys.argv) > 3:
    sys.exit('\nUtilizare: %s Folder_local Folder_cloud\n' % sys.argv[0])
else:
    email = raw_input('Email: ')
    password = getpass.getpass()

    api_base = 'https://storage.rcs-rds.ro'

    s = requests.Session()

# get auth token

    token = s.get(api_base + '/token', headers = {
        'X-Koofr-Email': email,
        'X-Koofr-Password': password
    }).headers['X-Koofr-Token']

    s.headers['Authorization'] = 'Token ' + token

# get mount (Digi Cloud, Dropbox...)

    mounts = s.get(api_base + '/api/v2/mounts').json()['mounts']

    mount = [x for x in mounts if x['name'] == 'Digi Cloud'][0]

# upload file

    def upload_file( str, path = '' ):
        f = open(str, 'r')
        content = f.read()
        s.post(api_base + '/content/api/v2/mounts/' + mount['id'] + '/files/put', params = {'path': '/'+path}, files = {
            'file': (str, content)
        }).json()[0]['name']
        f.close()

# create directory
    def mkdir( str, path = '' ):
        s.post(api_base + '/api/v2/mounts/' + mount['id'] + '/files/folder', params = {'path': '/' + path},
            data = json.dumps({
                "name": str
            }),
            headers = {
                'content-type': 'application/json'
            }
        )
    upload_dir = sys.argv[2]
    mkdir(upload_dir)
    rootDir = sys.argv[1]
    if os.path.isfile(rootDir):
        upload_file(rootDir,upload_dir)
    else:
        counter = 0
        for root, dirs, files in os.walk(rootDir):
            for file in files:
                counter += 1

        widgets = ['Total: ', Percentage(), ' ', Bar(marker='#',left='[',right=']'),' ', ETA(), ' ', FileTransferSpeed()]

        pbar = ProgressBar(widgets=widgets, maxval=counter)
        pbar.start()

        for dirName, subdirList, fileList in os.walk(rootDir):
            temp = ''
            string2 = dirName.split("/")
            path = upload_dir
            upload_path = upload_dir+"/"+dirName
            for i in range(len(string2)):
                if i == 0:
                    path = path
                    temp = path
                    mkdir(string2[i], temp)
                else:
                    path = path + "/"+ string2[i-1]
                    temp = path
                    mkdir(string2[i], temp)
            i = 0
            for fname in fileList:

                if dirName[2:] != '':
                    pbar.update(i)
                    upload_file(dirName+"/"+fname,upload_path)
                else:
                    pbar.update(i)
                    upload_file(fname,upload_path)
                i += 1
        pbar.finish()
print "\nupload-ul s-a finalizat!\n"

Exemplu de utilizare:

  • Uploadul unui director împreună cu toate subdirectoarele și toate fișierele pe care le conţine:

    $ python digiupload.py MUSIC/ Test Email: email@domeniu.tld Password: Total: 100% [####################################################################################################] Time: 00:00:52 0.47 B/s

    upload-ul s-a finalizat!

    $

  • Uploadul unui fișier:

    $ python digiupload.py image.png Imagini Email: email@domeniu.tld Password:

    upload-ul s-a finalizat!

    $

Înscrie-te la newsletter ca să fii la curent cu toate îmbunătățirile la care lucrăm!

Contact:     031.400.6500         *500 - apelabil din reteaua Digi Mobil