Показват се публикациите с етикет scripting python. Показване на всички публикации
Показват се публикациите с етикет scripting python. Показване на всички публикации

24.04.2019 г.

Python script to extract multiple rar file's

# TODO: Add check if file exists if try to extract and if true delete them before extract!

# Import some module's
import os
import sys
# Require to install patool module
import patoolib
import multiprocessing as mp

# Hard code param if not add from termonal
if len(sys.argv) > 1:
    assert os.path.exists(sys.argv[1])
    PATH_TO_FILE = sys.argv[1]
else:
    PATH_TO_FILE = r"D:\Movies\Man.with.a.Plan.S01.HDTV.x264-Scene"


def extract_function(line):
    dir = os.path.dirname(line)
    # This function use for extract file, need to use multiprocessing
    try:
        patoolib.extract_archive(line, outdir=dir)
    except:
        raise "Some error acquire when try to extract {line}".format(line=line)
    else:
        print("{line} was successfully extract!".format(line=line))


class MainCLass:

    def __init__(self, path_to_search, param):
        self.search_param = param
        self._path_to_Search = path_to_search

    def search_to_path(self):
        """
        Method used for search zip files in folder
        :return: List with file conteins rar
        """
        list_with_files = []
        for root, dirname, filename in os.walk(self._path_to_Search):
            for file in filename:
                if file.endswith(self.search_param):
                    list_with_files.append(os.path.join(root, file))
        return list_with_files

    def show_file_from_list(self):
        """
        Method use for conteins result of file with suffix rar and yield result
        :return:
        """
        list_with_file = self.search_to_path()
        for line in list_with_file:
            yield line

    def unrar_file_from_list(self):
        my_file = list(i for i in self.show_file_from_list())
        with mp.Pool() as pool:
            result = pool.map(extract_function, my_file)
            return result

    def delete_rar_archive(self):
        # Delete rar arhive! do after unrar them
        for line in self.show_file_from_list():
            os.unlink(line)


if __name__ == '__main__':
    zip_file = MainCLass(PATH_TO_FILE, 'rar')
    # uncoment to unrar file's
    zip_file.unrar_file_from_list()
    # uncoment to delte rar file's
    # zip_file.delete_rar_archive()

3.09.2017 г.

Python use picke to write json to faile and read back to dictionary

#import module
from pprint import pprint  # pretiprint
import json
import os
import pickle
# hard code param
test_file = 'test_file.json'
db = {}
dl = {}

def check_file_exists(filename):
    ''' Check file exists or not '''
    if not os.path.exists(filename):
        open(filename, 'w').close()

def write_in_json(filename, db):
    ''' Write dictionary to file with ekstension json'''
    db = json.dumps(db)
    with open(filename, 'wb') as f:
        pickle.dump(db, f)

def load_from_json_file(filename, dl):
    ''' Load serial object into file to dictionary '''
    json_file = open(filename, 'rb')
    dl = pickle.load(json_file)
    dlj = json.loads(dl)
    return dlj

# Get from enywere for test
borko = {'name': 'Borko', 'ip': '192.168.168.1', 'df': 30000, 'free': 'dev'}
sue = {'name': 'Sue Somebody', 'ip': '192.168.168.2', 'df': 40000, 'free': 'hdw'}
tom = {'name': 'Tom Aks', 'ip': '192.168.168.3', 'df': 0, 'free': None}

db['bob'] = borko
db['sue'] = sue
db['tom'] = tom
if __name__ == '__main__':
    # Create file if not exists uncoment to begin
    # check_file_exists(test_file)
    # function write json to file uncoment to begin
    #write_in_json(test_file, db)
    # fuction load picke file to json object return to dictionary
    #print(load_from_json_file(test_file, dl))

6.04.2015 г.

Python script for listen radion station online

# -*- coding: utf-8 -*-
"""
Created on Thu Sep 18 10:24:56 2014

@author: borko-8.1
"""

import platform
import os
print "---------------Radio Station's----------------"
trance={u'Трансе FM': 'http://listen.trance.fm/1/320', u'Elit trance music': 'http://www.slusham.com/radio/fmplus.m3u', u'Elit houce music': 'http://eilo.org:8000/house.m3u'}

bgradio={u'Energy':'http://80.72.68.217/nrj.ogg.m3u', u'Рок радио':'http://80.72.68.217/radio1rock_low.ogg', u'Радио Melody':'http://live.btvradio.bg/melody.mp3.m3u',u'Радио Nova':'http://149.13.0.81/nova.aac.m3u'}


def menu():
    print u"1 за меню"
    print u"2 за избор на жанр"
    print u"3 за изход"
    cmenu=0
    while cmenu !=3:
        print
        cmenu=input("Enter choice :")
        if cmenu == 1:
            menu()
            print
        elif cmenu == 2:
            chanel()
            print
        print
    
def chanel():
    print u"1 за трансе"
    print u"2 за БГ радио"
    print u"3 за изход"
    choice=0
    while choice!=3:
        choice = input("Choice radio vid :")
        if choice == 1:
            for i in range(len(trance)):
                print i, "-",trance.keys()[i]
            print
            tranceradio = input("Play radio")
            if platform.system() == "Windows":
                print "Windows system "
                path = "C:\\Program Files (x86)\\VideoLAN\\VLC"
                os.chdir( path )
                player = "vlc.exe "
                chanel = trance.values()[tranceradio]
                start = player+chanel
                os.system(start)
            if platform.system() == "linux":
                print "Linux system"
                player = "cvlc -vvv "
                chanel = trance.values()[tranceradio]
                start = player+chanel
                os.system(start)
        if choice == 2:
            for i in range(len(bgradio)):
                print i, "-",bgradio.keys()[i]
            print
            tranceradio = input("Push radio")
            if platform.system() == "Windows":
                print "Windows system "
                path = "C:\\Program Files (x86)\\VideoLAN\\VLC"
                os.chdir( path )
                player = "vlc.exe "
                chanel = bgradio.values()[tranceradio]
                start = player+chanel
                os.system(start)
            if platform.system() == "linux":
                print "Linux system"
                player = "cvlc -vvv "
                chanel = bgradio.values()[tranceradio]
                start = player+chanel
                os.system(start)


#if __name__ == "__main__":
#    menu()
menu()
raw_input()