File size: 6,827 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
import pandas as pd
import urllib3
import json
from bs4 import BeautifulSoup
import numpy as np
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import Future
from traitlets import List

from reddit.reddit_info import subreddit_name_l, subreddit_sort_l, subreddit_t_l
import itertools
import random
from pathlib import Path

from tqdm import tqdm

from datetime import datetime, timezone
from typing import Any, Optional
from requests.utils import requote_uri
from random_word import RandomWords
from wonderwords import RandomSentence


class RedditProcessor:
    def get_subreddit_url(self, subreddit, sort_by:str = "hot", sort_time:str="all", limit:int=100, query:Optional[str]=None):
        if not query:
            return f'https://www.reddit.com/r/{subreddit}/{sort_by}/.json?raw_json=1&t={sort_time}&limit={limit}'
        else:
            return f'https://www.reddit.com/r/{subreddit}/search/.json?raw_json=1&q={query}&limit={100}'
    
    
    def fetch_subreddit_image_entries(self, subreddit_url: str, pool_manager):
        result = [ ]
        try:
            response = pool_manager.request('GET', subreddit_url)
            #print(response.data)
            subreddit_data = json.loads(response.data)        
            
            if not "data" in subreddit_data: return []
            if not "children" in subreddit_data["data"]: return []
                
            for content in subreddit_data['data']['children']:
                try:
                    if content['data'].get('post_hint', 'none') == 'image' and 'preview' in content['data']:                    
                        created_utc = datetime.fromtimestamp(content['data']["created_utc"], timezone.utc)
                        
                        #print(created_utc)
                        
                        source_d = content['data']['preview']['images'][0]['source']
                        image_url, width, height = source_d['url'], source_d["width"], source_d["height"]
                        image_title = content['data']['title']
                        image_id = content['data']['id']
                        data_url = content['data']['url']
                        subreddit = content['data']['subreddit']
                        if content['data']['is_video'] : continue                    
                        result.append({
                            "image_url" : image_url,
                            "title" : image_title,
                            "image_id" : image_id,
                            "url" : data_url,
                            "subreddit" : subreddit,
                            "width" : width,
                            "height" : height,
                            "created_utc" : created_utc,
                        })
                except Exception as e:
                    pass
            return result
        except Exception as e:
            #print(e)
            return []
    
    
    def fetch_multiple_subreddit_image_entries(self, subreddit_urls: str, thread_pool_size: int=5, urllib_pool_size:int=5):
    
        pool_manager = urllib3.PoolManager(maxsize=urllib_pool_size)
        thread_pool = ThreadPoolExecutor(thread_pool_size)
        res_futs = [ ]
        
        for subreddit_url in subreddit_urls:
            res_futs.append(thread_pool.submit(self.fetch_subreddit_image_entries, subreddit_url, pool_manager))
        
        res :[List[Future]] = []
            
        for r in res_futs:
            res.extend(r.result())
                
        return list({x["image_id"] : x for x in res}.values())
    
    def get_random_subreddit_urls(self, num_urls:int = 20):
        subr_l = list(itertools.product(subreddit_name_l, subreddit_sort_l, subreddit_t_l))
        return [self.get_subreddit_url(*xs, 100) for xs in random.sample(subr_l, k=num_urls)]


    def get_random_subreddit_query_urls(self, num_urls:int = 20, query_type: str = "chronology"):
        '''
            query_type:
                chronology
                random_word
                random_phrase
        '''
        timeline = random.choices(["days", "months", "years"], k = num_urls)
        timevalue = random.choices(range(1, 12), k = num_urls)
        subr = random.sample(subreddit_name_l, k = num_urls)
        
        if query_type == "chronology":
            return [self.get_subreddit_url(subreddit=sr, query=f"{tv} {tl} ago") for (sr, tl, tv) in list(itertools.product(subr, timeline, timevalue))]
        elif query_type == "random_word":
            r = RandomWords()
            return [self.get_subreddit_url(subreddit=sr, query=f"{r.get_random_word()}") for sr in subr]
        elif query_type == "random_phrase":
            s = RandomSentence()
            return [self.get_subreddit_url(subreddit=sr, query=f"{s.sentence()}") for sr in subr]
        else:
            return [ ]
        
        
    def __call__(self, reddit_out_file: os.PathLike) -> Any:
        dfname = reddit_out_file
        otime = 0

        tarr = []
        karr = []

        total_updates = 0

        with tqdm(total=10000) as pbar:
            for _  in range(10000):
                if random.random() > 0.6:
                    res = self.fetch_multiple_subreddit_image_entries(self.get_random_subreddit_urls(num_urls=100))
                else:
                    res = self.fetch_multiple_subreddit_image_entries(
                        self.get_random_subreddit_query_urls(num_urls=5, query_type="random_phrase"))
                
                num_fetched = len(res)
                
                if res:
                    if not Path(dfname).exists():
                        pd.DataFrame(res).to_csv(dfname, index=False)
                        karr.append(len(res))
                    else:
                        df = pd.read_csv(dfname)
                        keys = set(df["image_id"])
                        cres = [x for x in res if not (x["image_id"] in keys)]
                        
                        if cres:
                            ndf = pd.DataFrame(cres)
                            ndf.to_csv(dfname, mode="a", header=None, index=False)
                        karr.append(len(cres))
                else:
                    karr.append(0)
                        
                ntime = pbar.format_dict['elapsed']
                N = len(pd.read_csv(dfname))
                tarr.append(ntime-otime)
                otime = ntime
                tarr = tarr[-25:]
                karr = karr[-25:]
                rate = sum(karr)/sum(tarr)
                pbar.update(1)
                total_updates = total_updates + karr[-1]
                pbar.set_description_str(f"count:{N}, fetch rate:{rate:.3f}, last_update:{karr[-1]}, total_updates:{total_updates}")