playlist.py

#!/usr/bin/python
# -*- coding: utf-8 -*- 
import os
import mimetypes
import string
import sys
import shutil
import getopt
import re
 
class main:
 
    def usage(self):
        print('%s %s dir te explore'%('-i', '--indir'))
        print('%s %s playlist output'%('-p', '--playlist'))
        print('exemple: %s %s %s'%(__file__, '--indir=/var/www/mp3', '-p ./blu.xml'))
 
    def __init__(self, argv):
        self.indir = 0
        self.playlist = 'playlist.xml'
        try:
            opts, args = getopt.getopt(argv, "i:p", ["indir=", "playlist="])
        except getopt.GetoptError:
            self.usage()
            sys.exit(2)
 
        for opt, arg in opts:
            if opt in ('-i', '--indir'):
                if(os.path.exists(arg) == 0):
                    sys.exit(1)
                print('walk %s'%(arg))
                self.indir = arg
            elif opt in ('-p', '--playlist'):
                print('playlist %s'%(arg))
                self.playlist = arg
 
        if self.indir != 0:
            self.run()
        else:
            self.usage()
 
    def strReplace(self, str, array):
        for search, replace in array:
            str = str.replace(search, replace)
        return str
 
    def _human_key(self, key):
        parts = re.split('(\d*\.\d+|\d+)', key)
        return tuple((e.swapcase() if i % 2 == 0 else float(e))
                     for i, e in enumerate(parts))
 
    def run(self):
        print 'run'
        xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
        xml += '<playlist version="1" xmlns="http://xspf.org/ns/0/">\n'
        xml += '<title>'+os.path.split(self.playlist)[1].replace('_', ' ').replace('.xml', '')+'</title>\n'
        xml += '<creator>Bayard</creator>\n'
        xml += '<link></link>\n'
        xml += '<info></info>\n'
        xml += '<image></image>\n'
        xml += '<trackList>\n'
        for dirpath, dirnames, filenames in os.walk(self.indir):
            c = os.path.split(dirpath)[1]
            filenames.sort(key=self._human_key)
            for filename in filenames:
                print dirpath+'/'+filename
                newfilename = self.strReplace(filename, [(' ', '_'), ('–', '-')]);
                fo_path = os.path.join(dirpath, filename)
                fn_path = os.path.join(dirpath, newfilename)
                f_mime = mimetypes.guess_type(fo_path)
                if(os.path.exists(fn_path) == 0):
                    print('%s move to %s'%(fo_path, fn_path))
                    shutil.move(fo_path, fn_path)      
                if f_mime[0] == 'audio/mpeg':
                    xml += '  <track>\n'
                    xml += '    <title>'+newfilename.replace('_', ' ').replace('.mp3', '')+'</title>\n';
                    xml += '    <location>http://bayardmusique.bayardcdn.com/'+c+'/'+filename+'</location>\n';
                    xml += '  </track>\n'
        xml += '</trackList>\n'
        xml += '</playlist>'
        self.write(xml)
 
    def write(self, xml):
        print('write %s'%(self.playlist))
        f = open(self.playlist, 'w')
        f.write(xml)
        f.close()
 
 
if __name__ == "__main__":
    main(sys.argv[1:])

python: httpget.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python
import sys
import getopt
import httplib
import urllib
 
class main:
    def __init__(self, argv):
        self.url = 0
        self.port = 80
        self.count = 50
        self.uri = '/'
 
        try:
            opts, args = getopt.getopt(argv, "h:u:c:ur", ["help", "url=", "uri="])
        except getopt.GetoptError:
            self.usage()
            sys.exit(2)
 
        for opt, arg in opts:
            if opt in ('-h', '--help'):
                self.usage()
                sys.exit(0)
            if opt in ('-u', '--url'):
                self.url = arg
            elif opt in ('-p', '--port'):
                self.port = arg
            elif opt in ('-c', '--count'):
                self.count = arg
            elif opt in ('-p', '--uri'):
                self.uri = arg
 
        if self.url != 0:
            self.run()
        else:
            self.usage()
 
    def run(self):
        for i in range(0, int(self.count)):
            conn = httplib.HTTPConnection(self.url, self.port)
            conn.request('GET', self.uri)
            response = conn.getresponse()
            print("#%i %s, %s %s"%(i, self.url+self.uri, response.status, response.reason))
            conn.close()
 
    def usage(self):
        print('%s %s'%('-h', '--help'))
        print('%s %s'%('-u', '--url'))
        print('%s %s'%('-c', '--count'))
        print('%s %s'%('-ur', '--uri'))
        print('exemple: %s %s %s %s'%(__file__, '--url=be-geek.com', '-c 10', '--uri=/'))
 
