curses-subs.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*
  3. from curses import wrapper
  4. import curses
  5. import subprocess
  6. status = "nominal"
  7. class Entry:
  8. def __init__(self, t, k="sth"):
  9. self.text = t
  10. self.key = k
  11. def show_list(stdscr, l, selected=-1):
  12. global status
  13. maxline = curses.LINES - 2
  14. offset = 2
  15. stdscr.clear()
  16. start = max(0, selected - (maxline - offset))
  17. stdscr.addstr(0, 0, status, curses.A_REVERSE)
  18. j = 0
  19. for i in range(start, len(l)):
  20. e = l[i]
  21. stdscr.addstr(
  22. j + offset,
  23. 0,
  24. e.key + ": " + e.text,
  25. curses.A_REVERSE if i == selected else curses.A_BOLD,
  26. )
  27. j += 1
  28. if j + offset > maxline:
  29. break
  30. stdscr.refresh()
  31. def find_name(keystring):
  32. p = subprocess.Popen(
  33. 'yt-dlp --get-title -s "ytsearch:' + keystring + '"',
  34. shell=True,
  35. stdout=subprocess.PIPE,
  36. stderr=subprocess.STDOUT,
  37. )
  38. res = ""
  39. lines = p.stdout.readlines()
  40. retval = p.wait()
  41. return lines[-1].decode("utf-8")
  42. def download(sub):
  43. p = subprocess.Popen(
  44. 'yt-dlp "ytsearch:' + sub.key + '"',
  45. shell=True,
  46. stdout=subprocess.PIPE,
  47. stderr=subprocess.STDOUT,
  48. )
  49. # res = ''
  50. # for line in p.stdout.readlines():
  51. # res+=line.decode('utf-8').strip('\n')
  52. retval = p.wait()
  53. return
  54. def main(stdscr):
  55. global status
  56. # Clear screen
  57. stdscr.clear()
  58. selected = 2
  59. file = open("subs")
  60. text = file.read()
  61. subs = text.split("\n")
  62. sublist = [Entry("", x) for x in subs if x != ""]
  63. show_list(stdscr, sublist)
  64. for s in sublist:
  65. s.text = find_name(s.key)
  66. show_list(stdscr, sublist, sublist.index(s))
  67. while True:
  68. show_list(stdscr, sublist, selected)
  69. ch = stdscr.getch()
  70. if ch == curses.KEY_DOWN:
  71. selected += 1
  72. elif ch == curses.KEY_UP:
  73. selected -= 1
  74. elif ch == 10: # enter
  75. status = "Downloading"
  76. show_list(stdscr, sublist, selected)
  77. download(sublist[selected])
  78. status = "nominal"
  79. show_list(stdscr, sublist, selected)
  80. else:
  81. status = str(ch)
  82. # looping
  83. if selected == len(sublist):
  84. selected = 0
  85. elif selected == -1:
  86. selected = len(sublist) - 1
  87. wrapper(main)