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):
    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]

    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
        else: #no / at the end
        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
        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+')
    return True

# open directory =>
# get path 
mainPath = str(input("Enter movie directory: "))
movName = re.split("/", mainPath)
if movName[-1] == '':
    movName = movName[-2]
    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.')
        print('K Bye')

    print("K bye")
  • 3
    \$\begingroup\$ I've edited your title. You don't need to specify the programming language as it already appears in the post's tags :) Apart from that, this seems like a great first post, the only thing I'd consider is to add maybe an example of what your program does, it would make the post even better. \$\endgroup\$ – IEatBagels May 7 at 17:17
  • \$\begingroup\$ @IEatBagels I appreciate it :D. Edited and added an example. \$\endgroup\$ – Nitwit May 7 at 23:54

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.