Skip to content

Commit

Permalink
improve and fix tiktok messages parser
Browse files Browse the repository at this point in the history
  • Loading branch information
98jfran committed May 7, 2020
1 parent a872d2d commit 7397c5d
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions modules/report/tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,9 @@ def get_user_messages(self):
else:
message["sender"] = conversation_output["participant_2"]
message["receiver"] = conversation_output["participant_1"]
message_type = entry[4]

message_dump = json.loads(entry[1])
body=""

if message_type == 7: #text message type
message["type"] = "text"
body = message_dump.get("text")

elif message_type == 8: #video message type
message["type"] = "video"
body= "https://www.tiktok.com/@tiktok/video/{}".format(message_dump.get("itemId"))

elif message_type == 5:
message["type"] = "gif"
body=message_dump.get("url").get("url_list")[0]
else:
message["type"] = "unknown"
body= str(message_dump)

message["message"] = body

message["type"] = self.get_message_type_by_id(entry[4])
message["message"] = self.parse_body_message_by_id(entry[4], json.loads(entry[1]))
message["deleted"] = str(entry[5])
conversation_output["messages"].append(message)

Expand Down Expand Up @@ -214,7 +196,6 @@ def get_videos(self):
video = {}
video["key"] = entry[0]
dump = json.loads(entry[1])

for line in dump["responseHeaders"].splitlines():
if 'Last-Modified:' in line:
video["last_modified"] = Utils.date_parser(line.split(": ")[1], "%a, %d %b %Y %H:%M:%S %Z")
Expand Down Expand Up @@ -252,7 +233,7 @@ def get_videos_publish(self):
for entry in aweme_list:
video ={}
video["created_time"] = entry.get("create_time")
video["video"] = str(entry.get("video"))#.get("animated_cover").get("url_list")[0]
video["video"] = str(entry.get("video").get("animated_cover").get("url_list")[0])


timeline_event = {}
Expand Down Expand Up @@ -302,4 +283,30 @@ def get_last_session(self):

logging.info("{} entrys found".format(len(results)))
return session

@staticmethod

def parse_body_message_by_id(message_type, message_dump):
body=""
if message_type == 7:
body = message_dump.get("text")
elif message_type == 8:
body= "https://www.tiktok.com/@tiktok/video/{}".format(message_dump.get("itemId"))
elif message_type == 5:
body=message_dump.get("url").get("url_list")[0]
elif message_type == 15:
body=message_dump.get("joker_stickers")[0].get("static_url").get("url_list")[0]
else:
body= str(message_dump)

return body


@staticmethod
def get_message_type_by_id(message_type_id):
if message_type_id == 7: return "text"
if message_type_id == 8: return "video"
if message_type_id == 5: return "gif"
if message_type_id == 15: return "gif"
return "unknown"

0 comments on commit 7397c5d

Please sign in to comment.