def make_image_object_from_url(image_url): import urllib2 import socket from parse_uri import ParseUri from PIL import Image from StringIO import StringIO from django.contrib.sites.models import Site # parse url p = ParseUri() parsed_url = p.parse(image_url) # handle absolute and relative urls, Assuming http for now. if not parsed_url.host: parsed_url = p.parse(Site.objects.get_current().domain + image_url) parsed_url.protocol = "http" parsed_url.source = parsed_url.protocol + "://" + parsed_url.source request = urllib2.Request(parsed_url.source) request.add_header('User-Agent', settings.TENDENCI_USER_AGENT) opener = urllib2.build_opener() # make image object try: socket.setdefaulttimeout(1.5) data = opener.open(request).read() # get data im = Image.open(StringIO(data)) except: im = None return im
def resolve_artifact(uri): uri_parser = ParseUri() parsed_uri = uri_parser.parse(uri) if parsed_uri.protocol == "ssh": return SSHArtifact(parsed_uri.host, parsed_uri.path) elif parsed_uri.protocol == "http": return HTTPArtifact(parsed_uri.source) elif parsed_uri.protocol == "s3": return S3Artifact(parsed_uri.host, parsed_uri.path) elif parsed_uri.protocol == "mysql": return MysqlArtifact(uri) else: return FileArtifact(uri)
def run(self, file_name, user, **kwargs): """ Parse the given xml file using BeautifulSoup. Save all Article, Redirect and Page objects. """ f = open(file_name, 'r') xml = f.read() f.close() uri_parser = ParseUri() soup = BeautifulStoneSoup(xml) items = soup.findAll('item') for item in items: post_type = item.find('wp:post_type').string post_status = item.find('wp:status').string if post_type == 'attachment': get_media(item, uri_parser, user) # Note! This script assumes all the attachments come before # posts and pages in the xml. If this ends up changing, # do two loops, one with attachments and the second with posts and pages. elif post_type == 'post' and post_status == 'publish': get_posts(item, uri_parser, user) elif post_type == 'page' and post_status == 'publish': get_pages(item, uri_parser, user) if user.email: send_mail('Blog Import Success!', 'Your blog has been imported! Thank you for waiting.', settings.DEFAULT_FROM_EMAIL, [user.email], fail_silently=False)
def __init__(self, uri): uri_parser = ParseUri() parsed_uri = uri_parser.parse(uri) params = {} if parsed_uri.host: params['host'] = parsed_uri.host if parsed_uri.user: params['user'] = parsed_uri.user if parsed_uri.password: params['passwd'] = urllib.unquote(parsed_uri.password) if parsed_uri.port: params['port'] = parsed_uri.port database, query = parsed_uri.path.split('/')[1:] params['db'] = database self.connection_params = params self.query = query self.parsed_uri = parsed_uri
def get_absolute_url(self): from django.core.urlresolvers import reverse url = reverse("story", args=[self.pk]) if self.full_story_link: url = self.full_story_link parsed_url = ParseUri().parse(url) if not parsed_url.protocol: # if relative URL url = '%s%s' % (get_setting('site', 'global', 'siteurl'), url) return url
def url_exists(url): import socket import httplib from parse_uri import ParseUri from django.contrib.sites.models import Site # parse url p = ParseUri() parsed_url = p.parse(url) # doesn't have a host so it's relative to the website if not parsed_url.host: parsed_url = p.parse(Site.objects.get_current().domain + url) conn = httplib.HTTPConnection(parsed_url.authority) conn.request("HEAD", parsed_url.path) try: socket.setdefaulttimeout(1.5) response = conn.getresponse() if response.status == 200: return True except: return False
def run(self, file_name, user, **kwargs): """ Parse the given xml file using BeautifulSoup. Save all Article, Redirect and Page objects. """ f = open(file_name, 'r') xml = f.read() f.close() uri_parser = ParseUri() soup = BeautifulStoneSoup(xml) items = soup.findAll('item') for item in items: post_type = item.find('wp:post_type').string post_status = item.find('wp:status').string if post_type == 'attachment': get_media(item, uri_parser, user) # Note! This script assumes all the attachments come before # posts and pages in the xml. If this ends up changing, # do two loops, one with attachments and the second with posts and pages. elif post_type == 'post' and post_status == 'publish': get_posts(item, uri_parser, user) elif post_type == 'page' and post_status == 'publish': get_pages(item, uri_parser, user) if user.email: context_instance = { 'SITE_GLOBAL_SITEDISPLAYNAME': get_setting('site', 'global', 'sitedisplayname'), 'SITE_GLOBAL_SITEURL': get_setting('site', 'global', 'siteurl'), } subject = ''.join( render_to_string(('notification/wp_import/short.txt'), context_instance).splitlines()) body = render_to_string(('notification/wp_import/full.html'), context_instance) #send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, [user.email], fail_silently=False) email = EmailMessage(subject, body, settings.DEFAULT_FROM_EMAIL, [user.email]) email.content_subtype = 'html' email.send(fail_silently=True)