-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_flow.py
More file actions
72 lines (61 loc) · 2.43 KB
/
Copy pathprocess_flow.py
File metadata and controls
72 lines (61 loc) · 2.43 KB
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
import os
import easyocr
import datetime
import instaloader
from tqdm import tqdm
from metaflow import FlowSpec, Parameter, step, conda_base
# @conda_base(python="3.8.0", base=conda_base.DEFAULT_BASE)
class ProcessFlow(FlowSpec):
iloader = instaloader.Instaloader(
download_pictures=True,
download_videos=False,
download_video_thumbnails=False,
compress_json=False,
save_metadata=False)
start_date = Parameter("start_date", help="Define start date")
end_date = Parameter("end_date", help="Define end date")
profile = Parameter("profile", help="Define target instagram profile")
reader = easyocr.Reader(['en'])
@step
def start(self):
print(f"Start date: {self.start_date}")
print(f"End date: {self.end_date}")
print(f"Profile: {self.profile}")
self.next(self.scrape_data)
@step
def scrape_data(self):
print(f"Scraping profile {self.profile} for posts between {self.start_date} and {self.end_date}")
since = datetime.datetime.strptime(self.start_date, "%Y-%m-%d")
until = datetime.datetime.strptime(self.end_date, "%Y-%m-%d")
posts = instaloader.Profile.from_username(self.iloader.context, self.profile).get_posts()
filtered_posts = filter(lambda p: since <= p.date <= until, posts)
for post in filtered_posts:
self.iloader.download_post(post, self.profile)
self.next(self.filter_data)
@step
def filter_data(self):
print(f"Filtering out images only")
for filename in os.listdir(self.profile):
if not filename.endswith(".jpg"):
os.remove(os.path.join(self.profile, filename))
self.next(self.extract_text)
@step
def extract_text(self):
print("Extracting text from images")
self.text_container = []
img_list = [os.path.join(self.profile, f) for f in os.listdir(self.profile) if f.endswith(".jpg")]
for img in tqdm(img_list):
result = self.reader.readtext(img, detail = 0)
self.text_container.extend(result)
self.next(self.save_data)
@step
def save_data(self):
print("Saving data")
with open(f'{self.profile}_data.txt', 'w') as f:
f.write(" ".join(self.text_container))
self.next(self.end)
@step
def end(self):
print("End of Flow")
if __name__ == "__main__":
ProcessFlow()