Your Ad Here

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