def redirect_back(url, source_domain):
 parse_data = urlparse(url)
 domain = parse_data.netloc
 query = parse_data.query
 if ((source_domain in domain) or (domain in source_domain)):
  return url
 query_item = parse_qs(query)
 if query_item.get('url'):
  return query_item['url'][0]
 return url

def redirect_back ( url , source_domain ) : parse_data = urlparse ( url ) domain = parse_data . netloc query = parse_data . query if ( ( source_domain in domain ) or ( domain in source_domain ) ) : return url query_item = parse_qs ( query ) if query_item . get ( 'url' ) : return query_item [ 'url' ] [ 0 ] return url
some sites like pinterest have apis that cause news args to direct to their site with the real news url as a get param .