-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsharechat_helper.py
600 lines (553 loc) · 27 KB
/
sharechat_helper.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# Common helper functions for various Sharechat scrapers
import os
import requests
import pandas as pd
import numpy as np
import re
import datetime
from datetime import datetime
from IPython.display import Image, HTML
import time
from time import sleep
from random import uniform
import json
import urllib
import uuid
from PIL import Image
import io
from dotenv import load_dotenv
load_dotenv()
import wget
import s3_mongo_helper
import selenium
from selenium import webdriver
import tempfile
from tempfile import mkdtemp
import shutil
import subprocess
import logging
from tqdm import tqdm
import boto
import boto3
import pymongo
from pymongo import MongoClient
import sys
import codecs
# For targeted tag scraper
# Generates params for API requests
def generate_requests_dict(USER_ID, PASSCODE, tag_hash=None, content_type=None, unix_timestamp=None, post_key=None, bucket_id=None):
requests_dict = {
"tag_data_request": { # gets tag info
"api_url" : "https://apis.sharechat.com/explore-service/v1.0.0/tag?tagHash=",
"headers": {"content-type": "application/json",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"x-sharechat-authorized-userid": USER_ID,
"x-sharechat-secret": PASSCODE,
"x-sharechat-userid": USER_ID
}},
"trending_posts_request": { # gets media & metadata from trending section within tag
"body": {
"bn":"broker3",
"userId": USER_ID,
"passCode": PASSCODE,
"client":"web",
"message":{
"th": "{}".format(tag_hash),
"allowOffline": True}},
"api_url": "https://restapi1.sharechat.com/getViralPostsSeo",
"headers": {"content-type": "application/json",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"x-sharechat-authorized-userid": USER_ID,
"x-sharechat-secret": PASSCODE,
"x-sharechat-userid": USER_ID
}},
"type_specific_request": {# gets media & metadata by content type within tag (image/video/text)
"body": {
"bn":"broker3",
"userId": USER_ID,
"passCode": PASSCODE,
"client":"web",
"message":{
"tagHash": "{}".format(tag_hash),
"feed": True,
#"allowOffline": True,
"type": "{}".format(content_type)}},
"api_url": "https://restapi1.sharechat.com/requestType88",
"headers": {"content-type": "application/json",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"x-sharechat-authorized-userid": USER_ID,
"x-sharechat-secret": PASSCODE,
"x-sharechat-userid": USER_ID
}},
"fresh_posts_request": {# gets media & metadata by timestamp ("fresh" content)
"body": {
"bn":"broker3",
"userId": USER_ID,
"passCode": PASSCODE,
"client":"web",
"message":{
"th": "{}".format(tag_hash),
"s": "{}".format(unix_timestamp),
"allowOffline": True}},
"api_url": "https://restapi1.sharechat.com/requestType25",
"headers": {"content-type": "application/json",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"x-sharechat-authorized-userid": USER_ID,
"x-sharechat-secret": PASSCODE,
"x-sharechat-userid": USER_ID
}},
"virality_metrics_request": { # gets current virality metrics for a post
"body": {
"bn":"broker3",
"userId": USER_ID,
"passCode": PASSCODE,
"client":"web",
"message":{
"key": "{}".format(post_key),
"ph": "{}".format(post_key),
"allowOffline": True
}},
"api_url" : "https://restapi1.sharechat.com/requestType45",
"headers": {"content-type": "application/json",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"x-sharechat-authorized-userid": USER_ID,
"x-sharechat-secret": PASSCODE,
"x-sharechat-userid": USER_ID
}},
"bucket_data_request": { # gets list of tag hashes in a bucket
"body": {
"bn":"broker3",
"userId": USER_ID,
"passCode": PASSCODE,
"client":"web",
"message":{
"key": "{}".format(bucket_id),
"bucketId": bucket_id,
"t": 3
}},
"api_url" : "https://apis.sharechat.com/requestType66",
"headers": {"content-type": "application/json",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"x-sharechat-authorized-userid": USER_ID,
"x-sharechat-secret": PASSCODE,
"x-sharechat-userid": USER_ID
}}
}
return requests_dict
# Gets tag hashes from content bucket
def get_tag_hashes(USER_ID, PASSCODE, bucket_ids):
tag_hashes = []
for i in bucket_ids:
try:
requests_dict = generate_requests_dict(USER_ID, PASSCODE, tag_hash=None, content_type=None, unix_timestamp=None, post_key=None, bucket_id=i)
bucket_data_response_dict = get_response_dict(requests_dict=requests_dict, request_type="bucket_data_request")
for tag in bucket_data_response_dict["payload"]["tags"]:
tag_hashes.append(tag["tagHash"])
except Exception as e:
print(logging.traceback.format_exc())
return tag_hashes
def get_response_dict(requests_dict, request_type):
url = requests_dict[request_type]["api_url"]
headers = requests_dict[request_type]["headers"]
if request_type == "tag_data_request":
response = requests.get(url=url, headers=headers)
response_dict = json.loads(response.text)
else:
body = requests_dict[request_type]["body"]
response = requests.post(url=url, json=body, headers=headers)
response_dict = json.loads(response.text)
return response_dict
def get_tag_data(payload_dict):
tag_name = payload_dict["tagName"]
tag_translation = payload_dict["englishMeaning"]
tag_genre = payload_dict["tagGenre"]
bucket_name = payload_dict["bucketName"]
bucket_id = payload_dict["bucketId"]
return tag_name, tag_translation, tag_genre, bucket_name, bucket_id
# Gets payload metadata that is common across content types
def get_common_metadata(payload_key, timestamp, language, media_type, post_permalink, caption, external_shares, likes, comments, reposts, views, profile_page):
timestamp.append(payload_key["o"])
language.append(payload_key["m"])
media_type.append(payload_key["t"])
post_permalink.append(payload_key["permalink"])
profile_page.append("https://sharechat.com/profile/"+payload_key["ath"]["h"])
if "c" in payload_key.keys():
caption.append(payload_key["c"])
else:
caption.append(None)
virality_metrics = {"usc": external_shares,
"lc": likes,
"c2": comments,
"repostCount": reposts,
"l": views}
for metric in virality_metrics:
if metric in payload_key.keys():
virality_metrics[metric].append(payload_key[metric])
else:
virality_metrics[metric].append(0)
# Gets tag contents i.e. metadata for each post
def get_post_data(payload_dict, tag_name, tag_translation, tag_genre, bucket_name, bucket_id):
media_link = []
timestamp = []
language = []
media_type = []
external_shares = []
likes = []
comments = []
reposts = []
post_permalink = []
caption = []
text = []
views = []
profile_page = []
for i in payload_dict["payload"]["d"]:
if i["t"] == "image":
get_common_metadata(i, timestamp, language, media_type, post_permalink, caption, external_shares, likes, comments, reposts, views, profile_page)
media_link.append(i["g"])
text.append(None)
elif i["t"] == "video":
get_common_metadata(i, timestamp, language, media_type, post_permalink, caption, external_shares, likes, comments, reposts, views, profile_page)
media_link.append(i["v"])
text.append(None)
elif i["t"] == "text":
if "x" in i.keys(): # if post metadata contains the text
get_common_metadata(i, timestamp, language, media_type, post_permalink, caption, external_shares, likes, comments, reposts, views, profile_page)
text.append(i["x"])
media_link.append(None)
else:
pass
elif i["t"] == "link":
if "ld" in i.keys(): # if post metadata contains link description
get_common_metadata(i, timestamp, language, media_type, post_permalink, caption, external_shares, likes, comments, reposts, views, profile_page)
media_link.append(i["hl"])
text.append(i["ld"])
else:
pass
else:
pass
post_data = pd.DataFrame(np.column_stack([media_link, timestamp, language, media_type, external_shares, likes, comments, reposts, post_permalink, caption, text, views, profile_page]),
columns = ["media_link", "timestamp", "language", "media_type",
"external_shares", "likes", "comments",
"reposts", "post_permalink", "caption", "text", "views", "profile_page"])
post_data["tag_name"] = tag_name
post_data["tag_translation"] = tag_translation
post_data["tag_genre"] = tag_genre
post_data["bucket_name"] = bucket_name
post_data["bucket_id"] = int(bucket_id)
return post_data
# Gets next offset hash for scraping the next page
def get_next_offset_hash(payload_dict):
if "nextOffsetHash" in payload_dict["payload"]:
next_offset_hash = payload_dict["payload"]["nextOffsetHash"]
else:
next_offset_hash=None
return next_offset_hash
# Gets next timestamp for scraping the next page
def get_next_timestamp(payload_dict):
if "n" in payload_dict["payload"]:
next_timestamp = payload_dict["payload"]["n"]
else:
next_timestamp=None
return next_timestamp
# Gets trending tag data
def get_trending_data(USER_ID, PASSCODE, tag_hashes, pages, delay):
# Create empty dataframe to collect scraped data
df = pd.DataFrame(columns = ["media_link", "timestamp", "language",
"media_type", "tag_name", "tag_translation",
"tag_genre", "bucket_name", "bucket_id",
"external_shares", "likes", "comments",
"reposts", "post_permalink", "caption", "text", "views", "profile_page"])
content_types = ["image", "video", "text"] # add others if required
for tag_hash in tag_hashes:
#next_offset_hash = None
next_offset_hash = "kdn0"
tagDataScraped = False
try:
# Send API request to scrape tag info
requests_dict = generate_requests_dict(USER_ID, PASSCODE, tag_hash=tag_hash, content_type=None, unix_timestamp=None, post_key=None)
requests_dict["tag_data_request"]["api_url"] = requests_dict["tag_data_request"]["api_url"]+tag_hash+"&groupTag=true"
tag_data_response_dict = get_response_dict(requests_dict=requests_dict, request_type="tag_data_request")
tag_name, tag_translation, tag_genre, bucket_name, bucket_id = get_tag_data(tag_data_response_dict)
tagDataScraped = True
except Exception as e:
print("Could not scrape data from '{}'".format(tag_hash))
print("Continuing ...")
pass
# Send API requests to scrape tag media & metadata
if tagDataScraped:
# Scrape trending pages
for i in range(pages):
try:
if next_offset_hash is not None:
requests_dict["trending_posts_request"]["body"]["message"]["nextOffsetHash"] = "{}".format(next_offset_hash)
else:
pass
post_data_response_dict = get_response_dict(requests_dict=requests_dict, request_type="trending_posts_request")
post_data = get_post_data(post_data_response_dict, tag_name, tag_translation, tag_genre, bucket_name, bucket_id)
next_offset_hash = get_next_offset_hash(post_data_response_dict)
df = df.append(post_data, sort = True)
time.sleep(delay) # random time delay between requests
except Exception as e:
print(logging.traceback.format_exc())
# Scrape additional content by content type
for c in content_types:
try:
requests_dict["type_specific_request"]["body"]["message"]["type"] = "{}".format(c)
type_specific_response_dict = get_response_dict(requests_dict=requests_dict, request_type="type_specific_request")
post_data = get_post_data(type_specific_response_dict, tag_name, tag_translation, tag_genre, bucket_name, bucket_id)
df = df.append(post_data, sort = True)
time.sleep(delay)
except Exception as e:
print(logging.traceback.format_exc())
else:
pass
df.drop_duplicates(inplace = True)
df["timestamp"] = df["timestamp"].apply(lambda x: datetime.utcfromtimestamp(int(x)))
df["filename"] = [str(uuid.uuid4()) for x in range(len(df))]
df["scraped_date"] = datetime.utcnow()
return df
# Gets fresh tag data
def get_fresh_data(USER_ID, PASSCODE, tag_hashes, pages, unix_timestamp, delay):
# Create empty dataframe to collect scraped data
print("Getting fresh data ...")
df = pd.DataFrame(columns = ["media_link", "timestamp", "language",
"media_type", "tag_name", "tag_translation",
"tag_genre", "bucket_name", "bucket_id",
"external_shares", "likes", "comments",
"reposts", "post_permalink", "caption", "text", "views", "profile_page"])
for tag_hash in tag_hashes:
request_timestamp = unix_timestamp
tagDataScraped = False
try:
# Send API request to scrape tag info
requests_dict = generate_requests_dict(USER_ID, PASSCODE, tag_hash=tag_hash, content_type=None, unix_timestamp=unix_timestamp, post_key=None)
requests_dict["tag_data_request"]["api_url"] = requests_dict["tag_data_request"]["api_url"]+tag_hash+"&groupTag=true"
tag_data_response_dict = get_response_dict(requests_dict=requests_dict, request_type="tag_data_request")
tag_name, tag_translation, tag_genre, bucket_name, bucket_id = get_tag_data(tag_data_response_dict)
tagDataScraped = True
except Exception as e:
print("Could not scrape data from '{}'".format(tag_hash))
print("Continuing ...")
pass
# Send API requests to scrape tag media & metadata
if tagDataScraped:
# Scrape fresh pages
for i in range(pages):
try:
requests_dict["fresh_posts_request"]["body"]["message"]["s"] = "{}".format(request_timestamp)
fresh_posts_response_dict = get_response_dict(requests_dict=requests_dict, request_type="fresh_posts_request")
fresh_posts_data = get_post_data(fresh_posts_response_dict, tag_name, tag_translation, tag_genre, bucket_name, bucket_id)
request_timestamp = get_next_timestamp(fresh_posts_response_dict)
df = df.append(fresh_posts_data, sort = True)
time.sleep(delay)
except Exception:
pass
else:
pass
df.drop_duplicates(inplace = True)
df["timestamp"] = df["timestamp"].apply(lambda x: datetime.utcfromtimestamp(int(x)))
df["filename"] = [str(uuid.uuid4()) for x in range(len(df))]
df["scraped_date"] = datetime.utcnow()
return df
# Mongo upload function for targeted tag scraper
def sharechat_mongo_upload(df, coll):
#coll = s3_mongo_helper.initialize_mongo()
for i in df.to_dict("records"):
s3_mongo_helper.upload_to_mongo(data=i, coll=coll)
# Generate html file with thumbnails for image and video posts
def get_thumbnails_from_s3(df):
def path_to_image_html(path):
return '<img src="'+ path + '"width="200" >'
thumbnail = []
aws, bucket, s3 = s3_mongo_helper.initialize_s3()
temp_dir = tempfile.mkdtemp(dir=os.getcwd())
for link in df["s3_url"]:
if link is not None:
if link.split(".")[-1] == "mp4":
video_input_path = link
img_output_path = temp_dir.split("/")[-1]+"/"+link.split("/")[-1].split(".")[0]+".jpg"
filename = link.split("/")[-1].split(".")[0]+".jpg"
subprocess.call(['ffmpeg', '-i', video_input_path, '-ss', '00:00:00.000', '-vframes', '1', img_output_path], stderr=subprocess.DEVNULL,stdout=subprocess.DEVNULL)
s3_mongo_helper.upload_to_s3(s3=s3, file=img_output_path, filename=filename, bucket=bucket, content_type="image/jpeg")
thumbnail.append(aws+bucket+"/"+filename)
elif link.split(".")[-1] == "txt":
thumbnail.append(None)
else: # if jpg/jpeg/png
thumbnail.append(link)
else: # if NaN
thumbnail.append(None)
df['thumbnail'] = np.array(thumbnail)
pd.set_option('display.max_colwidth', -1)
df_html = HTML(df.to_html(index = False, escape=False ,formatters=dict(thumbnail=path_to_image_html), render_links = True))
shutil.rmtree(temp_dir)
return df, df_html
def get_thumbnails_from_sharechat(df):
def path_to_image_html(path):
return '<img src="'+ path + '"width="200" >'
thumbnail = []
temp_dir = tempfile.mkdtemp(dir=os.getcwd())
for link in df["media_link"]:
if link is not None and "sharechat" in link:
if link.split(".")[-1] == "mp4":
video_input_path = link
img_output_path = temp_dir.split("/")[-1]+"/"+link.split("/")[-1].split(".")[0]+".jpg"
subprocess.call(['ffmpeg', '-i', video_input_path, '-ss', '00:00:00.000', '-vframes', '1', img_output_path], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
thumbnail.append(img_output_path)
elif link.split(".")[-1] == "txt":
thumbnail.append(None)
else: # if jpg/jpeg/png
thumbnail.append(link)
else: # if NaN
thumbnail.append(None)
df['thumbnail'] = np.array(thumbnail)
#print(df["thumbnail"])
pd.set_option('display.max_colwidth', -1)
df_html = HTML(df.to_html(index = False, escape=False ,formatters=dict(thumbnail=path_to_image_html), render_links = True))
shutil.rmtree(temp_dir)
return df, df_html
# Virality scraper helper functions
def save_updated_df(df, today):
df.to_csv("virality_df_{}.csv".format(today), index=False)
def scrape_metrics(response_dict):
virality_metrics = {"c2": "comments",
"usc": "external_shares",
"lc": "likes",
"repostCount": "reposts",
"l": "views"}
values = [[]]
for key in virality_metrics:
if key in response_dict["payload"]["d"].keys():
res = int(response_dict["payload"]["d"][key])
values[0].append(res)
else:
values[0].append(0)
return values
def get_current_metrics(USER_ID, PASSCODE, post_permalink):
post_key = post_permalink.split("/")[-1]
requests_dict = generate_requests_dict(USER_ID, PASSCODE, tag_hash=None, content_type=None, unix_timestamp=None, post_key=post_key)
# Send API request & get response
virality_metrics_response_dict = get_response_dict(requests_dict=requests_dict, request_type="virality_metrics_request")
# Scrape current metrics for post
result = scrape_metrics(virality_metrics_response_dict)
time.sleep(uniform(30,35))
return result
# s3 upload functions for ML scraper
def ml_initialize_s3():
aws_access_key_id = os.environ.get("AWS_ACCESS_KEY_ID")
aws_secret_access_key = os.environ.get("AWS_SECRET_ACCESS_KEY_ID")
aws = os.environ.get("AWS_BASE_URL")
bucket = os.environ.get("AWS_ML_BUCKET") # changed bucket
s3 = boto3.client("s3", aws_access_key_id = aws_access_key_id,
aws_secret_access_key= aws_secret_access_key)
return aws, bucket, s3
def ml_upload_to_s3(s3, file, filename, bucket, content_type):
with open(file, "rb")as data:
s3.upload_fileobj(Fileobj = data,
Bucket = bucket,
Key = "machinelearning-negatives/"+filename)
def ml_sharechat_s3_upload(df, aws, bucket, s3):
for index, row in df.iterrows():
try:
if (row["media_type"] == "image"):
# Create S3 file name
filename = row["filename"]+".jpg"
# Get media
temp = wget.download(row["media_link"])
# Upload media to S3
ml_upload_to_s3(s3=s3, file=temp, filename=filename, bucket=bucket, content_type=row["media_type"])
os.remove(temp)
elif (row["media_type"] == "video"):
# Create S3 file name
filename = row["filename"]+".mp4"
# Get media
temp = wget.download(row["media_link"])
# Upload media to S3
ml_upload_to_s3(s3=s3, file=temp, filename=filename, bucket=bucket, content_type=row["media_type"])
os.remove(temp)
else: # for text posts and media links
# Create S3 file name
filename = row["filename"]+".txt"
# Create text file
with open("temp.txt", "w+") as f:
f.write(row["text"])
# Upload media to S3
ml_upload_to_s3(s3=s3, file="temp.txt", filename=filename, bucket=bucket, content_type=row["media_type"])
os.remove("temp.txt")
except:
pass
# Add S3 urls with correct extensions
df.reset_index(inplace = True)
df.loc[df["media_type"] == "image", "s3_url"] = aws+bucket+"/"+df["filename"]+".jpg"
df.loc[df["media_type"] == "video", "s3_url"] = aws+bucket+"/"+df["filename"]+".mp4"
df.loc[df["media_type"] == "text", "s3_url"] = aws+bucket+"/"+df["filename"]+".txt"
df.loc[df["media_type"] == "link", "s3_url"] = aws+bucket+"/"+df["filename"]+".txt"
return df # return df with s3 urls added
def sharechat_s3_upload(df, aws, bucket, s3):
for index, row in df.iterrows():
try:
if (row["media_type"] == "image"):
# Create S3 file name
filename = row["filename"]+".jpg"
# Get media
temp = wget.download(row["media_link"])
# Upload media to S3
s3_mongo_helper.upload_to_s3(s3=s3, file=temp, filename=filename, bucket=bucket, content_type="image/jpeg")
os.remove(temp)
elif (row["media_type"] == "video"):
# Create S3 file name
filename = row["filename"]+".mp4"
# Get media
temp = wget.download(row["media_link"])
# Upload media to S3
s3_mongo_helper.upload_to_s3(s3=s3, file=temp, filename=filename, bucket=bucket, content_type="video/mp4")
os.remove(temp)
else: # for text posts and media links
# Create S3 file name
filename = row["filename"]+".txt"
# Create text file
with codecs.getwriter("utf8")(open("temp.txt", "wb")) as f:
f.write(row["text"])
# with open("temp.txt", "w+") as f:
# f.write(row["text"])
# Upload media to S3
s3_mongo_helper.upload_to_s3(s3=s3, file="temp.txt", filename=filename, bucket=bucket, content_type="application/json")
os.remove("temp.txt")
except:
pass
# Add S3 urls with correct extensions
df.reset_index(inplace = True)
df.loc[df["media_type"] == "image", "s3_url"] = aws+bucket+"/"+df["filename"]+".jpg"
df.loc[df["media_type"] == "video", "s3_url"] = aws+bucket+"/"+df["filename"]+".mp4"
df.loc[df["media_type"] == "text", "s3_url"] = aws+bucket+"/"+df["filename"]+".txt"
df.loc[df["media_type"] == "link", "s3_url"] = aws+bucket+"/"+df["filename"]+".txt"
return df # return df with s3 urls added
def ml_initialize_mongo():
mongo_url = "mongodb+srv://"+os.environ.get("SHARECHAT_DB_USERNAME")+":"+os.environ.get("SHARECHAT_DB_PASSWORD")+"@tattle-data-fkpmg.mongodb.net/test?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE"
cli = MongoClient(mongo_url)
db = cli[os.environ.get("SHARECHAT_DB_NAME")]
coll = db[os.environ.get("SHARECHAT_ML_DB_COLLECTION")]
if coll.count_documents({}) > 0:
return coll
else:
print("Error accessing Mongo collection")
sys.exit()
def ml_sharechat_mongo_upload(df, coll):
#coll = s3_mongo_helper.initialize_mongo()
for i in df.to_dict("records"):
s3_mongo_helper.upload_to_mongo(data=i, coll=coll)
# Old helper functions
# Saves data locally in csv and html formats
def save_data_to_disk(df, html):
with open("sharechat_data_preview.html", "w") as f:
f.write(html.data)
df.drop("thumbnail", axis = 1, inplace = True)
df.to_csv("sharechat_data.csv")
# Converts links to thumbnails in html
def convert_links_to_thumbnails(df):
df["thumbnail"] = df["media_link"]
def path_to_image_html(path):
return '<img src="'+ path + '"width="200" >'
image_df = df[df["media_type"] == "image"]
pd.set_option('display.max_colwidth', -1)
data_html = HTML(image_df.to_html(index = False, escape=False ,formatters=dict(thumbnail=path_to_image_html), render_links = True))
return data_html