def facebook_parse_messages(thread_id, last_message_id, json_string): result = { } try: conversation = database.get_facebook_conversation(thread_id) if not(conversation is None): conversation.set_last_message_id(last_message_id) else: database.add_facebook_conversation(thread_id, last_message_id) messages = json.loads(json_string) links = [] for message in messages: if 'text' in message: if not(':ig:' in message['text']): urls = utils.extract_links(message['text']) date = utils.parse_date(message['date']) title = '' if ':rt:' in message['text']: title = utils.extract_title(message['text'], urls) for i in range(len(urls)): if 'linkbucket.joshasch.com' not in urls[i]: link = database.add_link(urls[i], date) if len(title) > 0: if len(urls) > 1: link = database.edit_title_without_counting(link.id, title + " (" + str(i+1) + ")") else: link = database.edit_title_without_counting(link.id, title) link_dict = { "id": link.id, "title": link.title, "url": link.url, "domain": link.domain, "image_url": link.image_url, "embed_url": link.embed_url, "embed_type": link.embed_type, "screenshot_url": screenshots.get_screenshot(link.url), "timesince": utils.timesince(link.date) } links.append(link_dict) result = { 'success': True, 'thread_id': thread_id, 'links': links } except Exception as error: result = { 'success': False, 'message': str(error) } return jsonify(result)
def add(url, title=''): result = { } if len(url) > 0: if utils.page_exists(url): try: link = database.add_link(url, datetime.now()) if title and (len(title) > 0): database.edit_title_without_counting(link.id, title) if link.embed_type == 0: duration = utils.reading_time(link.word_count) elif link.embed_type == 1: duration = utils.video_duration(link.word_count) else: duration = "" result = { 'success': True, 'valid_url': True, 'link': { 'id': link.id, 'title': link.title, 'url': link.url, 'domain': link.domain, 'embed_type': link.embed_type, 'embed_url': link.embed_url, 'image_url': link.image_url, 'screenshot_url': screenshots.get_screenshot(link.url), 'duration': duration } } except Exception as error: print "(API) Couldn't add link. " + str(type(error)) + " - " + str(error) result = { 'success': False, 'valid_url': True, 'message': str(error) } else: result = { 'success': False, 'valid_url': False, 'no_url': False } else: result = { 'success': False, 'valid_url': False, 'no_url': True} return jsonify(result)