-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpost_list.py
75 lines (61 loc) · 2.25 KB
/
post_list.py
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
'''Support for post filtering algorithms'''
import dataclasses
import os
import shutil
import numpy as np
from .post import Post
from .state import cache_base, staging_base, hqa_filename
@dataclasses.dataclass
class Algorithm:
'''An algorithm for filtering posts'''
hqa_filename: str = None
non_lqa_count: int = 30
lqa_count: int = 20
def get_filenames_with_algorithm(algorithm, all_files):
'''Return the list of JSON filenames for the posts using algorithm'''
with open(algorithm.hqa_filename or hqa_filename(), encoding='utf-8') as f:
hqas = [l.strip() for l in f]
files = list(np.random.choice([
file[0]
for file in all_files
if (file[1].username() not in ['benb', 'HamiltonsLive'])
and (file[1].full_account_name() in hqas)
], size=algorithm.non_lqa_count, replace=False))
files += list(np.random.choice([
file[0]
for file in all_files
if (file[1].username() not in ['benb', 'HamiltonsLive'])
and (file[1].full_account_name() not in hqas)
], size=algorithm.lqa_count, replace=False))
return files
class PostList:
'''A filtered list of Posts'''
def __init__(self, staging_path=None):
staging_path = staging_path or staging_base()
self._files = list(os.listdir(staging_path))
def plan(
self,
path=None,
staging_path=None,
algorithm=None,
copy_to_staging=False
):
'''Generate the list, unless there's already one, in which case do nothing'''
path = path or cache_base()
staging_path = staging_path or staging_base()
algorithm = algorithm or Algorithm()
if len(self._files) > 0:
return
all_files = [
(file, Post.from_file(f'{path}/{file}'))
for file in os.listdir(path)
]
self._files = get_filenames_with_algorithm(algorithm, all_files)
for filename in self._files:
if copy_to_staging:
shutil.copy(f'{path}/{filename}', f'{staging_path}/{filename}')
else:
shutil.move(f'{path}/{filename}', f'{staging_path}/{filename}')
def get_filenames(self):
'''Return the list of JSON filenames for the posts'''
return self._files