if __name__ == "__main__":
    main(sys.argv[1:])

PYTHON Script, Couper des mp3 recursivement

Script qui sert a répliquer l’arborescence d’un répertoire de mp3 dans un repertoire de sortie en les coupant pour faire des extrait musicaux, c’est pas beau tout ça ?!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python
import os
import mimetypes
import string
import sys
import shutil
 
input = '/media/disk/originaux/'
output = '/media/disk/extraits/'
 
def grey(string):
    return '\x1b\x5b1;30;40m%s\x1b\x5b0;37;40m'%(string)
 
def red(string):
    return '\x1b\x5b1;31;40m%s\x1b\x5b0;37;40m'%(string)
 
def green(string):
    return '\x1b\x5b1;32;40m%s\x1b\x5b0;37;40m'%(string)
 
def yellow(string):
    return '\x1b\x5b1;33;40m%s\x1b\x5b0;37;40m'%(string)
 
def blue(string):
    return '\x1b\x5b1;34;40m%s\x1b\x5b0;37;40m'%(string)
 
def orange(string):
    return '\x1b\x5b1;40;40m%s\x1b\x5b0;37;40m'%(string)
 
if(os.path.exists(output) == 0):
    print("mkdir %s"%(output))
    os.mkdir(output)
 
#On preparre le repertoire de sortie en replicant l'arborescence du repertoire d'entre
for dirpath, dirnames, filenames in os.walk(input):
    tmp = dirpath.replace(input, output)
    if(os.path.exists(tmp) == 0):
        print('mkdir %s'%(tmp))
        os.mkdir(tmp)
 
mp3_count = 0
xml_count = 0
mp3_cut = 0
#On fait cqu'on a faire, forcement a un moment faut y aller et on va se prendre un cafe
for dirpath, dirnames, filenames in os.walk(input):
    for filename in filenames:
        f_mime = mimetypes.guess_type(filename)
        if f_mime[0] == 'audio/mpeg':
            mp3_count += 1
            mp3_in = os.path.join(dirpath, filename)
            mp3_out = os.path.join(dirpath.replace(input, output), filename)
            cut_line = 'cutmp3 -i "%s" -a 0:30 -b 1:00 -O "%s"'%(mp3_in, mp3_out)
            if(os.path.exists(mp3_out) == 0):
                mp3_cut += 1
                print('#%s %s (%s)\n      cut to \n      %s'%(grey(mp3_cut) ,yellow(mp3_in), blue(f_mime[0]) , green(mp3_out)))
                a = os.popen('%s'%(cut_line)).readlines()
                for p in a: 
                    if p != '\n':
                        if 'ERROR' in p:
                            print('      %s'%(red(p.strip())))
                        elif 'WARNING' in p:
                            print('      %s'%(red(p.strip())))
                        else:
                            print('      %s'%(green(p.strip())))
 
        elif f_mime[0] == 'application/xml':
            xml_count += 1
            xml_in = os.path.join(dirpath, filename)
            xml_out = os.path.join(dirpath.replace(input, output), filename)
            if(os.path.exists(xml_out) == 0):
                print('      %s (%s) copy to %s'%(blue(xml_in), blue(f_mime[0]) , blue(xml_out)))
                shutil.copyfile(xml_in, xml_out)
 
print
print('      mp3 count: %s, xml count: %s, mp3 cutted: %s'%(green(mp3_count), green(xml_count), green(mp3_cut)))            
print

PYTHON Script, verifier la syntaxe d’un projet php

python php_syntax.py (-e pour voir seulement les fichiers contenant une erreur)

import os
import mimetypes
import string
import sys
 
def red(string):
    return '\x1b\x5b1;31;40m%s\x1b\x5b0;37;40m'%(string)
 
def green(string):
    return '\x1b\x5b1;32;40m%s\x1b\x5b0;37;40m'%(string)
 
def count(list):
    line_count = 0
    for line in list:
        line_count += 1
    return line_count
 
mimeArray = ['application/x-httpd-php']
errors_count = 0
files_count = 0
lines_count = 0
bytes_count = 0
 
if '-e' in sys.argv:
    showonlyerror = 1
else:
    showonlyerror = 0
 
