Amazing-Python-Scripts

Форк
0
126 строк · 3.8 Кб
1
import praw
2
import PySimpleGUI as sg
3
import wget
4
import pandas as pd
5
import datetime as dt
6
import json
7
import os
8

9
destination_folder = sg.popup_get_folder(
10
    'Choose where to download files:\n\n'
11
    'NOTE: A folder to store the files will be created within the directory!',
12
    default_path='',
13
    title='Choose destination')
14
folder_lst = [destination_folder]
15
if folder_lst[0] is None:
16
    sg.Popup('Destination not specified!\nProgram terminated!',
17
             title='ERROR: No destination!',
18
             custom_text='Close',
19
             button_type=0)
20
    raise SystemExit()
21

22

23
class RedditCred:
24
    def __init__(self):
25
        self.text_file = 'reddit_tokens.json'
26

27

28
# Functions made to read the reddit app id and secret from file
29

30

31
    def read_id(self):
32
        file = self.text_file
33
        with open(file, 'r') as f:
34
            data = json.load(f)
35
            keys = data.keys()
36
            return str(*keys)
37

38
    def read_secret(self):
39
        file = self.text_file
40
        with open(file, 'r') as f:
41
            data = json.load(f)
42
            value = data.values()
43
            return str(*value)
44

45

46
red_cred = RedditCred()
47
u_agent = 'Script that downloads memes from various subreddits'
48

49
reddit = praw.Reddit(client_id=red_cred.read_id(),
50
                     client_secret=red_cred.read_secret(),
51
                     user_agent=u_agent)
52

53
subreddit = reddit.subreddit(
54
    'deepfriedmemes+surrealmemes+nukedmemes+bigbangedmemes+wackytictacs+bonehurtingjuice'
55
)
56
posts = subreddit.hot(limit=25)
57

58
# Empty lists to hold data
59

60
image_urls = []
61
image_titles = []
62
image_scores = []
63
image_timestamps = []
64
image_ids = []
65
image_extensions = ['.jpg', '.jpeg', '.png']
66

67
# This iterates through posts and collects their data into lists
68

69
for post in posts:
70
    image_urls.append(post.url.encode('utf-8'))
71
    image_titles.append(post.title.encode('utf-8'))
72
    image_scores.append(post.score)
73
    image_timestamps.append(dt.datetime.fromtimestamp(post.created))
74
    image_ids.append(post.id)
75

76
# This creates a GUI window with a progress bar to keep track of the download
77

78
layout = [[sg.Text(f"Downloading files...", key='textkey')],
79
          [sg.ProgressBar(25, orientation='h', size=(20, 20), key='progbar')],
80
          [sg.Cancel()]]
81

82
window = sg.Window('Download in Progress', layout)
83

84
# This iterates through URLs, checks if it has the specified image extension and downloads the image
85

86
for index, url in enumerate(image_urls):
87
    path = str(folder_lst[0])
88
    file_ending = str(url)[2:-1]
89
    event, values = window.read(timeout=0)
90
    _, extension = os.path.splitext(file_ending)
91
    if extension in image_extensions:
92
        try:
93
            if os.path.exists(path + '/' + 'Downloaded Images'):
94
                pass
95
            else:
96
                os.mkdir(path + '/' + 'Downloaded Images')
97
            if event == 'Cancel' or event == sg.WIN_CLOSED:
98
                break
99

100
            destination = str(folder_lst[0]) + '/' + 'Downloaded Images' + '/'
101
            window['progbar'].update_bar(index + 1)
102
            print(
103
                f"Downloading '{str(image_titles[index])[2:-1]}' to '{path}' from '{str(image_urls[index])[2:-1]}'"
104
            )
105
            download = wget.download(str(image_urls[index])[2:-1],
106
                                     out=destination)
107
        except:
108
            print(
109
                f"Something went wrong while downloading '{str(image_urls[index])[2:-1]}'\n"
110
            )
111
else:
112
    print("\nDownload complete!")
113
    window.close()
114
    sg.Popup(f"Files downloaded into:\n\n'{path}/Downloaded Images'",
115
             title='Download complete!')
116

117
# Optional saving of collected data to .csv file
118

119
dataframe = pd.DataFrame({
120
    'Title': image_titles,
121
    'Score': image_scores,
122
    'URL': image_urls,
123
    'Timestamp': image_timestamps,
124
    'ID': image_ids
125
})
126
csv = dataframe.to_csv('./images.csv', index=True, header=True)
127

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.