Skip to content

Instantly share code, notes, and snippets.

@Himan10
Last active November 26, 2020 18:07
Show Gist options
  • Select an option

  • Save Himan10/72a6764a174cef67b3d892a1c3bfee77 to your computer and use it in GitHub Desktop.

Select an option

Save Himan10/72a6764a174cef67b3d892a1c3bfee77 to your computer and use it in GitHub Desktop.
Find songs path that are stored in local directory
Finding tests in NoName
Trying:
main('My December')
Expecting:
/home/hi-man/Music/LinkinPark/Linkin Park - My December. Live Projekt Revolution 2002 Las Vegas._480p.mp4
/home/hi-man/Music/LinkinPark/my_december.mp4
ok
Trying:
main('turn-the page-metallica')
Expecting:
/home/hi-man/Music/LinkinPark/Metallica Turn the Page (Official Music Video)_qPOTEs_yTJo_360p.mp4
ok
Trying:
main('crawling linkin park')
Expecting:
/home/hi-man/Music/LinkinPark/Crawling (Official Video) - Linkin Park.mp4
/home/hi-man/Music/LinkinPark/Linkin Park ft Chris Cornell - Crawling_HHH.mp4
/home/hi-man/Music/LinkinPark/Linkin Park _ Under Attack (Crawling Demo).mp4
/home/hi-man/Music/LinkinPark/Linkin Park - Crawling_Where'd You Go (Ghost in the Machine Remix).mp4
ok
Trying:
main('beautiful now zedd')
Expecting:
/home/hi-man/Music/Zedd - Beautiful Now (Official Music Video) ft. Jon Bellion.mp4
ok
Trying:
main('snuff SLIPKNOT')
Expecting:
/home/hi-man/Music/LinkinPark/Slipknot - Snuff [OFFICIAL VIDEO]_LXEKuttVRIo_360p.mp4
ok
Trying:
main('--Interstellar Theme-- Hans Zimmer')
Expecting:
/home/hi-man/Music/Hans Zimmer - Interstellar Theme (Live in Prague).mp4
ok
import os
import re
import doctest
import cProfile
from functools import partial
orignalSongNames = []
def _remove_punctuations(songname):
songname = re.sub(r"[\s\W_]+", "", songname)
return songname.lower()
def yieldOrignalNames(path):
""" gonna use a generator function """
for dir_, subdir, files in os.walk(path, topdown=True):
yield dir_, files
def SearchLocal(user_input: str, path, AlreadySearched: dict):
""" Search songs in local directory """
if path in AlreadySearched:
return AlreadySearched[path]
global orignalSongNames
genObj = yieldOrignalNames(path)
path, orignalSongNames = next(genObj)
# Pause the generator at line 16
# until we send something to it or run next()
temp = ", ".join(
str(i) + "-" + _remove_punctuations(orignalSongNames[i])
for i in range(0, len(orignalSongNames))
)
# Find the songs
user_input = re.sub(r'[^\s\w]', ' ', user_input).split()
match = 0
n = len(user_input)
i = 0
while i < n and match < n:
pattern = r"[^,]*{0}[^,]*(?:mp[3|4]|opus|mkv|webm)".format(user_input[i])
found = re.findall(pattern, temp, re.I) # returns a list
#print(found, path)
if found.__sizeof__() > 40: # not an empty list
i += 1
match += 1
temp = ", ".join(found)
if match > n:
genObj.close()
break
else:
try:
path, orignalSongNames = next(genObj) # Call for path of another dir.
success, found, path = SearchLocal(' '.join(user_input), path, AlreadySearched) # Recursive call
AlreadySearched[path] = success, found, path
#print(success, path)
if success:
genObj.close()
break
except Exception as err:
# Possible condition : We've iterated over all the paths but couldn't find any
return False, None, path
AlreadySearched[path] = True, found, path
return True, found, path
def test():
"""
>>> main('My December')
/home/hi-man/Music/LinkinPark/Linkin Park - My December. Live Projekt Revolution 2002 Las Vegas._480p.mp4
/home/hi-man/Music/LinkinPark/my_december.mp4
>>> main('turn-the page-metallica')
/home/hi-man/Music/LinkinPark/Metallica Turn the Page (Official Music Video)_qPOTEs_yTJo_360p.mp4
>>> main('crawling linkin park')
/home/hi-man/Music/LinkinPark/Crawling (Official Video) - Linkin Park.mp4
/home/hi-man/Music/LinkinPark/Linkin Park ft Chris Cornell - Crawling_HHH.mp4
/home/hi-man/Music/LinkinPark/Linkin Park _ Under Attack (Crawling Demo).mp4
/home/hi-man/Music/LinkinPark/Linkin Park - Crawling_Where'd You Go (Ghost in the Machine Remix).mp4
>>> main('beautiful now zedd')
/home/hi-man/Music/Zedd - Beautiful Now (Official Music Video) ft. Jon Bellion.mp4
>>> main('snuff SLIPKNOT')
/home/hi-man/Music/LinkinPark/Slipknot - Snuff [OFFICIAL VIDEO]_LXEKuttVRIo_360p.mp4
>>> main('--Interstellar Theme-- Hans Zimmer')
/home/hi-man/Music/Hans Zimmer - Interstellar Theme (Live in Prague).mp4
"""
def main(user_input: str):
# Create a partial argument with limited args.
AS = {}
SearchLocalT = partial(SearchLocal, AlreadySearched = AS)
path = '/home/hi-man/Music'
result = SearchLocalT(user_input, path)
if result[0]:
for i in result[1]:
print(os.path.join(result[2], orignalSongNames[int(i.lstrip().split('-')[0])]))
else:
return False
if __name__ == "__main__":
pass
doctest.run_docstring_examples(test, globals(), verbose=True)
@Himan10
Copy link
Copy Markdown
Author

Himan10 commented Nov 26, 2020

Added recursion technique to search for the songs in different directories, and if nothing is found in the first directory returned by yieldOrignalNames then it will make a second call to the generator function which returns another directory path.
Now, the same computation will be done for this path too. if the song name is found then simply checks for each piece of user input that is split into pieces by using the .split() method, and once you get the final song name that contains all the words or at least contain more than half of the words of user input (split one) then it simply breaks the while loop, otherwise it again makes a call to generator function, ask for another directory path and do the same.

One problem still exists i.e., the function does the computation for the path that is already been checked.. (needs a memorization technique here)

@Himan10
Copy link
Copy Markdown
Author

Himan10 commented Nov 26, 2020

Fixed the problem :) it now takes only 2 recursive calls to search a song (only case, if the song is not present in all the 3 directories) because I've only two sub-directories inside the main directory...
If a song present in the first sub-directory then it will take only 1 recursive call = main(not found) -> sub-directory(found)
If a song present in the main directory then no recursive call = main(found)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment