Skip to content

Fix invalid URL in test.py and Suppory Python3.4 #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions opengraph/opengraph.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# encoding: utf-8

import re
import urllib2

try:
import urllib2
except ImportError:
from urllib import request as urllib2

try:
from bs4 import BeautifulSoup
except ImportError:
Expand Down Expand Up @@ -59,7 +64,7 @@ def parser(self, html):
else:
doc = html
ogs = doc.html.head.findAll(property=re.compile(r'^og'))
for og in ogs:
for og in ogs:
if og.has_attr(u'content'):
self[og[u'property'][3:]]=og[u'content']
# Couldn't fetch all attrs from og tags, try scraping body
Expand All @@ -72,7 +77,13 @@ def parser(self, html):
pass

def valid_attr(self, attr):
return hasattr(self, attr) and len(self[attr]) > 0
has_attr_result = False
try:
has_attr_result = hasattr(self, attr)
except Exception as e:
has_attr_result = False

return has_attr_result and len(self[attr]) > 0

def is_valid(self):
return all([self.valid_attr(attr) for attr in self.required_attrs])
Expand Down
10 changes: 5 additions & 5 deletions opengraph/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
class test(unittest.TestCase):

def test_url(self):
data = opengraph.OpenGraph(url='http://vimeo.com/896837')
self.assertEqual(data['url'], 'http://vimeo.com/896837')
data = opengraph.OpenGraph(url='https://vimeo.com/896837')
self.assertEqual(data['url'], 'https://vimeo.com/896837')

def test_isinstace(self):
data = opengraph.OpenGraph()
Expand All @@ -31,17 +31,17 @@ def test_to_html(self):
self.assertTrue(og.to_html())

def test_to_json(self):
og = opengraph.OpenGraph(url='http://www.youtube.com/watch?v=XAyNT2bTFuI')
og = opengraph.OpenGraph(url='https://www.youtube.com/watch?v=XAyNT2bTFuI')
self.assertTrue(og.to_json())
self.assertTrue(isinstance(og.to_json(),str))

def test_no_json(self):
opengraph.import_json = False
og = opengraph.OpenGraph(url='http://grooveshark.com')
og = opengraph.OpenGraph(url='http://ogp.me')
self.assertEqual(og.to_json(),"{'error':'there isn't json module'}")

def test_is_valid(self):
og = opengraph.OpenGraph(url='http://grooveshark.com')
og = opengraph.OpenGraph(url='http://ogp.me')
self.assertTrue(og.is_valid())


Expand Down