本文整理汇总了Python中models.SyndicatedPost.insert_original_blank方法的典型用法代码示例。如果您正苦于以下问题:Python SyndicatedPost.insert_original_blank方法的具体用法?Python SyndicatedPost.insert_original_blank怎么用?Python SyndicatedPost.insert_original_blank使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.SyndicatedPost
的用法示例。
在下文中一共展示了SyndicatedPost.insert_original_blank方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_insert_replaces_blanks
# 需要导入模块: from models import SyndicatedPost [as 别名]
# 或者: from models.SyndicatedPost import insert_original_blank [as 别名]
def test_insert_replaces_blanks(self):
"""Make sure we replace original=None with original=something
when it is discovered"""
# add a blank for the original too
SyndicatedPost.insert_original_blank(
self.source, 'http://original/newly-discovered')
self.assertTrue(
SyndicatedPost.query(
SyndicatedPost.syndication == 'http://silo/no-original',
SyndicatedPost.original == None, ancestor=self.source.key).get())
self.assertTrue(
SyndicatedPost.query(
SyndicatedPost.original == 'http://original/newly-discovered',
SyndicatedPost.syndication == None, ancestor=self.source.key).get())
r = SyndicatedPost.insert(
self.source, 'http://silo/no-original',
'http://original/newly-discovered')
self.assertIsNotNone(r)
self.assertEquals('http://original/newly-discovered', r.original)
# make sure it's in NDB
rs = SyndicatedPost.query(
SyndicatedPost.syndication == 'http://silo/no-original',
ancestor=self.source.key
).fetch()
self.assertEquals(1, len(rs))
self.assertEquals('http://original/newly-discovered', rs[0].original)
self.assertEquals('http://silo/no-original', rs[0].syndication)
# and the blanks have been removed
self.assertFalse(
SyndicatedPost.query(
SyndicatedPost.syndication == 'http://silo/no-original',
SyndicatedPost.original == None, ancestor=self.source.key).get())
self.assertFalse(
SyndicatedPost.query(
SyndicatedPost.original == 'http://original/newly-discovered',
SyndicatedPost.syndication == None, ancestor=self.source.key).get())
示例2: _process_entry
# 需要导入模块: from models import SyndicatedPost [as 别名]
# 或者: from models.SyndicatedPost import insert_original_blank [as 别名]
def _process_entry(source, permalink, feed_entry, refetch, preexisting,
store_blanks=True):
"""Fetch and process an h-entry, saving a new SyndicatedPost to the
DB if successful.
Args:
source:
permalink: url of the unprocessed post
feed_entry: the h-feed version of the h-entry dict, often contains
a partial version of the h-entry at the permalink
refetch: boolean, whether to refetch and process entries we've seen before
preexisting: a list of previously discovered models.SyndicatedPosts
for this permalink
store_blanks: boolean, whether we should store blank SyndicatedPosts when
we don't find a relationship
Returns:
a dict from syndicated url to a list of new models.SyndicatedPosts
"""
# if the post has already been processed, do not add to the results
# since this method only returns *newly* discovered relationships.
if preexisting:
# if we're refetching and this one is blank, do not return.
# if there is a blank entry, it should be the one and only entry,
# but go ahead and check 'all' of them to be safe.
if not refetch:
return {}
synds = [s.syndication for s in preexisting if s.syndication]
if synds:
logging.debug('previously found relationship(s) for original %s: %s',
permalink, synds)
# first try with the h-entry from the h-feed. if we find the syndication url
# we're looking for, we don't have to fetch the permalink
permalink, _, type_ok = util.get_webmention_target(permalink)
usynd = feed_entry.get('properties', {}).get('syndication', [])
if usynd:
logging.debug('u-syndication links on the h-feed h-entry: %s', usynd)
results = _process_syndication_urls(source, permalink, set(
url for url in usynd if isinstance(url, basestring)), preexisting)
success = True
# fetch the full permalink page, which often has more detailed information
if not results:
parsed = None
try:
logging.debug('fetching post permalink %s', permalink)
if type_ok:
resp = util.requests_get(permalink)
resp.raise_for_status()
parsed = mf2py.Parser(url=permalink, doc=resp.text).to_dict()
except AssertionError:
raise # for unit tests
except BaseException:
# TODO limit the number of allowed failures
logging.warning('Could not fetch permalink %s', permalink, exc_info=True)
success = False
if parsed:
syndication_urls = set()
relsynd = parsed.get('rels').get('syndication', [])
if relsynd:
logging.debug('rel-syndication links: %s', relsynd)
syndication_urls.update(url for url in relsynd
if isinstance(url, basestring))
# there should only be one h-entry on a permalink page, but
# we'll check all of them just in case.
for hentry in (item for item in parsed['items']
if 'h-entry' in item['type']):
usynd = hentry.get('properties', {}).get('syndication', [])
if usynd:
logging.debug('u-syndication links: %s', usynd)
syndication_urls.update(url for url in usynd
if isinstance(url, basestring))
results = _process_syndication_urls(
source, permalink, syndication_urls, preexisting)
# detect and delete SyndicatedPosts that were removed from the site
if success:
result_syndposts = itertools.chain(*results.values())
for syndpost in list(preexisting):
if syndpost.syndication and syndpost not in result_syndposts:
logging.info('deleting relationship that disappeared: %s', syndpost)
syndpost.key.delete()
preexisting.remove(syndpost)
if not results:
logging.debug('no syndication links from %s to current source %s.',
permalink, source.label())
results = {}
if store_blanks and not preexisting:
# remember that this post doesn't have syndication links for this
# particular source
logging.debug('saving empty relationship so that %s will not be '
'searched again', permalink)
SyndicatedPost.insert_original_blank(source, permalink)
# only return results that are not in the preexisting list
new_results = {}
for syndurl, syndposts_for_url in results.iteritems():
#.........这里部分代码省略.........
示例3: _process_entry
# 需要导入模块: from models import SyndicatedPost [as 别名]
# 或者: from models.SyndicatedPost import insert_original_blank [as 别名]
def _process_entry(source, permalink, feed_entry, refetch_blanks, preexisting):
"""Fetch and process an h-entry, saving a new SyndicatedPost to the
DB if successful.
Args:
source:
permalink: url of the unprocessed post
feed_entry: the h-feed version of the h-entry dict, often contains
a partial version of the h-entry at the permalink
refetch_blanks: boolean whether we should ignore blank preexisting
SyndicatedPosts
preexisting: a list of previously discovered models.SyndicatedPosts
for this permalink
Returns:
a dict from syndicated url to a list of new models.SyndicatedPosts
"""
results = {}
# if the post has already been processed, do not add to the results
# since this method only returns *newly* discovered relationships.
if preexisting:
# if we're refetching blanks and this one is blank, do not return.
# if there is a blank entry, it should be the one and only entry,
# but go ahead and check 'all' of them to be safe.
if refetch_blanks and all(not p.syndication for p in preexisting):
logging.debug('ignoring blank relationship for original %s', permalink)
else:
return results
# first try with the h-entry from the h-feed. if we find the syndication url
# we're looking for, we don't have to fetch the permalink
usynd = feed_entry.get('properties', {}).get('syndication', [])
logging.debug('u-syndication links on the h-feed h-entry: %s', usynd)
results = _process_syndication_urls(source, permalink, set(
url for url in usynd if isinstance(url, basestring)))
# fetch the full permalink page, which often has more detailed information
if not results:
parsed = None
try:
logging.debug('fetching post permalink %s', permalink)
permalink, _, type_ok = util.get_webmention_target(permalink)
if type_ok:
resp = requests.get(permalink, timeout=HTTP_TIMEOUT)
resp.raise_for_status()
parsed = mf2py.Parser(url=permalink, doc=resp.text).to_dict()
except BaseException:
# TODO limit the number of allowed failures
logging.warning('Could not fetch permalink %s', permalink, exc_info=True)
if parsed:
syndication_urls = set()
relsynd = parsed.get('rels').get('syndication', [])
logging.debug('rel-syndication links: %s', relsynd)
syndication_urls.update(url for url in relsynd
if isinstance(url, basestring))
# there should only be one h-entry on a permalink page, but
# we'll check all of them just in case.
for hentry in (item for item in parsed['items']
if 'h-entry' in item['type']):
usynd = hentry.get('properties', {}).get('syndication', [])
logging.debug('u-syndication links: %s', usynd)
syndication_urls.update(url for url in usynd
if isinstance(url, basestring))
results = _process_syndication_urls(source, permalink,
syndication_urls)
if not results:
logging.debug('no syndication links from %s to current source %s.',
permalink, source.label())
if not preexisting:
# remember that this post doesn't have syndication links for this
# particular source
logging.debug('saving empty relationship so that it %s will not be '
'searched again', permalink)
SyndicatedPost.insert_original_blank(source, permalink)
logging.debug('discovered relationships %s', results)
return results