1

download files from disketta

Тема: download files from disketta

Hello guys. Is there any way through API to download files from disketta?
We are using disketta app for uploading of tachograph files from bce fm 500 tacho, but would like also to download them at a specific time at night.
Thanks!

2

download files from disketta

Re: download files from disketta

Nevermind, i\ve done it.

3

download files from disketta

Re: download files from disketta

Hi how did you do it?

4

download files from disketta

Re: download files from disketta

I made a python script. I made it in a hurry, so it isn't that pretty, but it works smile It is scheduled to run every night. It uses a flat .txt file, called "db.txt" where it stores the tacho file names, so that whenever it runs again it won't download the existing ones again. For every file/files downloaded, it makes an archive out of them with the current datetime as name. You also need to have the "tachograph" folder existing in disketta, otherwise it will throw an error and i didn't have time to make an exception for non-existing folders, because this client of mine has all the trucks with tachograph downloading, and by default already has the folder created.
You also need to install Wialon python wrapper for the remote api.


# -*- coding: utf-8 -*-
from wialon.api import Wialon, WialonError
from wialon import flags
import json
import urllib
import time
import datetime
import dateutil.relativedelta
import calendar
import os
import re
from pathlib import Path
import tarfile
import shutil
import unicodedata


fls = []
folders = []
step = 0
try:
    wialon_api = Wialon()
    result = wialon_api.token_login(token='xxxxxxxxxxxxxxxxxxxxx')
    wialon_api.sid = result['eid']

    result = wialon_api.avl_evts()

    print wialon_api.sid
    spec = {
        "itemsType": "avl_unit",
        "propName": "sys_name",
        "propValueMask": "*",
        "sortType": "sys_name"
    }
    
 
    interval = {"from": 0, "to": 0}


    try:
        units = wialon_api.core_search_items(spec=spec, force=1, flags=flags.ITEM_DATAFLAG_BASE, **interval)
        for i in units['items']:
            gen = {
            "itemId": i['id'],
            "storageType": 2,
            "path": "/tachograph",
            "mask": "*",
            "recursive": "false",
            "fullPath": "false"
            }
            itId = i['id']
            getSid = wialon_api.sid
            print i['nm']
            folder = i['nm']
            if not os.path.exists(folder):
                os.makedirs(folder)
                folders.append(folder)
            if os.path.exists(folder):
                files = wialon_api.file_list(**gen)
                for i in files:
                    print i["n"]
                    repl = i["n"]
                    repl = ''.join((c for c in unicodedata.normalize('NFD', repl) if unicodedata.category(c) != 'Mn'))
                    tachofile = Path(folder + "/" + str(repl))
                    caut = folder + "/" + repl 
                    with open("db.txt") as f:
                        found = False
                        for line in f:  
                            if re.search("^{0}$".format(caut),line):    
                                print "File already exists"
                                found = True
                        if not found:
                         
                            print "File doesn't exist, downloading..."
                            for j in range (0,3):
                                try:
                                    print ()
                                    urllib.urlretrieve('https://hst-api.wialon.com/wialon/ajax.html?svc=file/get&params={"itemId":' + str(itId) + ',"storageType":2,"path":"/tachograph/' + str(repl) + '"}&sid=' + getSid, folder + '/' + str(repl))
                                    if tachofile.is_file():
                                        print "Downloaded"
                                        fls.append(folder + "/" + str(repl))
                                except Exception as e:
                                    print(e)
                                    continue
                                break
                            step = 1   
        if step == 1:
            timestr = time.strftime("%Y%m%d-%H%M%S")
            num_arh = timestr + "-tacho.tar.gz"
            tar = tarfile.open(num_arh, "w:gz")
            for g in fls:
                tar.add(g)
                print "Archiving " + g
                with open('db.txt', 'r+') as the_file:
                    gasit = False
                    for line in the_file:  
                        if re.search("^{0}$".format(g),line): 
                            print "already in file db"
                            gasit = True
                    if not gasit:
                        the_file.write(g + '\n')
            tar.close()
            pathArh = Path(num_arh)
            if pathArh.is_file():
                print "Archive " + num_arh + " was created"
                print "Deleting leftovers..."
                for f in fls:
                      os.remove(f)
            else:
                print "Problem creating the archive"

    except WialonError as e:
        print e


    wialon_api.core_logout()
except WialonError as e:
    pass