Python Manejar Windows Media Encoder con pywinauto
import pywinauto
from pywinauto import application
import sys
import time
app = application.Application()
#TODO en vez de hacer start conectar a la aplicacion si ya esta en marcha
#app.start_("c:/Archivos de Programa/Windows Media Components/Encoder/wmenc.exe")
try:
app.connect_(title_re = ".*Codificador de Windows Media.*") # Probar en espanyol
except:
try:
app.connect_(title_re = ".*Windows Media Encoder .*") # Probar en ingles
except:
try:
app.start_("c:/Archivos de Programa/Windows Media Components/Encoder/wmenc.exe")
except:
print("Windows Media Encoder no encontrado")
sys.exit(1)
ventana = app.MSWindowsMediaEncoder
while not(ventana.Exists()):
time.sleep(0.1)
propiedades = ventana.ScrollDockHolder
time.sleep(0.1)
# Todo ver cómo sacar la ventana de propiedades de la sesión si no está abierta
# Tomar el control de pestañas
tabs = ventana.Tab1
# Seleccionar la pestaña de salida
tabs.Select(1)
time.sleep(0.1)
# FIXME Al parecer lo siguiente no activa el nombre de archivo -> comprobar y ver si se puede arreglar
# Elegir almacenamiento en archivo
checkbox = ventana.Almacenarenarchivo
# Marcar el checkbox si no estaba activado
if (checkbox.GetCheckState()!=1):
checkbox.Click()
time.sleep(0.1)
nombrearchivo = ventana.NombredearchivoEdit
nombrearchivo.SetText("c:/video.wmv") # Ponerle un nombre de archivo para grabar ahÃ
# archivoanterior = nombrearchivo.TextBlock() # guardarnos el nombre de archivo anterior
def partition(iterable, func):
result = {}
for i in iterable:
result.setdefault(func(i), []).append(i)
return result
def group(seq):
result = {}
for item, category in seq:
result.setdefault(category, []).append(item)
return result
# def is_odd(n):
# return (n%2) == 1
# l = range(100)
# print partition(l, is_odd)
# print group( (item, is_odd(item)) for item in l)
# print group( (item, item%11) for item in l) # no need to use lamda/def
import os
import random
class localImages(object):
def __init__(self, directory='/home/Foto'):
self.directoryScan = directory # Cartella da cui cominciare a prelevare le foto
self.directoryRemain = [self.directoryScan]
self.filePath = {}
def getRandomImages(self):
'''
Prelieva dal proprio disco delle immagini in maniera random...
'''
for i in range(5): # Scansiona 5 directory
if len(self.directoryRemain) > 0:
directory = random.choice(self.directoryRemain)
self.directoryRemain.remove(directory)
files = []
try:
files = os.listdir(directory)
except:
pass
# Serve per aumentare il Random delle Foto
# FIXME: cercare di aumentare questo random
if len(files) > 0:
for i in range(len(files)/2):
files_remove = random.choice(files)
files.remove(files_remove)
##########################################
for filename in files:
filepath = os.path.join(directory, filename)
if os.path.isdir(filepath):
self.directoryRemain.append(filepath)
elif os.path.isfile(filepath):
(name, ext) = os.path.splitext(filepath)
if ext.lower() in ('.jpg', '.gif'):
self.filePath[filepath] = 0
if len(self.directoryRemain) == 0: self.directoryRemain = [self.directoryScan]
def downloadImages(self):
'''
Scarica nella cartella localIMGs le foto che vengono trovate nel disco...
'''
numberIMGs = len(self.filePath)
posIMGs = 1
for imageName in self.filePath:
print '[' + str(posIMGs) + '/' + str(numberIMGs) + '] - ' + imageName
fread = open(imageName, 'rb')
fwrite = open('localIMGs' + os.sep + os.path.split(imageName)[1], 'wb')
fwrite.write(fread.read())
fread.close()
fwrite.close()
posIMGs += 1
if __name__ == '__main__':
test = localImages()
test.getRandomImages()
test.downloadImages()
print 'Finito...'
import os
import random
import re
import urllib
import urllib2
class yahooImages(object):
RE_IMAGEURL = re.compile('&imgurl=(.+?)&', re.DOTALL | re.IGNORECASE)
def __init__(self):
self.imagesURLs = {}
def getRandomImages(self, imageName=None):
'''
imageName = Nome dell'immagine da cercare, se non impostato viene generato un nome Random
Scarica dal sito YahooImages delle immagini in maniera random...
'''
htmlPage = ''
request = ''
if imageName == None: imageName = self._randomWords()
requestURL = 'http://it.search.yahoo.com/search/images?p=%s&b=%d' % (imageName, (random.randint(0, 50)*10))
requestHeaders = {'User-Agent':'yahooImages/1.0'}
try:
request = urllib2.Request(requestURL, None, requestHeaders)
htmlPage = urllib2.urlopen(request).read(500000)
except:
pass
results = yahooImages.RE_IMAGEURL.findall(htmlPage)
if len(results) > 0:
for image in results:
imageURL = urllib.unquote_plus(image)
if not imageURL.startswith('http://'): imageURL = 'http://'+imageURL
self.imagesURLs[imageURL] = 0
def _randomWords(self):
'''
Viene generata una parola in maniera Random...
'''
words = ''
charset = 'abcdefghijklmnopqrtuvwxyz'*2 + '0123456789'
for i in range(random.randint(2, 7)): words += random.choice(charset)
return words
def downloadImages(self):
'''
Scarica nella cartella yahooIMGs le foto che vengono trovate in rete...
'''
numberIMGs = len(self.imagesURLs)
posIMGs = 1
for imageName in self.imagesURLs:
print '[' + str(posIMGs) + '/' + str(numberIMGs) + '] - ' + imageName
urllib.urlretrieve(imageName, 'yahooIMGs' + os.sep + os.path.split(imageName)[1])
posIMGs += 1
if __name__ == '__main__':
test = yahooImages()
test.getRandomImages()
test.downloadImages()
print 'Finito...'