-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexport.py
142 lines (101 loc) · 3.47 KB
/
export.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
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
"""
This program uses API key from webpage, and uses it to produce static lists
./data/export/static_lists/
-api API key
-search "search term"
"""
import argparse
import json
from pathlib import Path
from rsshistory.webtools import (
Url,
WebConfig,
)
export_dir = Path("..") / "data" / "exports" / "static_lists"
class Exporter(object):
def __init__(self, file_name):
self.file_name = file_name
self.entries = []
def add_entry(self, entry):
self.entries.append(entry)
def close(self):
json_data = {"entries" : self.entries }
text = json.dumps(json_data, indent=4)
if self.file_name:
export_dir.mkdir(parents=True, exist_ok=True)
file_name = export_dir / self.file_name
with open(str(file_name), "w") as fh:
fh.write(text)
else:
print(text)
return text
class Processor(object):
def __init__(self, parser, args):
self.parser = parser
self.args = args
self.exporter = Exporter(args.file_name)
self.num_pages = None
def process(self):
page_num = 1
if self.args.page_limit:
self.num_pages = int(self.args.page_limit)
while True:
if self.num_pages and page_num > self.num_pages:
break
if not self.num_pages or page_num <= self.num_pages:
status = self.process_for_page(page_num)
print("Done page [{}/{}]".format(page_num, self.num_pages))
if not status:
break
else:
break
page_num += 1
self.exporter.close()
def process_for_page(self, page_num):
address = self.args.host
workspace = self.args.workspace
search_term = self.args.search
key = self.args.key
full_address = f"https://{address}/apps/{workspace}/entries-json/?tags=1&search={search_term}&page={page_num}&key={key}"
print(f"Fetching {full_address}")
u = Url(full_address)
u.options.ssl_verify = False
response = u.get_response()
if response.is_valid():
json = self.response_to_json(response)
if not self.are_entries(json):
return False
if self.num_pages == None:
self.num_pages = int(json["num_pages"])
self.process_json_entries(json["entries"])
return True
else:
print(response)
return False
def process_json_entries(self, entries):
for entry in entries:
self.exporter.add_entry(entry)
def are_entries(self, json):
if not json or "entries" not in json or len(json["entries"]) == 0:
return False
return True
def response_to_json(self, response):
object = json.loads(response.get_text())
return object
def parse():
parser = argparse.ArgumentParser(prog="Exporter", description="Workspace Exporter")
parser.add_argument("-s", "--search")
parser.add_argument("-k", "--key")
parser.add_argument("-f", "--file-name")
parser.add_argument("--page-limit")
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("-w", "--workspace")
return parser, parser.parse_args()
def main():
WebConfig.init()
WebConfig.disable_ssl_warnings()
parser, args = parse()
p = Processor(parser, args)
p.process()
if __name__ == "__main__":
main()