2222from lib .objects .Posts import Post
2323from lib .objects .UsersAccount import UserAccount
2424from lib .objects .Usernames import Username
25+ from lib .objects import Images
2526
2627# TODO IMAGES + USER ACCOUNTS + FILENAMES
2728class Forum_ExtractorFeeder (DefaultFeeder ):
@@ -35,6 +36,7 @@ def __init__(self, json_data):
3536 self .seen_subforums = set ()
3637 self .root_subforums = set ()
3738 self .objs_to_process = set ()
39+ self .har_images = {}
3840
3941 def process_meta (self ):
4042 """Import one forum-extractor result dictionary from self.json_meta."""
@@ -60,6 +62,7 @@ def _import_success(self, result):
6062 forum_type = result .get ('forum_type' )
6163 forum_id = result .get ('forum_id' )
6264 extracted = result .get ('extracted' ) or {}
65+ self .har_images = result .get ('har_images' )
6366
6467 if not forum_type or not forum_id :
6568 # TODO Exception + logs
@@ -307,16 +310,48 @@ def _upsert_post(self, post_data, parent_thread):
307310 post .create (post_id , post_timestamp , content , parent_thread , self .forum , state = post_data .get ('post_state' ), quote_ids = quote_ids )
308311 for reaction in post_data .get ('reactions' , []):
309312 if isinstance (reaction , dict ):
310- reaction_name = reaction .get ('reaction ' )
313+ reaction_name = reaction .get ('reaction_type ' )
311314 reaction_count = reaction .get ('count' )
312315 else :
313316 reaction_name = None
314317 reaction_count = None
315318 if reaction_name and reaction_count :
319+ reaction_name = reaction_name .strip ().lower ().replace ("-" , "_" ).strip ("_" )
316320 post .add_reaction (reaction_name , int (reaction_count ))
321+
322+ # images
323+ # print('--------------------------------------------------')
324+ # print(post_data.get('content', {}).get('images', []))
325+ for url in post_data .get ('content' , {}).get ('images' , []):
326+ print ('URL:' , url )
327+ if url in self .har_images :
328+ image = self ._create_image_from_har_url (url , post .get_date (), post )
329+ self .objs_to_process .add (image )
330+
317331 user_account = self ._create_post_user_account (post_data .get ('author' ), post , post_timestamp )
332+ if user_account :
333+ author_image_url = (post_data .get ('author' , {})).get ('author_profile_image_url' )
334+ if author_image_url in self .har_images :
335+ image = self ._create_image_from_har_url (author_image_url , post .get_date (), user_account )
336+ if image :
337+ user_account .set_icon (image .get_global_id ())
338+ self .objs_to_process .add (image )
318339 return post
319340
341+ def _create_image_from_har_url (self , url , date , obj ):
342+ print (url )
343+ image_payload = self .har_images [url ]
344+ content = image_payload ['content' ]
345+ if not image_payload ['b64' ] and isinstance (content , str ):
346+ content = content .encode ()
347+ image = Images .create (content , b64 = image_payload ['b64' ])
348+ if image :
349+ # create correlation image - obj # TODO correlation forum ?
350+ image .add (date , obj )
351+ self .objs_to_process .add (image )
352+ print (image .id )
353+ return image
354+
320355 # TODO A POST MUST HAVE AN USER ACCOUNT
321356 def _create_post_user_account (self , author , post , timestamp ):
322357 """Create/update the post author user-account and username metadata."""
0 commit comments