Labels

Java (10) Spring (10) Spring MVC (6) Web Services (5) Rest (4) Javascript (3) Nodejs (3) Spring Batch (3) Angular (2) Angular2 (2) Angular6 (2) Expressjs (2) Passportjs (2) SOAP (2) SOAPUI (2) Spring Boot (2) AJAX (1) H2 (1) JQuery (1) JUnit (1) Npm (1) Puppeteer (1) Python (1) RaspberryPi (1) Raspbian (1) SQL (1) SQLite (1) Scripts (1) html (1)

Sunday, May 28, 2017

Automatically tag audio files using python

Problem


I have been listening to audiobooks lately. If you try to listen long audiobooks you will normally have to handle a lot of mp3 files in your phone or mp4 device. If these files are not well tagged, you will struggle jumping from one chapter to the next and you will end up mad.

One solution is to tag them manually by clicking one file after the next and changing one label after another (tittle, artist, track...). I solved this by automatically assigning the title as the filename using python. 

Changing audio tags using python

There are many python libraries out there that allow you to read and edit audio tags. The one that has given me better results is mutagen. To use it you will need to install it with


pip install mutagen


The following script takes all the files inside the folder where it its contained and sets their title tag as their respective filename (without extension)

Before changing the tags it renames the file so that it has a cleaner name (removing rare characters).


import os
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, TIT2

def clean_filename(filename):
 keepcharacters = (' ','.','-','_')
 return "".join(c for c in filename if c.isalnum() or c in keepcharacters).rstrip()


def rename_cleaning_filename(filename):
 os.rename(filename, clean_filename(filename))

def extension(filename):
 return '.' + filename.split('.')[len(filename.split('.'))-1]
 
def remove_extension(filename):
 return filename.split(extension(filename))[0]

for filename in os.listdir('.'):
  rename_cleaning_filename(filename)

music_folder = os.path.dirname(os.path.realpath(__file__))

for file in os.listdir(music_folder):
 if os.path.isfile(file): 
  filename = os.path.join(music_folder, file)
  if (extension(file) != '.py'):
   tags = ID3(filename)
   print('Changing title of ' + filename + ' to ' + remove_extension(file))
   tags["TIT2"] = TIT2(encoding=3, text = remove_extension(file))
   tags.save(filename)


If for you want to change other tags you can do it similarly, here you have more examples.


Tagging files across subfolders


If you want to do the same thing as before, but tagging all the files inside all the subfolders of a folder, you can use this python code instead:


import os
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, TIT2

def clean_filename(filename):
 keepcharacters = (' ','.','-','_')
 return "".join(c for c in filename if c.isalnum() or c in keepcharacters).rstrip()


def rename_cleaning_filename(filename):
 os.rename(filename, clean_filename(filename))

def extension(filename):
 return '.' + filename.split('.')[len(filename.split('.'))-1]
 
def remove_extension(filename):
 return filename.split(extension(filename))[0]

for filename in os.listdir('.'):
  rename_cleaning_filename(filename)

music_folder = os.path.dirname(os.path.realpath(__file__))

for root, subdirs, files in os.walk(music_folder):
 for file in files:
  filename = os.path.join(root, file)
  if (extension(file) != '.py'):
   tags = ID3(filename)
   print('Changing title of ' + filename + ' to ' + remove_extension(file))
   tags["TIT2"] = TIT2(encoding=3, text = remove_extension(file))
   tags.save(filename)


The main difference is that here we are using the line


for root, subdirs, files in os.walk(music_folder):

which goes inside subfolders too.

Sample




Download the code here:



Saturday, May 27, 2017

SoapUI mock web services tricks

In a previous article I explained how to create and deploy a mock web service using SOAPUI.

In the process of testing applications sometimes you need to do quick changes in a mock web service and deploy it shiftly.

XML project file in SoapUI

When you export a SoapUI project you will get an xml that contains all the information and configuration of your project. Furthermore, if your project contains a mock service with its responses, those will be inside that xml.

For instance I have created this simple project


I have added three very simple responses:



You can save it with Project --> Save project as




 Then, if you open that xml flile, you will see that at the end of it are the responses that I created above.


So If you change the data in those responses and import the project again you will see that the responses have changed accordingly.

This is a method that I found out for doing quick small changes across several SoapUI projects that I use frequently. 

Nevertheless I still have not figured out how tho add responses using only this xml file. I do not fully understand the headings of the responses.


Deploying a SoapUI mock web service as a war file using the command line.


So if you have saved your soapui project into a xml as we did above, you can create a .war from it using the command line. I have troubles finding information about this and I think it is a great trick.

This is the .bat file that I use:



set war_file=D:\hugo_documentos\mock2.war
set war_dir=D:\hugo_documentos
set xml_dir=D:\hugo_documentos\mock-example-soapui-project.xml
set soapui-settings=C:\Users\hugo\soapui-settings.xml

cd /d C:\Program Files (x86)\SmartBear\SoapUI-5.3.0\bin

wargenerator.bat -f %war_file% -d %war_dir% -w true -x true -s %soapui-settings% %xml_dir%

pause

The trick is to use an executable called wargenerator that you will find inside the bin folder of your SoapUI installation folder. You will have to give it as inputs the place where you want the war file, the war directory, the SoapUI settings xml and the xml of your project.

I hope this helps!