Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion booksoup/BookSoup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def load_conversation(self, search_name, interval="month"):
for link in convo_links:
if link.text != search_name:
continue
contact = Conversation(os.path.join(self.__path, link["href"]))
contact = Conversation(os.path.join(self.__path, link["href"]), interval=interval)
self.conversations[contact.name] = contact
return contact
return None
Expand Down
7 changes: 5 additions & 2 deletions booksoup/Conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ def interaction_freq(self):
def interaction_timeline(self, name):
return self.__fbt.interaction_timeline(name, self.messages)

def sentiment_timeline(self, name):
return self.__sent.sentiment_timeline(name)
def sentiment_timeline(self, name, interval):
return self.__sent.sentiment_timeline(name, interval)

def avg_sentiment(self, name):
return self.__sent.avg_sentiment(name)

def get24HourTime(self, elem):
return self.__fbt.get24HourTime(elem)

# Returns a list of participants in the conversation.
def __scrape_participants(self):
users = []
Expand Down
29 changes: 24 additions & 5 deletions booksoup/FbTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ def interaction_freq(self):

for date_str in self.span_meta:
time = date_str.split("at ")[1][:5]
amOrPm = date_str.split(":")[1][2:4]
hour = time.split(":")[0]
times[hour+":00"] += 1
if len(hour) == 1:
times["0"+hour+":00"+amOrPm] += 1
else:
times[hour+":00"+amOrPm] += 1
return times

# Returns a dict where each key is a date and each value is the number of
Expand All @@ -33,8 +37,11 @@ def interaction_timeline(self, name, messages):
# Creates a dictionary of times on the hour where each value is 0.
def generate_time_dict(self):
times = {}
for h in range(0,24):
time = self.__pad(h) + ":" + "00"
for h in range(1,13):
time = self.__pad(h) + ":" + "00am"
times[time] = 0
for h in range(1,13):
time = self.__pad(h) + ":" + "00pm"
times[time] = 0
return times

Expand Down Expand Up @@ -80,7 +87,19 @@ def span_meta_to_date(self, span_str, interval="month"):
span_str = ''.join(span_str.rsplit(',', 1))

date_arr = span_str.split(", ")[1].split(" ")[:3]
date_str = date_arr[2]+"-"+self.__pad(list(calendar.month_name).index(date_arr[1]))
date_str = date_arr[2]+"-"+self.__pad(list(calendar.month_name).index(date_arr[0]))
if interval == "day":
date_str += "-"+self.__pad(date_arr[0])
date_str += "-"+self.__pad(date_arr[1])
return date_str

def get24HourTime(self, elem):
amOrPm = elem.split(":")[1][2:4]
hour = int(elem.split(":")[0])
if amOrPm == "am":
if hour == 12:
return hour+12
return hour
else:
if hour == 12:
return hour
return hour+12
6 changes: 3 additions & 3 deletions booksoup/Sentiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def __init__(self, messages, fbt):
self.fbt = fbt
self.messages = messages

def sentiment_timeline(self, name):
timeline = self.fbt.generate_date_dict()
sentiment_counts = self.fbt.generate_date_dict()
def sentiment_timeline(self, name, interval="month"):
timeline = self.fbt.generate_date_dict(interval)
sentiment_counts = self.fbt.generate_date_dict(interval)
for message in self.messages:
if message.content is None or message.name != name:
continue
Expand Down
7 changes: 4 additions & 3 deletions demo_interaction_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
from booksoup import BookSoup
import numpy as np
import matplotlib.pyplot as plt
import sys

# Enter the path to the top level of your facebook data folder below.
me = BookSoup("facebook-data")
me = BookSoup(sys.argv[2])

# Enter the name of the conversation or the numerical ID below.
contact = me.load_conversation(274)
contact = me.load_conversation(sys.argv[1], sys.argv[3])

times = contact.interaction_freq()

objects = sorted(times.keys())
objects = sorted(times.keys(), key=contact.get24HourTime)
y_pos = np.arange(len(objects))
vals = [times[t] for t in objects]

Expand Down
5 changes: 3 additions & 2 deletions demo_interaction_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
from booksoup import BookSoup
import numpy as np
import matplotlib.pyplot as plt
import sys

times = []
objects = []
vals = []

# Enter the path to the top level of your facebook data folder below.
me = BookSoup("facebook-data")
me = BookSoup(sys.argv[2])

# Enter the name of the conversation or the numerical ID below.
conversation = me.load_conversation(108)
conversation = me.load_conversation(sys.argv[1])

for participant in conversation.participants:
timeline = conversation.interaction_timeline(participant)
Expand Down
8 changes: 5 additions & 3 deletions demo_sentiment_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
from booksoup import BookSoup
import numpy as np
import matplotlib.pyplot as plt
import sys

times = []
objects = []
vals = []

# Enter the path to the top level of your facebook data folder below.
me = BookSoup("facebook-data")
me = BookSoup(sys.argv[2])
interval = sys.argv[3]

# Enter the name of the conversation or the numerical ID below.
conversation = me.load_conversation(104)
conversation = me.load_conversation(sys.argv[1], interval=interval)

for participant in conversation.participants:
timeline = conversation.sentiment_timeline(participant)
timeline = conversation.sentiment_timeline(participant, interval)
sorted_keys = sorted(timeline.keys())
times.append(timeline)
objects.append(sorted_keys)
Expand Down