-
Notifications
You must be signed in to change notification settings - Fork 1
/
socialmedia.py
157 lines (126 loc) · 5.32 KB
/
socialmedia.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
from flask import Blueprint, render_template
import map
import json
import pandas as pd
import math
import numpy
import sys
sys.path.insert(0, '/instagram/')
from instagram import instagram_crawling_methods
socialmedia = Blueprint('socialmedia', __name__)
@socialmedia.route("/social/reddit/count")
def reddit_count():
csv_file = open('docs/toilet_Reddit_sentiments_3.csv')
count = sum(1 for line in csv_file) - 1
results = {"count": count}
return json.dumps(results)
@socialmedia.route("/social/instareview/count")
def instareview_count():
csv_file = open('docs/username_post.csv')
count = sum(1 for line in csv_file) - 1
csv_file.close()
csv_file = open('docs/toiletsg_post.csv')
count += sum(1 for line in csv_file) - 1
results = {"count": count}
return json.dumps(results)
@socialmedia.route("/insta/crawl/toiletsg")
def crawl_toilet_sg():
instagram_crawling_methods.get_csv_from_hashtag("toiletsg")
pd_read = pd.read_csv('docs/toiletsg_post.csv')
rows = pd_read.values.tolist()
columns = get_columns_dict(list(pd_read.columns))
print("Checking columns")
print(columns)
venue_dict = {"posts":[]}
location_name = None
for row in rows:
display_url = row[columns['display_url']]
print("Display url: " + display_url )
print(row[columns['location']])
if math.isnan(row[columns['location']]):
locData = row[columns['location.address_json']]
if type(locData) == str or not math.isnan(locData):
locData.replace(",","\"")
location_name = json.loads(locData)["zip_code"]
else:
location_name = row[columns['location']]
if location_name != None and type(location_name) == str :
poster_username = row[columns['owner.username']]
try:
caption = row[columns['edge_media_to_caption.edges']].replace("[","").replace("]","")
caption = caption.replace("\'","\"")
caption_obj = json.loads(caption)
except:
caption_obj = {"node":{"text":""}}
coord_dict = map.get_coords_by_name(location_name)
if coord_dict != None:
print("INSTA GET LOCATION FOR TOILETSG")
print(location_name)
print(coord_dict)
foundObj = {
"img_source": display_url,
"location_name": location_name,
"lat": coord_dict["lat"],
"lon": coord_dict["lon"],
"address": coord_dict["address"],
"user_name": "@" + poster_username,
"caption":caption_obj["node"]["text"]
}
venue_dict["posts"].append(foundObj)
venue_dict["count"] = len(venue_dict["posts"])
return json.dumps(venue_dict)
@socialmedia.route("/insta/crawl/sadtoilet")
def crawl_sad_toilet():
instagram_crawling_methods.get_csv_from_hashtag("sgsadtoilet")
pd_read = pd.read_csv('docs/sgsadtoilet_post.csv') #iterate this weird way because Dataframe to CSV is flawed
rows = pd_read.values.tolist()
columns = get_columns_dict(list(pd_read.columns))
print("Checking columns")
print(columns)
venue_dict = {"posts":[]}
location_name = None
for row in rows:
display_url = row[columns['display_url']]
print("Display url: " + display_url )
if type(row[columns['location.name']]) != str:
locData = row[columns['location.address_json']]
if type(locData) == str or not math.isnan(locData):
locData.replace(",","\"")
location_name = json.loads(locData)["zip_code"]
else:
location_name = row[columns['location.name']]
if location_name != None and type(location_name) == str :
poster_username = row[columns['owner.username']]
try:
caption = row[columns['edge_media_to_caption.edges']].replace("[","").replace("]","")
caption = caption.replace("\'","\"")
caption_obj = json.loads(caption)
except:
caption_obj = {"node":{"text":""}}
coord_dict = map.get_coords_by_name(location_name)
if coord_dict != None:
print("INSTA GET LOCATION FOR TOILETSG")
print(location_name)
print(coord_dict)
foundObj = {
"img_source": display_url,
"location_name": location_name,
"lat": coord_dict["lat"],
"lon": coord_dict["lon"],
"address": coord_dict["address"],
"user_name": "@" + poster_username,
"caption":caption_obj["node"]["text"]
}
venue_dict["posts"].append(foundObj)
venue_dict["count"] = len(venue_dict["posts"])
return json.dumps(venue_dict)
@socialmedia.route("/sgsadtoilet")
def show_sad_toilets():
response = crawl_sad_toilet()
hashtag_counts = json.loads(response)["count"]
return render_template('sgsadtoilet.html',title='Sg Sad Toilet',jsonResponse=response,count=hashtag_counts)
def get_columns_dict(datal):
diction = {}
for i in range(0,len(datal)):
diction[datal[i]] = i
return diction