Posted By

god_of_nothing on 03/07/11


Tagged

scripting


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

saman


music launcher


 / Published in: Python
 

with a given directory address, looks for musics in the directory and it's subdirectories and ask for launching them

  1. # BY: AMIR NAGHAVI
  2. """
  3. this program is made for playing music tracks in a directory and it's subdirectories
  4. with user permission.
  5. it works roughly random but not at whole.
  6. you can modify it's suflst list to play and launch any program with permitted suffix you want.
  7. """
  8.  
  9. import os
  10. from re import findall
  11. from random import randint
  12. # -------------------------------------------
  13. class shufle_launch(object):
  14. def __init__(self,add='C:\Music'):
  15. "add is directory path, self.c is counter look at run(),and lst is for temporary songs go to run() and play()"
  16. self.add=add #address
  17. self.c=0
  18. self.lst=[] #for collecting music paths
  19. # -------------------------------------------
  20. def search(self,add,music): # to search in musics add:directory path,music:search key
  21. for item in os.listdir(add):
  22. path=os.path.join(add,item)
  23. if os.path.isfile(path):
  24. ppath=path.split('\\') #in windows, python separates paths with"\\"
  25. if path.find(music)!=-1:
  26. print 'do u want to play %s'%(ppath[-2],ppath[-1])
  27. f=raw_input('')
  28. if f=='y':
  29. os.startfile(path)
  30.  
  31.  
  32. # ---------------------------------------------------- # to undrstand easier
  33. def play(self,l): # l is the list of music paths # first review run() then play()
  34. while True:
  35. pnum=randint(0,len(l)) # play number
  36. try: # ------------- try/except block----
  37. print l[pnum]
  38. a=raw_input('y for play or q for quit or to search enter s: ')
  39. if a=='s':
  40. m=raw_input('enter the music name: ')
  41. self.search(self.add,m)
  42. if a=='q':
  43. print 'return'
  44. exit()
  45. if a=='y':
  46. os.startfile(l[pnum])
  47. print 'mow playing',os.path.basename(l[pnum])
  48. l.pop(pnum)
  49. raw_input('')
  50. else:
  51. l.pop(pnum)
  52. except: # -------------- try/except block----
  53. print "can't play",os.path.basename(l[pnum])
  54. l.pop(pnum)
  55. pass
  56. if len(l)==0:
  57. return
  58. # -------------------------------------------------------------------------
  59. def run(self): # literate through file paths in a directory and it's subdirectories
  60. for item in os.listdir(self.add):
  61. path=os.path.join(self.add,item) #making absolute path
  62. suflst=['mp3','wav','aac','flac','wma','ogg'] # for playing just music- you can add your suffix to it
  63. try:
  64. suf=findall('.*[.]{1}([a-zA-Z0-9]{2,4})',path)[0] #extracting suffix such as MP3
  65. except:
  66. pass
  67. try:
  68. if os.path.isfile(path) and len(suf)>0 and (suf.lower() in suflst):
  69. self.c+=1
  70. self.lst.append(path)
  71. if self.c%30==0: # every times that self.lst has 30 member we wood run play fun()^^^
  72. self.play(self.lst) #and make lst emty.^^^
  73. self.lst=[]
  74. except:
  75. pass
  76. if os.path.isdir(path): # going through subdirectories^^^
  77. shufle_launch(path).run()
  78.  
  79.  
  80. # ------>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  81. #END#
  82. # ------>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  83. if __name__=="__main__":
  84. shufle_launch('\Music').run()

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: god_of_nothing on March 7, 2011

if possible please give your suggests and opinions about my code a bit abstraction and complexity is in code is for practicing my skills in programming. merci!

You need to login to post a comment.