def test_tokenize_ignore_cc_tlds(self): text = 'Despite names,\nind.ie&indie.vc are NOT #indieweb @indiewebcamp\nindiewebcamp.com/2014-review#Indie_Term_Re-use\n@iainspad @sashtown @thomatronic' self.assertEqual([ brevity.Token(tag='text', content='Despite names,\nind.ie&indie.vc are NOT #indieweb @indiewebcamp\n'), brevity.Token(tag='link', content='indiewebcamp.com/2014-review#Indie_Term_Re-use'), brevity.Token(tag='text', content='\n@iainspad @sashtown @thomatronic') ], brevity.tokenize(text, skip_bare_cc_tlds=True))
def test_tokenize_ignore_email(self): text = 'this should [email protected]' self.assertEqual([brevity.Token(tag='text', content=text)], brevity.tokenize(text))
def publish(site): title = request.form.get('name') content = request.form.get('content[value]') or request.form.get('content') permalink = request.form.get('url') photo_file = util.get_first( util.get_possible_array_value(request.files, 'photo')) photo_url = util.get_first( util.get_possible_array_value(request.form, 'photo')) video_file = util.get_first( util.get_possible_array_value(request.files, 'video')) video_url = util.get_first( util.get_possible_array_value(request.form, 'video')) location = request.form.get('location') post_data = {'access_token': site.account.token} post_files = None api_endpoint = 'https://graph.facebook.com/v2.5/me/feed' fburl_separator = 'posts' message = (content if not permalink else '({})'.format(permalink) if not content else '{} ({})'.format(content, permalink)) if video_file or video_url: if video_file: post_files = { 'source': (video_file.filename, video_file.stream, video_file.content_type or 'video/mp4') } elif video_url: post_data['url'] = video_url post_data['title'] = title post_data['description'] = message api_endpoint = 'https://graph-video.facebook.com/v2.5/me/videos' fburl_separator = 'videos' elif photo_file or photo_url: if photo_file: post_files = {'source': photo_file} elif photo_url: post_data['url'] = photo_url post_data['caption'] = message # TODO support album id as alternative to 'me' # TODO upload to "Timeline photos" album by default api_endpoint = 'https://graph.facebook.com/v2.5/me/photos' fburl_separator = 'photos' elif title and content: # looks like an article -- include the permalink as a 'link' # instead of inline post_data['message'] = '{}\n\n{}'.format(title, content) post_data['link'] = permalink post_data['name'] = title elif content: post_data['message'] = message tokens = brevity.tokenize(content) # linkify the first url in the message linktok = next((tok for tok in tokens if tok.tag == 'link'), None) if linktok: post_data['link'] = linktok.content else: return util.make_publish_error_response( 'Request must contain a photo, video, or content') # posting Location to Facebook is disabled for now -- just # searching lat/long does not get us close enough to assume we # have the correct place. if False and location: if location.isnumeric(): post_data['place'] = location else: place_name = (request.form.get('place-name') or request.form.get('place_name')) lat, lng = util.parse_geo_uri(location) if lat and lng: current_app.logger.debug('Search FB for a place, %s at %s, %s', place_name, lat, lng) r = requests.get('https://graph.facebook.com/v2.5/search', params=util.trim_nulls({ 'type': 'place', 'center': '%s,%s' % (lat, lng), 'distance': '500', 'q': place_name, 'access_token': site.account.token, })) if r.status_code != 200: current_app.logger.warning( 'FB place search failed with response %r: %r', r, r.text) else: places = r.json().get('data', []) if not places: # TODO consider searching without a place name? current_app.logger.warning( 'FB no resuts for place %s at %s, %s ', place_name, lat, lng) else: current_app.logger.debug('Found FB place: %s (%s)', places[0].get('name'), places[0].get('id')) post_data['place'] = places[0].get('id') post_data = util.trim_nulls(post_data) current_app.logger.debug('Publishing to facebook %s: data=%s, files=%s', api_endpoint, post_data, post_files) r = requests.post(api_endpoint, data=post_data, files=post_files) # need Web Canvas permissions to do this, which I am too lazy to apply for # if r.status_code == 400: # error_data = r.json().get('error', {}) # code = error_data.get('code') # subcode = error_data.get('subcode') # # token is expired or otherwise invalid # if code == 190: # send_token_expired_notification( # site.account.user_id, # "silo.pub's Facebook access token has expired. Click the " # "Facebook button on silo.pub's homepage to renew.", # 'https://silo.pub/') if r.status_code // 100 != 2: return util.wrap_silo_error_response(r) resp_data = r.json() userid = '' fbid = resp_data.get('id') or resp_data.get('post_id') split = fbid.split('_') if len(split) == 2: userid, fbid = split return util.make_publish_success_response( 'https://www.facebook.com/{}/{}/{}'.format( site.account.username or userid, fburl_separator, fbid), data=resp_data)
def test_tokenize_ignore_html(self): text = 'this should <a href="http://example.com">not be linkified</a>' self.assertEqual([brevity.Token(tag='text', content=text)], brevity.tokenize(text))
def publish(site): title = request.form.get('name') content = request.form.get('content[value]') or request.form.get('content') permalink = request.form.get('url') photo_file = util.get_first(util.get_possible_array_value(request.files, 'photo')) photo_url = util.get_first(util.get_possible_array_value(request.form, 'photo')) video_file = util.get_first(util.get_possible_array_value(request.files, 'video')) video_url = util.get_first(util.get_possible_array_value(request.form, 'video')) location = request.form.get('location') post_data = {'access_token': site.account.token} post_files = None api_endpoint = 'https://graph.facebook.com/v2.5/me/feed' fburl_separator = 'posts' message = ( content if not permalink else '({})'.format(permalink) if not content else '{} ({})'.format(content, permalink)) if video_file or video_url: if video_file: post_files = {'source': (video_file.filename, video_file.stream, video_file.content_type or 'video/mp4')} elif video_url: post_data['url'] = video_url post_data['title'] = title post_data['description'] = message api_endpoint = 'https://graph-video.facebook.com/v2.5/me/videos' fburl_separator = 'videos' elif photo_file or photo_url: if photo_file: post_files = {'source': photo_file} elif photo_url: post_data['url'] = photo_url post_data['caption'] = message # TODO support album id as alternative to 'me' # TODO upload to "Timeline photos" album by default api_endpoint = 'https://graph.facebook.com/v2.5/me/photos' fburl_separator = 'photos' elif title and content: # looks like an article -- include the permalink as a 'link' # instead of inline post_data['message'] = '{}\n\n{}'.format(title, content) post_data['link'] = permalink post_data['name'] = title elif content: post_data['message'] = message tokens = brevity.tokenize(content) # linkify the first url in the message linktok = next((tok for tok in tokens if tok.tag == 'link'), None) if linktok: post_data['link'] = linktok.content else: return util.make_publish_error_response( 'Request must contain a photo, video, or content') # posting Location to Facebook is disabled for now -- just # searching lat/long does not get us close enough to assume we # have the correct place. if False and location: if location.isnumeric(): post_data['place'] = location else: place_name = (request.form.get('place-name') or request.form.get('place_name')) lat, lng = util.parse_geo_uri(location) if lat and lng: current_app.logger.debug('Search FB for a place, %s at %s, %s', place_name, lat, lng) r = requests.get( 'https://graph.facebook.com/v2.5/search', params=util.trim_nulls({ 'type': 'place', 'center': '%s,%s' % (lat, lng), 'distance': '500', 'q': place_name, 'access_token': site.account.token, })) if r.status_code != 200: current_app.logger.warning( 'FB place search failed with response %r: %r', r, r.text) else: places = r.json().get('data', []) if not places: # TODO consider searching without a place name? current_app.logger.warning( 'FB no resuts for place %s at %s, %s ', place_name, lat, lng) else: current_app.logger.debug( 'Found FB place: %s (%s)', places[0].get('name'), places[0].get('id')) post_data['place'] = places[0].get('id') post_data = util.trim_nulls(post_data) current_app.logger.debug( 'Publishing to facebook %s: data=%s, files=%s', api_endpoint, post_data, post_files) r = requests.post(api_endpoint, data=post_data, files=post_files) # need Web Canvas permissions to do this, which I am too lazy to apply for # if r.status_code == 400: # error_data = r.json().get('error', {}) # code = error_data.get('code') # subcode = error_data.get('subcode') # # token is expired or otherwise invalid # if code == 190: # send_token_expired_notification( # site.account.user_id, # "silo.pub's Facebook access token has expired. Click the " # "Facebook button on silo.pub's homepage to renew.", # 'https://silo.pub/') if r.status_code // 100 != 2: return util.wrap_silo_error_response(r) resp_data = r.json() userid = '' fbid = resp_data.get('id') or resp_data.get('post_id') split = fbid.split('_') if len(split) == 2: userid, fbid = split return util.make_publish_success_response( 'https://www.facebook.com/{}/{}/{}'.format( site.account.username or userid, fburl_separator, fbid), data=resp_data)