<s> """<STR_LIT>""" <EOL> try : <EOL> from urllib . error import URLError <EOL> from urllib . error import HTTPError <EOL> from urllib . request import urlopen <EOL> from urllib . parse import urlsplit <EOL> except ImportError : <EOL> from urllib2 import urlopen <EOL> from urllib2 import URLError <EOL> from urllib2 import HTTPError <EOL> from urlparse import urlsplit <EOL> from django . utils import six <EOL> from django . utils import timezone <EOL> from django . utils . html import strip_tags <EOL> from django . contrib . sites . models import Site <EOL> from django . core . urlresolvers import resolve <EOL> from django . core . urlresolvers import Resolver404 <EOL> from django . utils . translation import ugettext as _ <EOL> from django . contrib . contenttypes . models import ContentType <EOL> import django_comments as comments <EOL> from django_xmlrpc . decorators import xmlrpc_func <EOL> from bs4 import BeautifulSoup <EOL> from zinnia . models . entry import Entry <EOL> from zinnia . flags import PINGBACK <EOL> from zinnia . flags import get_user_flagger <EOL> from zinnia . signals import pingback_was_posted <EOL> from zinnia . settings import PINGBACK_CONTENT_LENGTH <EOL> UNDEFINED_ERROR = <NUM_LIT:0> <EOL> SOURCE_DOES_NOT_EXIST = <NUM_LIT:16> <EOL> SOURCE_DOES_NOT_LINK = <NUM_LIT> <EOL> TARGET_DOES_NOT_EXIST = <NUM_LIT:32> <EOL> TARGET_IS_NOT_PINGABLE = <NUM_LIT> <EOL> PINGBACK_ALREADY_REGISTERED = <NUM_LIT> <EOL> def generate_pingback_content ( soup , target , max_length , trunc_char = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> link = soup . find ( '<STR_LIT:a>' , href = target ) <EOL> content = strip_tags ( six . text_type ( link . findParent ( ) ) ) <EOL> index = content . index ( link . string ) <EOL> if len ( content ) > max_length : <EOL> middle = max_length // <NUM_LIT:2> <EOL> start = index - middle <EOL> end = index + middle <EOL> if start <= <NUM_LIT:0> : <EOL> end -= start <EOL> extract = content [ <NUM_LIT:0> : end ] <EOL> else : <EOL> extract = '<STR_LIT>' % ( trunc_char , content [ start : end ] ) <EOL> if end < len ( content ) : <EOL> extract += trunc_char <EOL> return extract <EOL> return content <EOL> @ xmlrpc_func ( returns = '<STR_LIT:string>' , args = [ '<STR_LIT:string>' , '<STR_LIT:string>' ] ) <EOL> def pingback_ping ( source , target ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if source == target : <EOL> return UNDEFINED_ERROR <EOL> site = Site . objects . get_current ( ) <EOL> try : <EOL> document = '<STR_LIT>' . join ( map ( <EOL> lambda byte_line : byte_line . decode ( '<STR_LIT:utf-8>' ) , <EOL> urlopen ( source ) . readlines ( ) ) ) <EOL> except ( HTTPError , URLError ) : <EOL> return SOURCE_DOES_NOT_EXIST <EOL> if target not in document : <EOL> return SOURCE_DOES_NOT_LINK <EOL> scheme , netloc , path , query , fragment = urlsplit ( target ) <EOL> if netloc != site . domain : <EOL> return TARGET_DOES_NOT_EXIST <EOL> try : <EOL> view , args , kwargs = resolve ( path ) <EOL> except Resolver404 : <EOL> return TARGET_DOES_NOT_EXIST <EOL> try : <EOL> entry = Entry . published . get ( <EOL> slug = kwargs [ '<STR_LIT>' ] , <EOL> publication_date__year = kwargs [ '<STR_LIT>' ] , <EOL> publication_date__month = kwargs [ '<STR_LIT>' ] , <EOL> publication_date__day = kwargs [ '<STR_LIT>' ] ) <EOL> if not entry . pingbacks_are_open : <EOL> return TARGET_IS_NOT_PINGABLE <EOL> except ( KeyError , Entry . DoesNotExist ) : <EOL> return TARGET_IS_NOT_PINGABLE <EOL> soup = BeautifulSoup ( document , '<STR_LIT>' ) <EOL> title = six . text_type ( soup . find ( '<STR_LIT:title>' ) ) <EOL> title = title and strip_tags ( title ) or _ ( '<STR_LIT>' ) <EOL> description = generate_pingback_content ( soup , target , <EOL> PINGBACK_CONTENT_LENGTH ) <EOL> pingback , created = comments . get_model ( ) . objects . get_or_create ( <EOL> content_type = ContentType . objects . get_for_model ( Entry ) , <EOL> object_pk = entry . pk , user_url = source , site = site , <EOL> defaults = { '<STR_LIT>' : description , '<STR_LIT>' : title , <EOL> '<STR_LIT>' : timezone . now ( ) } ) <EOL> if created : <EOL> pingback . flags . create ( user = get_user_flagger ( ) , flag = PINGBACK ) <EOL> pingback_was_posted . send ( pingback . __class__ , <EOL> pingback = pingback , <EOL> entry = entry ) <EOL> return '<STR_LIT>' % ( source , target ) <EOL> return PINGBACK_ALREADY_REGISTERED <EOL> except : <EOL> return UNDEFINED_ERROR <EOL> @ xmlrpc_func ( returns = '<STR_LIT>' , args = [ '<STR_LIT:string>' ] ) <EOL> def pingback_extensions_get_pingbacks ( target ) : <EOL> """<STR_LIT>""" <EOL> site = Site . objects . get_current ( ) <EOL> scheme , netloc , path , query , fragment = urlsplit ( target ) <EOL> if netloc != site . domain : <EOL> return TARGET_DOES_NOT_EXIST <EOL> try : <EOL> view , args , kwargs = resolve ( path ) <EOL> except Resolver404 : <EOL> return TARGET_DOES_NOT_EXIST <EOL> try : <EOL> entry = Entry . published . get ( <EOL> slug = kwargs [ '<STR_LIT>' ] , <EOL> publication_date__year = kwargs [ '<STR_LIT>' ] , <EOL> publication_date__month = kwargs [ '<STR_LIT>' ] , <EOL> publication_date__day = kwargs [ '<STR_LIT>' ] ) <EOL> except ( KeyError , Entry . DoesNotExist ) : <EOL> return TARGET_IS_NOT_PINGABLE <EOL> return [ pingback . user_url for pingback in entry . pingbacks ] </s>