for path, dirs, files in os.walk('.'):
    for file in files:
        f_mime = mimetypes.guess_type(file)
        if f_mime[0] in mimeArray:
            f_path = os.path.join(path, file)
            a = os.popen('php -l %s'%(f_path)).readlines()
            b = open(f_path)
            counta = count(a)
            sizea = os.path.getsize(f_path)
            line_count = count(b)
            files_count += 1
            bytes_count += sizea
            lines_count += line_count
            if counta > 1:
                errors_count += 1
                print('%s'%(red(f_path)))
                print('    %s, %s line(s), %s byte(s)'%(f_mime[0], line_count, sizea))
                for p in a:
                    if p != '\n':
                        print('    %s'%(p.strip()))
            else:
                if showonlyerror == 0:
                    print('%s'%(green(f_path)))
                    print('    %s, %s line(s), %s byte(s)'%(f_mime[0], line_count, sizea))
                    for p in a:
                        print('    %s'%(p.strip()))
print
if errors_count > 0:
    print('    file(s): %i, lines: %i, bytes: %i, error: %s'%(files_count, lines_count, bytes_count, red(errors_count)))
else:
    print('    file(s): %i, lines: %i, bytes: %i, error: %s'%(files_count, lines_count, bytes_count, green(errors_count)))

sorti:

PYTHON Script, image resize

Convertir des images pour le web de façon récursive et en reproduisant a la sortie la même arborescence que dans le répertoire d’origine,
Ce script est appeler a être modifier souvent, pour finir par arriver vers ce que je veux faire multi-threader la conversion et que çe soit le plus efficace possible ^^

import os
import string
import mimetypes
import Image
import ImageFilter
 
class pix:
    def __init__(self, pixdir, outputdir):
        self.pixdir = pixdir
        self.outputdir = outputdir
        self.mimeArray = ['image/jpeg', 'image/png']
        self.prepare()
        self.overwrite = 'N'
 
    def prepare(self):
        if(os.path.exists(self.outputdir) == 0):
            print("mkdir %s"%(self.outputdir))
            os.mkdir(self.outputdir)
        pixArray = {}
        i = 0
        for path, dirs, files in os.walk(self.pixdir):
            tmp = os.path.join(self.outputdir , path.replace(self.pixdir, ''))
            if os.path.exists(tmp) == 0:
                os.mkdir(tmp)
                print("mkdir %s"%(tmp))
            for file in files:
                f_path = os.path.join(path, file)
                f_mime = mimetypes.guess_type(f_path)
                if f_mime[0] in self.mimeArray:
                    f_size = os.path.getsize(f_path)
                    pixArray[i] = {'oldpath': os.path.join(path, file),
                                   'newpath': os.path.join(tmp, file.lower()),
                                   'size': self.o2ko(f_size),
                                   'mimetype': f_mime[0]}
                    i += 1
        self.count = i
        self.pixArray = pixArray
 
    def o2ko(self, o):
        return o/1024
 
    def convert(self, oldpath, newpath):
        try:
            bf_size = self.o2ko(os.path.getsize(oldpath))
            bi = Image.open(oldpath)
 
            if bi.size[0] > 2500:
                w = bi.size[0] * 50 / 100
                h = bi.size[1] * 50 / 100
            else:
                w = bi.size[0]
                h = bi.size[1]
 
            bi = bi.resize((w, h), Image.ANTIALIAS)
            bi.save(newpath, 'JPEG')     
            ai = Image.open(newpath)
            af_size = self.o2ko(os.path.getsize(newpath))
            print("\n[%ix%i %sKo %s] %s\n   to \n[%ix%i %sKo %s] %s"%(bi.size[0],
                                                                      bi.size[1],
                                                                      bf_size,
                                                                      bi.mode,
                                                                      oldpath,
                                                                      ai.size[0],
                                                                      ai.size[1],
                                                                      af_size,
                                                                      ai.mode,
                                                                      newpath))
        except IOError:
            print("\nimage file %s is endomaged"%(oldpath))
 
    def run(self):
        for i in self.pixArray:
            if os.path.exists(self.pixArray[i]['newpath']) == 1:
                if self.overwrite != 'ALL' and self.overwrite != 'NALL':
                    self.overwrite = raw_input("Overwrite %s [Y,N,ALL,NALL]: [N] "%(self.pixArray[i]['newpath']))
                if self.overwrite not in ['Y', 'N', 'ALL', 'NALL']:
                    self.overwrite = raw_input("Please answer [Y,N,ALL,NALL]: [N] ")
 
            if self.overwrite == 'Y' or self.overwrite == 'ALL':
                self.convert(self.pixArray[i]['oldpath'], self.pixArray[i]['newpath'])
            if self.overwrite == 'NALL' or os.path.exists(self.pixArray[i]['newpath']) == 0:
                self.convert(self.pixArray[i]['oldpath'], self.pixArray[i]['newpath'])
 
    def stats(self, dir):
        n_files = 0
        d_size = 0
        for path, dirs, files in os.walk(dir):
            for file in files:
                f_path = os.path.join(path, file)
                n_files += 1
                d_size = d_size + os.path.getsize(f_path)
        return {'n_files': n_files, 'd_size': d_size}
 
    def __del__(self):
        before = self.stats(self.pixdir)
        after = self.stats(self.outputdir)
        print('Before: %i files for %sKo'%(before['n_files'], self.o2ko(before['d_size'])))
        print('After : %i files for %sKo'%(after['n_files'], self.o2ko(after['d_size'])))
 
 
