File size: 3,393 Bytes
630930b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import os
import platform
import pwd
import time
from typing import List, Dict
from os import path as osp

import urllib.request as urllib2  # For Python 3.x

class Utils:
    """
    Bunch of utils used for the Reddit Downloader.
    """

    @staticmethod
    def save_to_preferences(preferences: Dict[str, Dict], preferences_file: str):
        """
        Save the preferences to a JSON file.
        :param preferences: Dict containing preferences to save to file.
        :param preferences_file: Location of the file where you want to save.
        """
        with open(preferences_file, 'w') as f:
            json.dump(preferences, f)

    @staticmethod
    def load_preferences(preferences_file: str) -> Dict:
        """
        Load the preferences from JSON file and return as Dict.
        :param preferences_file: Location of the file containing the preferences.
        :return: preferences - Dict containing preferences to save to file.
        """
        with open(preferences_file, 'r') as f:
            preferences = json.load(f)

        return preferences

    @staticmethod
    def get_os():
        """
        Get the OS type (Linux or Macbook), and set the wallpaper folder accordingly.
        :return:
            os_type: Type of OS (Linux for Linux, Darwin for Mac).
            wallpapers_directory: Directory where the wallpapers will be saved.
        """
        os_type = platform.system()
        assert os_type in {'Darwin', 'Linux'}

        # Get the username
        username = pwd.getpwuid(os.getuid()).pw_name

        # Set the directory to download images.
        wallpapers_directory = '/Users/{}/Pictures/Wallpapers/'.format(username) if os_type == 'Darwin' \
            else '/home/{}/Wallpapers/'.format(username)

        return platform.system(), wallpapers_directory

    @staticmethod
    def remove_unwanted_images(images: List[str]):
        """
        Remove unwanted images. Since this is a naive approach, we might end up downloading some unwanted images, so we
        delete them.
        :param images: List of image file locations to filter and remove unwanted images from.
        """
        count_removed = 0
        for image in images:
            # These are some random html pages that might have been downloaded.
            # This is a fairly quick and naive approach to downloading images from reddit.
            if osp.getsize(image) < 102400:
                os.remove(image)
                count_removed += 1
        return count_removed

    @staticmethod
    def fetch_subreddit_data(subreddit_url: str, max_trials: int = 20) -> Dict:
        """
        Fetch the subreddit JSON page based on the URL.
        :param subreddit_url: URL created based on user inputs (subreddit, sort_type, sort_time, max_download_count).
        :param max_trials: Maximum number of trial to use for fetching the subreddit JSON data.
        :return: subreddit_data - Nested Dict containing Subreddit data for query.
        """

        subreddit_data = None
        for _ in range(max_trials):
            try:
                subreddit_page = urllib2.urlopen(subreddit_url)
                subreddit_data = json.load(subreddit_page)
                break
            except:
                time.sleep(0.2)  # If we cannot access the reddit page, we wait for 2 seconds and retry.

        return subreddit_data