Listing movies for torrenting Can I get some help on shortening the amount of code I have in this program?

import requests
import gzip
import sys
import urllib
from tabulate import tabulate
from bs4 import BeautifulSoup
from io import BytesIO


# firstly it will display a list of movies, the user will then type in the torrent they want to download



# download file
def get_file(href, file_name):
    try:
        req = requests.get('https://examplewebsite.com' + href)
    except Exception as e:
        print (e)

    response = req.text
    soup = BeautifulSoup(response)
    download = soup.find('a', {'class': 'siteButton giantButton    verifTorrentButton'})

    file_name = file_name + '.torrent'

    # this encodes the response information in a file

    response = urllib.urlopen(download.get('href'))
    buffer = BytesIO(response.read())

    gz = gzip.GzipFile(fileobj=buffer)

    with open(file_name,'wb') as f:
        f.truncate()
        f.write(gz.read())



def select_torrent():
    torrent = raw_input('&gt;&gt;')
    if torrent =='q' or torrent =='Q':
        sys.exit(0)
    else:
        return torrent
def test(text):
    if text=='q' or text=='Q':
        sys.exit(0)


# generates a list of movies
def find_movies(pages):
    total_movie_list = {}
    total_amount_of_movies = []
    total_movie_number = 0

    for i in xrange(pages):
        try:
            req = requests.get('https://example.website/movies/'+ str(i))
        except Exception as e:
            print (e)
        else:

            response = req.text
            soup = BeautifulSoup(response)

            #find elements in html file
            movies = soup.findAll('a', {'class': 'cellMainLink'})
            seed = soup.findAll('td',{'class':'green center'})
            leech = soup.findAll('td',{'class':'red lasttd center'})

            #put seed and leech data into list
            seed_list = [number.string for number in seed]
            leech_list = [number.string for number in leech]


            # generate dictionary of movies with their movie name and seed/leech data
            movie_temp = {i+total_movie_number: (movies[i].get('href'), movies[i].string,seed_list[i],leech_list[i]) for i in range(len(movies))}
            #put data into a table

            table = [[key,value[1],value[2],value[3]]for key,value in movie_temp.items()]
            #add onto current number of movies
            total_amount_of_movies += table
            #update movie list in order to download them
            total_movie_list.update(movie_temp)

            # add on to the last movie  number
            current_number_of_movies = len(movies)
            total_movie_number +=current_number_of_movies

    print(tabulate(total_amount_of_movies,headers=['number','name','seeder','leecher']))

    return total_movie_list



if __name__ == '__main__':
    #find movies, get input from user, then download movie torrent
    pages = raw_input('how many pages do you want(press q to quit)?:')
    test(pages)
    movie_list = find_movies(int(pages))
    print('\ndownload a file by typing the number(press q to quit)\n')
    torrent  = select_torrent()
    test(torrent)

    get_file(movie_list[int(torrent)][0], movie_list[int(torrent)][1])
Shortening


  Can I get some help on shortening the amount of code I have in this program?


Shortening shouldn't be a primary concern. But ok let's go.



In get_file,
if an error occurs while downloading the page,
you print the exception, and then execution continues,
and the program will crash on the next line that tries to access the downloaded response.
You could just as well remove the try-except,
the result will be actually better,
because that way the crash will show the real problem (download error).

The same argument goes for the try-except in find_movies too.



You use two libraries to download data: requests and urllib.
You only need either of those, so you can get rid of one import.



file_name = file_name + '.torrent' can be shortened to file_name += '.torrent'



Instead of this:


torrent = select_torrent()
test(torrent)



You can delete the select_torrent function and write this:

torrent = raw_input('&gt;&gt;')
test(torrent)


Making it better

Follow PEP8, the Python coding style guide.

test is a very poor name for what it does.

Some validation for the selected torrent would be good.