def process_status(status,proc_posts,movies10,music10,args): i = args['i'] scores = args['scores'] movie_genres = args['movie'] music_genres = args['music'] picture = args['pic'] title='' embed='' description='' score_now = 0 post_id = status['id'] if post_id not in proc_posts: proc_posts.append(post_id) else: return proc_posts,movies10,music10 created = status['updated_time'][:10] if 'message' in status: message = status['message'] else: message = '' if 'comments' in status: comments = status['comments']['data'] else: comments = '' f_score = 0 #final score m_score = analysis.sentiment_analysis(message.encode('utf-8')) #message score if m_score >=-0.1: f_score +=1 else: f_score -=1 for c in comments: if c['from']['id'] == scores[i]['facebook_id']: c_score = 0 c_score = analysis.sentiment_analysis(c['message'].encode('utf8')) #comment_score if c_score >=-0.1: f_score+=1 else: f_score-=1 if f_score < 0: return proc_posts,movies10,music10 message = re.sub('\n',' ', message) message = re.split('[ .,!?]',message) message = ' '.join(message[:8]) results = text_analysis_freebase.getResults(message.encode('utf-8')) for entity in results: ubuff = 0 if bool(entity): if entity['type'] == 'movie' and entity['genres']: for gen in entity['genres']: if (gen in movie_genres.keys()): ubuff += movie_genres[gen] ubuff = float(ubuff)/len(entity['genres']) score_now = float(scores[i]['movie_score']['movie_friend_score'])*ubuff if score_now > 0: title,url,description = youtubeAPI.getVideo(entity) if title: exist_flag = 0 for it in movies10: if ((post_id == it["post_id"]) and (url == it["embed"])) or url == it["embed"]: exist_flag = 1 if (exist_flag == 0): movies10.append({"name":scores[i]['friend_name'],"f_id":scores[i]['facebook_id'],"post_id":post_id,"score":score_now,"message":message.encode('utf-8'),'picture':picture,'title':title,'embed':url,'description':description,'created':created,'genres':entity['genres'],'rated':0}) elif entity['type'] == 'music' and entity['genres']: for key in entity['genres']: if key in music_genres.keys(): ubuff += music_genres[key] ubuff = float(ubuff)/len(entity['genres']) score_now = float(scores[i]['music_score']['music_friend_score'])*ubuff if score_now>0.0: title,url,description = youtubeAPI.getVideo(entity) if title: exist_flag = 0 for it in music10: if ((post_id == it["post_id"]) and (url == it["embed"])) or url == it["embed"]: exist_flag = 1 if exist_flag == 0: music10.append({"name":scores[i]['friend_name'],"f_id":scores[i]['facebook_id'],"post_id":post_id,"score":score_now,"message":message.encode('utf-8'),'picture':picture,'title':title,'embed':url,'description':description,'created':created,'genres':entity['genres'],'rated':0}) return proc_posts,movies10,music10
def process_status(status, proc_posts, movies10, music10, args): i = args['i'] scores = args['scores'] movie_genres = args['movie'] music_genres = args['music'] picture = args['pic'] title = '' embed = '' description = '' score_now = 0 post_id = status['id'] if post_id not in proc_posts: proc_posts.append(post_id) else: return proc_posts, movies10, music10 created = status['updated_time'][:10] if 'message' in status: message = status['message'] else: message = '' if 'comments' in status: comments = status['comments']['data'] else: comments = '' f_score = 0 #final score m_score = analysis.sentiment_analysis( message.encode('utf-8')) #message score if m_score >= -0.1: f_score += 1 else: f_score -= 1 for c in comments: if c['from']['id'] == scores[i]['facebook_id']: c_score = 0 c_score = analysis.sentiment_analysis( c['message'].encode('utf8')) #comment_score if c_score >= -0.1: f_score += 1 else: f_score -= 1 if f_score < 0: return proc_posts, movies10, music10 message = re.sub('\n', ' ', message) message = re.split('[ .,!?]', message) message = ' '.join(message[:8]) results = text_analysis_freebase.getResults(message.encode('utf-8')) for entity in results: ubuff = 0 if bool(entity): if entity['type'] == 'movie' and entity['genres']: for gen in entity['genres']: if (gen in movie_genres.keys()): ubuff += movie_genres[gen] ubuff = float(ubuff) / len(entity['genres']) score_now = float( scores[i]['movie_score']['movie_friend_score']) * ubuff if score_now > 0: title, url, description = youtubeAPI.getVideo(entity) if title: exist_flag = 0 for it in movies10: if ((post_id == it["post_id"]) and (url == it["embed"])) or url == it["embed"]: exist_flag = 1 if (exist_flag == 0): movies10.append({ "name": scores[i]['friend_name'], "f_id": scores[i]['facebook_id'], "post_id": post_id, "score": score_now, "message": message.encode('utf-8'), 'picture': picture, 'title': title, 'embed': url, 'description': description, 'created': created, 'genres': entity['genres'], 'rated': 0 }) elif entity['type'] == 'music' and entity['genres']: for key in entity['genres']: if key in music_genres.keys(): ubuff += music_genres[key] ubuff = float(ubuff) / len(entity['genres']) score_now = float( scores[i]['music_score']['music_friend_score']) * ubuff if score_now > 0.0: title, url, description = youtubeAPI.getVideo(entity) if title: exist_flag = 0 for it in music10: if ((post_id == it["post_id"]) and (url == it["embed"])) or url == it["embed"]: exist_flag = 1 if exist_flag == 0: music10.append({ "name": scores[i]['friend_name'], "f_id": scores[i]['facebook_id'], "post_id": post_id, "score": score_now, "message": message.encode('utf-8'), 'picture': picture, 'title': title, 'embed': url, 'description': description, 'created': created, 'genres': entity['genres'], 'rated': 0 }) return proc_posts, movies10, music10
def readStatusAndCreateLists(uid): client = MongoClient('127.0.0.1') db = client.recommendation_db user_coll = db.users movies10 = [] music10 = [] friends_list = [] query = user_coll.find_one({"id":uid}) user = query acc_token = user['token'] graph = fb.GraphAPI(acc_token) scores = user['scores'] lim = len(scores) music_genres = user['music_genres'] movie_genres = user['movie_genres'] proc_posts = user['proc_posts'] #ore ex = datetime.now() - timedelta(days=14) fields = {"fields":'links.since('+str(ex)+').fields(comments.fields(id,from,message).limit(10),message,link,id,name,created_time,description),name,picture,statuses.since('+str(ex)+').fields(comments.fields(id,from,message).limit(10),message,id,updated_time),video.watches.fields(id,data,publish_time,message),music.listens.fields(id,data,publish_time,message)'} for i in range(0,lim): args = {} links_filtered = [] statuses_filtered = [] videos = [] music = [] if not scores[i]['music_score'] and not scores[i]['movie_score']: continue try: statuses = graph.get_object(scores[i]['facebook_id'],**fields) except: print str(scores[i]['friend_name'].encode('utf8')) + " - Graph error\n" continue if 'links' in statuses: links_filtered = statuses['links']['data'] if 'statuses' in statuses: statuses_filtered = statuses['statuses']['data'] if 'video.watches' in statuses: videos = statuses['video.watches']['data'] if 'music.listens' in statuses: music = statuses['music.listens']['data'] picture = statuses['picture']['data']['url'] for video in videos: p_time = video['publish_time'] if 'movie' in video['data'] and p_time > str(ex): entity = video['data']['movie']['title'] post_id = video['id'] if post_id not in proc_posts: proc_posts.append(post_id) else: continue check = text_analysis_freebase.search(entity,"movie") entity = check if bool(entity): ubuff = 0 for gen in entity['genres']: if (gen in movie_genres.keys()): ubuff += movie_genres[gen] ubuff = float(ubuff)/len(entity['genres']) score_now = float(scores[i]['movie_score']['movie_friend_score'])*ubuff title,url,description = youtubeAPI.getVideo(entity) if title: exist_flag = 0 for it in movies10: #TODO O markos to eixe valei se sxolia if ((post_id == it["post_id"]) and (url == it["embed"])) or url == it["embed"]: #edw paizei na prepei na be "or" analoga me to ama 8eloume genika na uparxei to 1 vid 1 fora mono. exist_flag = 1 if (exist_flag == 0): movies10.append({"name":scores[i]['friend_name'],"f_id":scores[i]['facebook_id'],"post_id":post_id,"score":score_now,'picture':picture,'title':title,'embed':url,'description':description,'created':p_time[:10],'genres':entity['genres'],'rated':0}) for item in music: p_time = item['publish_time'] if 'musician' in item['data'] and p_time > str(ex): entity = item['data']['musician']['title'] post_id = item['id'] if post_id not in proc_posts: proc_posts.append(post_id) else: continue check = text_analysis_freebase.search(entity,"music") entity = check if bool(entity): ubuff=0 for gen in entity['genres']: if (gen in movie_genres.keys()): ubuff += movie_genres[gen] ubuff = float(ubuff)/len(entity['genres']) score_now = float(scores[i]['movie_score']['movie_friend_score'])*ubuff title,url,description = youtubeAPI.getVideo(entity) if title: exist_flag = 0 for it in music10: if ((post_id == it["post_id"]) and (url == it["embed"])) or url == it["embed"]: #edw paizei na prepei na be "or" analoga me to ama 8eloume genika na uparxei to 1 vid 1 fora mono. exist_flag = 1 if exist_flag == 0: music10.append({"name":scores[i]['friend_name'],"f_id":scores[i]['facebook_id'],"post_id":post_id,"score":score_now,'picture':picture,'title':title,'embed':url,'description':description,'created':p_time[:10],'genres':entity['genres'],'rated':0}) args['i'] = i args['scores'] = scores args['movie'] = movie_genres args['music'] = music_genres args['pic'] = picture for link in links_filtered: if 'link' in link: proc_posts, movies10, music10 = process_link(link,proc_posts,movies10,music10,args) for status in statuses_filtered: status_flag = 1 if 'message' in status: lme = status['message'] try:#ore lang = classify(lme) if lang[0]!='en': continue except Exception as e: print e continue split_buffer = status['message'].split(" ") for word in split_buffer: if 'http://' in word: link_buffer = {} link_buffer['id'] = status['id'] link_buffer['link'] = word if 'updated_time' in status: link_buffer['created_time'] = status['updated_time'] proc_posts, movies10, music10 = process_link(link_buffer,proc_posts,movies10,music10,args) status_flag = 0 break if status_flag == 1: proc_posts, movies10, music10 = process_status(status,proc_posts,movies10,music10,args) movies10 = sorted(movies10, key=lambda k:k['created']) movies10 = movies10[::-1] music10 = sorted(music10, key=lambda k:k['created']) music10 = music10[::-1] user_coll.update({"id":uid},{"$set":{"movies10":movies10,"music10":music10,"has_lists":1,"proc_posts":proc_posts}})
def readStatusAndCreateLists(uid): client = MongoClient('127.0.0.1') db = client.recommendation_db user_coll = db.users movies10 = [] music10 = [] friends_list = [] query = user_coll.find_one({"id": uid}) user = query acc_token = user['token'] graph = fb.GraphAPI(acc_token) scores = user['scores'] lim = len(scores) music_genres = user['music_genres'] movie_genres = user['movie_genres'] proc_posts = user['proc_posts'] #ore ex = datetime.now() - timedelta(days=14) fields = { "fields": 'links.since(' + str(ex) + ').fields(comments.fields(id,from,message).limit(10),message,link,id,name,created_time,description),name,picture,statuses.since(' + str(ex) + ').fields(comments.fields(id,from,message).limit(10),message,id,updated_time),video.watches.fields(id,data,publish_time,message),music.listens.fields(id,data,publish_time,message)' } for i in range(0, lim): args = {} links_filtered = [] statuses_filtered = [] videos = [] music = [] if not scores[i]['music_score'] and not scores[i]['movie_score']: continue try: statuses = graph.get_object(scores[i]['facebook_id'], **fields) except: print str( scores[i]['friend_name'].encode('utf8')) + " - Graph error\n" continue if 'links' in statuses: links_filtered = statuses['links']['data'] if 'statuses' in statuses: statuses_filtered = statuses['statuses']['data'] if 'video.watches' in statuses: videos = statuses['video.watches']['data'] if 'music.listens' in statuses: music = statuses['music.listens']['data'] picture = statuses['picture']['data']['url'] for video in videos: p_time = video['publish_time'] if 'movie' in video['data'] and p_time > str(ex): entity = video['data']['movie']['title'] post_id = video['id'] if post_id not in proc_posts: proc_posts.append(post_id) else: continue check = text_analysis_freebase.search(entity, "movie") entity = check if bool(entity): ubuff = 0 for gen in entity['genres']: if (gen in movie_genres.keys()): ubuff += movie_genres[gen] ubuff = float(ubuff) / len(entity['genres']) score_now = float( scores[i]['movie_score']['movie_friend_score']) * ubuff title, url, description = youtubeAPI.getVideo(entity) if title: exist_flag = 0 for it in movies10: #TODO O markos to eixe valei se sxolia if ((post_id == it["post_id"]) and (url == it["embed"])) or url == it[ "embed"]: #edw paizei na prepei na be "or" analoga me to ama 8eloume genika na uparxei to 1 vid 1 fora mono. exist_flag = 1 if (exist_flag == 0): movies10.append({ "name": scores[i]['friend_name'], "f_id": scores[i]['facebook_id'], "post_id": post_id, "score": score_now, 'picture': picture, 'title': title, 'embed': url, 'description': description, 'created': p_time[:10], 'genres': entity['genres'], 'rated': 0 }) for item in music: p_time = item['publish_time'] if 'musician' in item['data'] and p_time > str(ex): entity = item['data']['musician']['title'] post_id = item['id'] if post_id not in proc_posts: proc_posts.append(post_id) else: continue check = text_analysis_freebase.search(entity, "music") entity = check if bool(entity): ubuff = 0 for gen in entity['genres']: if (gen in movie_genres.keys()): ubuff += movie_genres[gen] ubuff = float(ubuff) / len(entity['genres']) score_now = float( scores[i]['movie_score']['movie_friend_score']) * ubuff title, url, description = youtubeAPI.getVideo(entity) if title: exist_flag = 0 for it in music10: if ((post_id == it["post_id"]) and (url == it["embed"])) or url == it[ "embed"]: #edw paizei na prepei na be "or" analoga me to ama 8eloume genika na uparxei to 1 vid 1 fora mono. exist_flag = 1 if exist_flag == 0: music10.append({ "name": scores[i]['friend_name'], "f_id": scores[i]['facebook_id'], "post_id": post_id, "score": score_now, 'picture': picture, 'title': title, 'embed': url, 'description': description, 'created': p_time[:10], 'genres': entity['genres'], 'rated': 0 }) args['i'] = i args['scores'] = scores args['movie'] = movie_genres args['music'] = music_genres args['pic'] = picture for link in links_filtered: if 'link' in link: proc_posts, movies10, music10 = process_link( link, proc_posts, movies10, music10, args) for status in statuses_filtered: status_flag = 1 if 'message' in status: lme = status['message'] try: #ore lang = classify(lme) if lang[0] != 'en': continue except Exception as e: print e continue split_buffer = status['message'].split(" ") for word in split_buffer: if 'http://' in word: link_buffer = {} link_buffer['id'] = status['id'] link_buffer['link'] = word if 'updated_time' in status: link_buffer['created_time'] = status[ 'updated_time'] proc_posts, movies10, music10 = process_link( link_buffer, proc_posts, movies10, music10, args) status_flag = 0 break if status_flag == 1: proc_posts, movies10, music10 = process_status( status, proc_posts, movies10, music10, args) movies10 = sorted(movies10, key=lambda k: k['created']) movies10 = movies10[::-1] music10 = sorted(music10, key=lambda k: k['created']) music10 = music10[::-1] user_coll.update({"id": uid}, { "$set": { "movies10": movies10, "music10": music10, "has_lists": 1, "proc_posts": proc_posts } })