我可以翻译这句话:”how can i know how many episode of a movie do i have in my file”。

huangapple go评论67阅读模式
英文:

how can i know how many episode of a movie do i have in my file

问题

I wanna know how many episodes of a movie I have in my file. The name of episodes are something like this: "name.S01E01.720p.WEB-DL.mkv", "name.S01E02..." I think I should use something like this, but I don't know what's next.

import os
with os.scandir(path) as files:
    for file in files:
        ...

But every movie has a specific name (for example) "name.Joint.Economic.Area.S01E01.720p.NF.WEBRip.x264-GalaxyTV" or something else, and I wanna write a code that would work with any kind of names. I think the way that I'm doing this is not a professional way to do it.

I used this code:

import os
with os.scandir(path) as files:
    for file in files:
        num = 01
        if any(str(num) in file.name):
            print("yes")

But I got an error.

英文:

i wanna know how many episode of a movie i have in my file.
the name of episodes are something like this:
"name.S01E01.720p.WEB-DL.mkv", "name.S01E02..."
i think i should use something like this but idk what's next

import os
with os.scandir(path) as files:
    for file in files:
    ...

but every movie has a specific name (for example)"name.Joint.Economic.Area.S01E01.720p.NF.WEBRip.x264-GalaxyTV" or some thing else and i wanna write a code that would work with any kind of names

i think the way that im doing this is not a professional way to do it.

i used this one:

import os
with os.scandir(path) as files:
    for file in files:
        num = 01
        if any(str(num) in file.name):
            print("yes")

but i got an error.

答案1

得分: 0

以下是您要翻译的代码部分:

#!/usr/bin/env python3
import glob
import os,sys
import re
import json
try:
  movie_path_orig = sys.argv[1]
except:
  print(f"使用 ./{sys.argv[0]} <电影路径>")
  sys.exit(0)

movie_path = os.path.join(movie_path_orig, "*.mkv")

print(f"处理 {movie_path}..")
files = glob.glob(movie_path)

summary = {}
for f in files:
  r=re.search("^(.*)S([0-9]{2})E([0-9]{2})(.*)", f)
  prefix = r.group(1)
  s = int(r.group(2))  # 季节
  e = int(r.group(3))  # 集数
  suffix = r.group(4)
  if prefix not in summary.keys():
    summary[prefix] = { 'seasons': { f"{s}": e }, 'prefix': prefix, 'suffix': suffix}
  else:
    if f"{s}" not in summary[prefix]['seasons'].keys():
       summary[prefix]['seasons'][f"{s}"] = e
    else:
       summary[prefix]['seasons'][f"{s}"] = max(summary[prefix]['seasons'][f"{s}"], e)

print(json.dumps(summary, sort_keys=True, indent=4))

for i in summary.keys():
  for season in summary[i]['seasons'].keys():
    for episode in range(1, summary[i]['seasons'][season]+1):
      s = "{:02d}".format(int(season))
      e = "{:02d}".format(episode)
      file2chk = f"{summary[i]['prefix']}S{s}E{e}{summary[i]['suffix']}"
      if not os.path.exists(file2chk):
          print(f"缺失 {file2chk}")
英文:

maybe this can help:

#!/usr/bin/env python3
import glob
import os,sys
import re
import json
try:
  movie_path_orig = sys.argv[1]
except:
  print(f&quot;use ./{sys.argv[0]} &lt;movie path&gt;&quot;)
  sys.exit(0)

movie_path = os.path.join(movie_path_orig, &quot;*.mkv&quot;)

print(f&quot;Processing {movie_path}..&quot;)
files = glob.glob(movie_path)

summary = {}
for f in files:
  r=re.search(&quot;^(.*)S([0-9]{2})E([0-9]{2})(.*)&quot;, f)
  prefix = r.group(1)
  s = int(r.group(2)) #season
  e = int(r.group(3)) #episode
  suffix = r.group(4)
  if prefix not in summary.keys():
    summary[prefix] = { &#39;seasons&#39;: { f&quot;{s}&quot;: e }, &#39;prefix&#39;: prefix, &#39;suffix&#39;: suffix}
  else:
    if f&quot;{s}&quot; not in summary[prefix][&#39;seasons&#39;].keys():
       summary[prefix][&#39;seasons&#39;][f&quot;{s}&quot;] = e
    else:
       summary[prefix][&#39;seasons&#39;][f&quot;{s}&quot;] = max(summary[prefix][&#39;seasons&#39;][f&quot;{s}&quot;], e)

print(json.dumps(summary, sort_keys=True, indent=4))

for i in summary.keys():
  for season in summary[i][&#39;seasons&#39;].keys():
    for episode in range(1, summary[i][&#39;seasons&#39;][season]+1):
      s = &quot;{:02d}&quot;.format(int(season))
      e = &quot;{:02d}&quot;.format(episode)
      file2chk = f&quot;{summary[i][&#39;prefix&#39;]}S{s}E{e}{summary[i][&#39;suffix&#39;]}&quot;
      if not os.path.exists(file2chk):
          print(f&quot;missing {file2chk}&quot;)

first, you need to know the last episode of each season for each movie (first for), then check for everything starting from episode 01 (second for). Just make sure you have the last episode.

for a movie dir with:

name.Joint.Economic.Area.S01E01.720p.NF.WEBRip.x264-GalaxyTV.mkv
name.Joint.Economic.Area.S02E03.720p.NF.WEBRip.x264-GalaxyTV.mkv

it outputs:

Processing movies/*.mkv..
{
&quot;movies/name.Joint.Economic.Area.&quot;: {
&quot;prefix&quot;: &quot;movies/name.Joint.Economic.Area.&quot;,
&quot;seasons&quot;: {
&quot;1&quot;: 1,
&quot;2&quot;: 3
},
&quot;suffix&quot;: &quot;.720p.NF.WEBRip.x264-GalaxyTV.mkv&quot;
}
}
missing movies/name.Joint.Economic.Area.S02E01.720p.NF.WEBRip.x264-GalaxyTV.mkv
missing movies/name.Joint.Economic.Area.S02E02.720p.NF.WEBRip.x264-GalaxyTV.mkv

huangapple
  • 本文由 发表于 2023年3月21日 02:51:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定