pix = pix('/home/hio/Photos/', # from
          '/home/hio/Photos_Web/') # to
pix.run()

PYTHON str_replace en python

Comment faire un equivalent de str_replace() de php en python

La fonction strReplace

1
2
3
4
5
6
7
8
import os
import string
import shutil
 
def strReplace(str, array):
    for search, replace in array:
        str = str.replace(search, replace)
    return str

Utilisation

replace = [(' ', '_'), ('-', '')]
 
for dir in os.listdir('.'):
    newdir = strReplace(dir, replace).capitalize()
    print ("moving: %s > %s"% (dir, newdir))
    os.rename(dir, newdir)

ce qui nous donne dans ce cas précis

moving: Angel of Retribution > Angel_of_retribution
moving: Living After Midnight > Living_after_midnight
moving: British Steel > British_steel
moving: Defenders Of The Faith > Defenders_of_the_faith
moving: Killing Machine > Killing_machine
moving: Ram It Down > Ram_it_down
moving: Hell Bent For Leather > Hell_bent_for_leather
moving: Stained Class > Stained_class
moving: Sin After Sin > Sin_after_sin
moving: Jugulator > Jugulator
moving: Screaming For Vengeance > Screaming_for_vengeance
moving: Turbo > Turbo
moving: Painkiller > Painkiller
moving: Rocka Rolla > Rocka_rolla
moving: Point Of Entry > Point_of_entry
moving: Demolition > Demolition
moving: Priest...Live! > Priest...live!
moving: Sad Wings Of Destiny > Sad_wings_of_destiny

PYTHON Comment faire une requete http

Faire des requetes grace a python et httplib

La class HttpRequest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python                                                                                        
 
import httplib
import urllib
 
class HttpRequest:
    def __init__(self, proxyhost, proxyport):
        self.proxyhost = proxyhost
        self.proxyport = proxyport
        self.cnx = httplib.HTTPConnection(self.proxyhost, self.proxyport, 20)
 
    def get(self, url):
        self.cnx.request("GET", url)
        response = self.cnx.getresponse()
        print("url: %s, %s %s"%(url, response.status, response.reason))
        self.msg = response.msg
        self.content = response.read()
 
    def post(self, url, params = ''):
        headers = {"Content-type":"application/x-www-form-urlencoded", "Accept":"text/plain"}
        if(len(params) > 0):
            params = urllib.urlencode(params)
        self.cnx.request("POST", url, params, headers)
        response = self.cnx.getresponse()
        print("url: %s, %s %s"%(url, response.status, response.reason))
        self.msg = response.msg
        self.content = response.read()
 
    def getresponse(self):
        print(self.msg)
 
    def getcontent(self):
        print(self.content)
 
    def __del__(self):
        self.cnx.close()
httprequest = HttpRequest("127.0.0.1", "3777")

En GET

httprequest.get('http://www.google.fr')
httprequest.get('http://blog.hio.fr')
httprequest.get('http://blog.hio.fr/blu.html')
httprequest.get('http://blog.if-else.fr')

Renvoi

url: http://www.google.fr, 200 OK
url: http://blog.hio.fr, 200 OK
url: http://blog.hio.fr/blu.html, 404 Not Found
url: http://blog.if-else.fr, 200 OK

En POST

params = {"login":"login",
          "password":"password"}
httprequest.post('http://www.url-bidon.fr/login/connexion', params)

Renvoi par exemple

url: http://www.url-bidon.fr/login/connexion, 302 Moved Temporarily

Et si on veut en savoir plus on a les fonctions getresponse() et getcontent()

httprequest.getresponse()

Renvoi

Date: Sun, 15 Nov 2009 11:04:55 GMT
Server: Apache
X-Powered-By: PHP/5.2.6-1+lenny3
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=d1d0958e52fb25b874a573bd1c261b1a; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Location: /login/failed
Vary: Accept-Encoding
Content-Length: 0
Content-Type: text/html; charset=utf-8
X-Cache: MISS from proxy.hio.fr
X-Cache-Lookup: MISS from proxy.hio.fr:3777
Via: 1.0 proxy.hio.fr (squid/3.0.STABLE8)
Proxy-Connection: close
httprequest.getcontent()

Renvoi le contenu de la page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 
etc...