This is a small program to download random LOLCat pictures
The program consists of two pieces. Program.py and cat_service.py. The first contains the main functions and the latter contains the code related to downloading the LOLCats
The code for program.py:
__author__ = "byteme8bit"
import os
import cat_service
import subprocess
import platform
def main():
print_header()
folder = get_or_create_output_folder()
print('Found or created folder: ' + folder)
download_cats(folder)
display_cats(folder)
def print_header():
print('--------------------------------------')
print(' LOL CATS ')
print('--------------------------------------')
print()
def get_or_create_output_folder():
base_folder = os.path.dirname(__file__)
# print(base_folder)
folder = 'cats_pictures'
full_path = os.path.join(base_folder, folder)
print(full_path)
if not os.path.exists(full_path) or not os.path.isdir(full_path):
print('Creating new directory at {}'.format(full_path))
os.mkdir(full_path)
return full_path
def download_cats(folder):
print('Contacting server to download cats...')
cat_count = 8
for i in range(1, cat_count + 1):
name = 'lolcat_{}'.format(format(i))
print('Downloading cat ' + name)
cat_service.get_cat(folder, name)
print('Done.')
def display_cats(folder):
print('Displaying cats in OS window...')
if platform.system() == 'Darwin':
subprocess.call(['open', folder])
elif platform.system() == 'Windows':
subprocess.call(['explorer', folder])
elif platform.system() == 'Linux':
subprocess.call(['xdg-open', folder])
else:
print("We don't support your OS: " + platform.system())
if __name__ == '__main__':
main()
The code for cat_service.py:
__author__ = "byteme8bit"
import os
import shutil
import requests
def get_cat(folder, name):
url = 'http://consuming-python-services-api.azurewebsites.net/cats/random'
data = get_data_from_url(url)
save_image(folder, name, data)
def get_data_from_url(url):
response = requests.get(url, stream=True)
return response.raw
def save_image(folder, name, data):
filename = os.path.join(folder, name + '.jpg')
with open(filename, 'wb') as fout:
shutil.copyfileobj(data, fout)