6
\$\begingroup\$

I have my movie folders formatted:

  1. ~: no sub included

  2. {}: already watched

  3. nothing: sub included, ready to watch

the program asks for the movie directory; extracts the name and searches IMDb for the name; renames the given directory to the first IMDb search result.

finally saves what you'd see when the first result is clicked as a .html file in the given directory. Example

import urllib
import requests
import os
import re
from bs4 import BeautifulSoup

# write response page in tmp.html
def movNameSearch(path, movie):
    print('Fetching...\n')
    url = "https://www.imdb.com/find?ref_=nv_sr_fn&q=" + movie + "&s=tt"
    response = requests.get(url) #get response
    resTxt = response.text
    soup = BeautifulSoup(resTxt, 'html.parser') #parsed response
    findList = soup.find_all('td', class_ = 'result_text') #all founded movies
    someList = []
    for i in range(10):
        theRes = findList[i].contents[1].contents[0] + findList[i].contents[2]
        someList.append(theRes)

    for j in range(len(someList)):
        print(str(j+1) + '. ' + someList[j])

    selection = int(input('\nWhich is the desired result? '))
    rec = findList[selection-1].contents #first found result - recommended
    recText = rec[1].contents[0] #movie name
    fullName = recText + rec[2] #movie year
    suggestion = 'Confirm ' + fullName + '? (y/n) '
    accept = input(suggestion)
    if accept == 'y':
        print('Renaming the folder...')
        #rename folder
        pathArray = path.split(sep='/')
        if pathArray[-1] == '': # / at the end
            pathArray.pop()
            pathArray.pop()
        else: #no / at the end
            pathArray.pop()
        pathArray.append(fullName)
        src = path
        dst = '/'.join(pathArray)
        os.rename(src, dst)
        print('Renamed to ' + fullName)

        #generate info into info.txt
        imdbUrl = 'https://www.imdb.com'
        toAdd = list(rec[1].parent.contents[1].attrs.values())[0]
        imdbUrl = imdbUrl + toAdd
        result = [imdbUrl, dst]
        return result
    else:
        print('K bye')


def getInfo(url, path):
    print('Fetching extra info...')
    response = requests.get(url)
    resTxt = response.text
    soup = BeautifulSoup(resTxt, 'html.parser')
    print('Generating extra info...')
    f = open(path + '/info.html', 'w+')
    f.write(resTxt)
    print('Complete.')
    return True

#/home/name/some/~{}folder
# open directory =>
# get path 
mainPath = str(input("Enter movie directory: "))
movName = re.split("/", mainPath)
if movName[-1] == '':
    movName = movName[-2]
else:
    movName = movName[-1]

# remove extra chars
movName = re.sub("[~{}]", "", movName).capitalize()


# check
accept = input("Confirm " + movName + " ? (y/n) ")
if accept == "y":
    result = movNameSearch(mainPath, movName)
    if len(result) == 2:
        stat = getInfo(result[0], result[1])
        if stat == True:
            print('Success! Quitting now.')
    else:
        print('K Bye')

else:
    print("K bye")
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.