issue71:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue71:python [2013/05/28 21:20] – [9] fredphil91 | issue71:python [2013/06/07 14:32] (Version actuelle) – [15] auntiee | ||
---|---|---|---|
Ligne 8: | Ligne 8: | ||
Tv.Show.Name.SxxExx.Episode name here if you care.extension** | Tv.Show.Name.SxxExx.Episode name here if you care.extension** | ||
- | Supposons que vous avez décidé de créer un centre multimédia pour votre salle de séjour. Vous avez un ordinateur dédié à l' | + | Supposons que vous avez décidé de créer un centre multimédia pour votre salle de séjour. Vous avez un ordinateur dédié à l' |
- | C'est le projet que nous allons commencer ce mois-ci. Notre première tâche consiste à fouiller dans le dossier contenant vos émissions de télévision, | + | C'est le projet que nous allons commencer ce mois-ci. Notre première tâche consiste à fouiller dans le dossier contenant vos émissions de télévision, |
D' | D' | ||
Ligne 60: | Ligne 60: | ||
and so on. Now that we know what we will be looking for and where it will be, let's move on.** | and so on. Now that we know what we will be looking for and where it will be, let's move on.** | ||
- | La structure | + | La structure |
émissions | émissions | ||
Ligne 88: | Ligne 88: | ||
Which are very common extensions for video files in the media PC world.** | Which are very common extensions for video files in the media PC world.** | ||
- | Il y a très longtemps, nous avons créé un programme pour constituer une base de données contenant nos fichiers MP3. C' | + | Il y a très longtemps, nous avons créé un programme pour constituer une base de données contenant nos fichiers MP3. C' |
.avi | .avi | ||
Ligne 95: | Ligne 95: | ||
.mp4 | .mp4 | ||
- | Ce sont des extensions très communes | + | Ce sont des extensions très courantes |
====== 5 ====== | ====== 5 ====== | ||
Ligne 110: | Ligne 110: | ||
As you can see, we are importing the os, sys and apsw libraries. We've used them all before. We are also importing the re library to support Regular Expressions. We'll touch on that quickly this time, but more in the next article.** | As you can see, we are importing the os, sys and apsw libraries. We've used them all before. We are also importing the re library to support Regular Expressions. We'll touch on that quickly this time, but more in the next article.** | ||
- | Nous allons maintenant commencer avec la première partie de notre projet. Créez un fichier appelé " | + | Nous allons maintenant commencer avec la première partie de notre projet. Créez un fichier appelé " |
Commençons avec nos importations : | Commençons avec nos importations : | ||
Ligne 120: | Ligne 120: | ||
import re | import re | ||
- | Comme vous pouvez le voir, nous importons les bibliothèques os, sys et apsw. Nous les avons toutes déjà utilisées | + | Comme vous pouvez le voir, nous importons les bibliothèques os, sys et apsw. Nous les avons toutes déjà utilisées. Nous importons aussi la bibliothèque re pour le support des expressions régulières. Nous allons en parler rapidement cette fois-ci, nous approfondirons dans le prochain article. |
====== 6 ====== | ====== 6 ====== | ||
Ligne 145: | Ligne 145: | ||
Now all the dull stuff is done, so we can move on the the meat and potatoes of our project. We'll start with the MakeDataBase routine (middle right). Put it right after the imports.** | Now all the dull stuff is done, so we can move on the the meat and potatoes of our project. We'll start with the MakeDataBase routine (middle right). Put it right after the imports.** | ||
- | Mes fichiers TV sont situés | + | Mes fichiers TV se trouvent |
dossierDepart = "/ | dossierDepart = "/ | ||
Ligne 167: | Ligne 167: | ||
for file in [f for f in files if f.endswith < | for file in [f for f in files if f.endswith < | ||
- | Nous avons déjà discuté de cette routine lorsque nous avons traité le scanner MP3, donc je vais juste vous rappeler que, dans cette routine, nous vérifions pour voir si la table existe, et sinon nous la créons. | + | Nous avons déjà discuté de cette routine lorsque nous avons traité le scanner MP3, donc je vais juste vous rappeler que, dans cette routine, nous vérifions pour voir si la table existe et, sinon, nous la créons. |
Maintenant, nous allons créer la routine ParcourirChemin (à droite, deuxième à partir du bas). | Maintenant, nous allons créer la routine ParcourirChemin (à droite, deuxième à partir du bas). | ||
- | Lorsque nous entrons dans la routine (comme nous l' | + | Lorsque nous entrons dans la routine (comme nous l' |
for fic in [f for f in fichiers if f.endswith < | for fic in [f for f in fichiers if f.endswith < | ||
Ligne 187: | Ligne 187: | ||
Maintenant, nous découpons le nom de fichier en séparant l' | Maintenant, nous découpons le nom de fichier en séparant l' | ||
- | ONomFicOriginal,ext = os.path.splitext(fic) | + | NomFicOriginal,ext = os.path.splitext(fic) |
fl = fic | fl = fic | ||
Ligne 196: | Ligne 196: | ||
====== 10 ====== | ====== 10 ====== | ||
- | Next (middle | + | **Next (middle |
- | If everything works as it should, the response from the query should only be a 1 or a 0. If it's a 0, then it's not there, and we will write the information to the database. If it is, we just move past. Notice the Try Except commands above and below. If something goes wrong, like some character that the database doesn' | + | If everything works as it should, the response from the query should only be a 1 or a 0. If it's a 0, then it's not there, and we will write the information to the database. If it is, we just move past. Notice the Try Except commands above and below. If something goes wrong, like some character that the database doesn' |
+ | |||
+ | Ensuite (au milieu à droite), nous vérifions si le fichier est dans la base de données. Si c'est le cas, il ne faut pas le dupliquer. Nous vérifions simplement le nom du fichier. Nous pourrions aller plus loin et vérifier que le chemin est aussi le même, mais pour cette fois, c'est assez. | ||
+ | |||
+ | Si tout fonctionne correctement, | ||
====== 11 ====== | ====== 11 ====== | ||
- | We are simply inserting a new record into the database or writing to the error file. | + | **We are simply inserting a new record into the database or writing to the error file. |
# Close the log file | # Close the log file | ||
Ligne 212: | Ligne 216: | ||
def GetSeasonEpisode(filename): | def GetSeasonEpisode(filename): | ||
filename = filename.upper() | filename = filename.upper() | ||
- | resp = re.search(r' | + | resp = re.search(r' |
+ | |||
+ | Nous insérons simplement un nouvel enregistrement dans la base de données ou écrivons dans le fichier d' | ||
+ | |||
+ | # ferme le fichier de log | ||
+ | ficerr.close | ||
+ | # Fin de ParcourirChemin | ||
+ | |||
+ | Maintenant, regardons la routine RecupereSaisonEpisode. | ||
+ | |||
+ | # ========================================= | ||
+ | def RecupereSaisonEpisode(nomfic): | ||
+ | nomfic = nomfic.upper() | ||
+ | resp = re.search(r' | ||
====== 12 ====== | ====== 12 ====== | ||
- | The re.search portion of the code is part of the re library. It uses a pattern string, and, in this case, the filename that we want to parse. The re.M|re.I are parameters that say that we want to use a multiline type search (re.M) combined with an ignore-case (re.I). As I said earlier, we'll deal with the regular expressions more next month, since our routine will match only one type of series|episode string. As for the search pattern we are looking for: " | + | **The re.search portion of the code is part of the re library. It uses a pattern string, and, in this case, the filename that we want to parse. The re.M|re.I are parameters that say that we want to use a multiline type search (re.M) combined with an ignore-case (re.I). As I said earlier, we'll deal with the regular expressions more next month, since our routine will match only one type of series|episode string. As for the search pattern we are looking for: " |
Continuing on, the search returns a match object that we can look at. " | Continuing on, the search returns a match object that we can look at. " | ||
if resp: | if resp: | ||
- | showname = resp.group(1) | + | showname = resp.group(1)** |
+ | |||
+ | La partie re.search du code vient de la bibliothèque re. Elle utilise un modèle de chaîne et, dans ce cas, le nom du fichier que l'on veut analyser. re.M|re.I sont des paramètres qui disent que nous voulons utiliser une recherche de type multiligne (re.M) indépendante de la casse (re.I). Comme je l'ai dit précédemment, | ||
+ | |||
+ | Ensuite, la recherche retourne un objet correspondant que nous pouvons regarder. « rep » est une réponse qui est vide si aucune correspondance n'est trouvée, et, dans ce cas, deux morceaux d' | ||
+ | |||
+ | si rep : | ||
+ | nomEmission = rep.group(1) | ||
====== 13 ====== | ====== 13 ====== | ||
- | We take the show name from group number one. Then we get the length of that so we can grab the series and episode string with a substring command. | + | **We take the show name from group number one. Then we get the length of that so we can grab the series and episode string with a substring command. |
shownamelength = len(showname) + 1 | shownamelength = len(showname) + 1 | ||
Ligne 232: | Ligne 256: | ||
Next, we replace any periods in the showname with a space – to be more "Human Readable" | Next, we replace any periods in the showname with a space – to be more "Human Readable" | ||
- | showname = showname.replace(" | + | showname = showname.replace(" |
+ | |||
+ | Nous récupérons le nom de l' | ||
+ | |||
+ | longueurNomEmission = len(nomEmission) + 1 | ||
+ | se = nomfic[longueurNomEmission: | ||
+ | saison = se[1:3] | ||
+ | episode = se[4:6] | ||
+ | |||
+ | Ensuite, nous remplaçons tous les points de nomEmission par une espace, pour les rendre plus « lisibles par l' | ||
+ | |||
+ | nomEmission = nomEmission.replace(" | ||
====== 14 ====== | ====== 14 ====== | ||
- | We create a list to include the show name, season and episode, and return it along with the True boolean to say things went well. | + | **We create a list to include the show name, season and episode, and return it along with the True boolean to say things went well. |
ret = [showname, | ret = [showname, | ||
Ligne 241: | Ligne 276: | ||
Otherwise, if we didn't find a match, we create our list containing no show name and two " | Otherwise, if we didn't find a match, we create our list containing no show name and two " | ||
+ | |||
+ | else: | ||
+ | ret = ["", | ||
+ | return False,ret** | ||
+ | |||
+ | Nous créons une liste contenant le nom de l' | ||
+ | |||
+ | ret = [nomEmission, | ||
+ | return True,ret | ||
+ | |||
+ | Sinon, si nous n' | ||
else: | else: | ||
Ligne 247: | Ligne 293: | ||
====== 15 ====== | ====== 15 ====== | ||
- | That's all the code. Now let's see what the output would look like. Assuming your file structures are exactly like mine, some of the output on the screen would look like this... | + | **That's all the code. Now let's see what the output would look like. Assuming your file structures are exactly like mine, some of the output on the screen would look like this... |
Season 02 Episode 04 | Season 02 Episode 04 | ||
Ligne 265: | Ligne 311: | ||
Next time, we will deal with more Season|Episode formats, and do some other things to flesh out our program. | Next time, we will deal with more Season|Episode formats, and do some other things to flesh out our program. | ||
- | See you soon. | + | See you soon.** |
+ | |||
+ | Voilà tout le code. Maintenant, regardons à quoi le résultat devrait ressembler. En supposant que votre structure de fichier est exactement comme la mienne, une partie de l' | ||
+ | |||
+ | Saison 02 Episode 04 | ||
+ | SELECT count(pkid) as nbLignes from EmissionsTV where NomFichier = " | ||
+ | Serie - INSECURITY Fichier - InSecurity.S02E04.avi | ||
+ | Saison 01 Episode 08 | ||
+ | SELECT count(pkid) as nbLignes from EmissionsTV where NomFichier = " | ||
+ | Serie - PRIME SUSPECT US Fichier - Prime.Suspect.US.S01E08.Underwater.avi | ||
+ | |||
+ | et ainsi de suite. Vous pouvez raccourcir la sortie si vous voulez pour éviter que l' | ||
+ | |||
+ | pkID | Serie | Chemin Racine | ||
+ | 2526 | NCIS | / | ||
+ | |||
+ | Comme toujours, l' | ||
+ | La prochaine fois, nous traiterons un peu plus les formats de saison/ | ||
- | code en français | + | À bientôt. |
- | http:// | + | |
issue71/python.1369768827.txt.gz · Dernière modification : 2013/05/28 21:20 de fredphil91