Skip to content

Commit df2e235

Browse files
committed
Better plugin handling.
1 parent f67de22 commit df2e235

6 files changed

Lines changed: 50 additions & 63 deletions

File tree

bin/resolveimg.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161

6262
i = imageresolver.ImageResolver(**kw_options)
6363
i.register(imageresolver.FileExtensionResolver())
64-
i.register(imageresolver.PluginResolver())
6564
i.register(imageresolver.WebpageResolver(**kw_options))
6665

6766
print i.resolve(url)

imageresolver/__init__.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,24 +149,6 @@ def resolve(self,url,**kwargs):
149149

150150
return None
151151

152-
class PluginResolver(object):
153-
def resolve(self,url,**kwargs):
154-
plugins = {}
155-
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'plugins')
156-
sys.path.insert(0, path)
157-
for plugin_file in os.listdir(path):
158-
filename, extension = os.path.splitext(plugin_file)
159-
if extension == '.py' and filename != '__init__':
160-
mod = __import__(filename)
161-
plugins[filename] = mod.Plugin()
162-
sys.path.pop(0)
163-
164-
for plugin in plugins.values():
165-
image = plugin.get_image(url)
166-
if image:
167-
return image
168-
return None
169-
170152
class WebpageResolver(object):
171153
def __init__(self,**kwargs):
172154
self.load_images = kwargs.get('load_images',True)
@@ -261,12 +243,35 @@ def _score(self,image):
261243

262244
return score
263245

246+
def plugin_resolve(self,url,soup,**kwargs):
247+
plugins = {}
248+
path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'plugins')
249+
sys.path.insert(0, path)
250+
for plugin_file in os.listdir(path):
251+
filename, extension = os.path.splitext(plugin_file)
252+
if extension == '.py' and filename != '__init__':
253+
mod = __import__(filename)
254+
plugins[filename] = mod.Plugin()
255+
sys.path.pop(0)
256+
257+
for plugin in plugins.values():
258+
image = plugin.get_image(url,soup)
259+
if image:
260+
return image
261+
return None
262+
263+
264264
def resolve(self,url,**kwargs):
265265
logger.debug('Resolving as a webpage ' + str(url))
266-
267266
ir = ImageResolver()
268267
content = ir.fetch(url)
269268
soup = BeautifulSoup(content,self.parser)
269+
270+
plugin_image = self.plugin_resolve(url,soup)
271+
272+
if plugin_image:
273+
return plugin_image
274+
270275
images = soup.find_all('img')
271276

272277
candidates = []

imageresolver/plugins/flickr.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
from bs4 import BeautifulSoup
66

77
class Plugin:
8-
def get_image(self, url, **kwargs):
8+
def get_image(self, url, soup):
99
if re.search('http(s*):\/\/www.flickr.com\/photos\/([^\/]*)\/([^\/]*)\/(.*)', url):
1010
logger = logging.getLogger('ImageResolver')
1111
logger.debug('Resolving using plugin ' + str(os.path.basename(__file__)) + ' ' + str(url))
12-
r = requests.get(url)
13-
if r.status_code == 200:
14-
soup = BeautifulSoup(r.text)
15-
tag = soup.find('img', {'class':'main-photo'})
16-
if tag:
17-
return 'https:' + tag['src']
12+
tag = soup.find('img', {'class':'main-photo'})
13+
if tag:
14+
return 'https:' + tag['src']
1815
return None

imageresolver/plugins/imgur.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,24 @@
66
import logging
77

88
class Plugin:
9-
def get_image(self, url, **kwargs):
9+
def get_image(self, url, soup):
1010
if re.search('http(s*):\/\/(i\.|m\.)*imgur.com\/(gallery\/){0,1}(.*)', url):
1111
logger = logging.getLogger('ImageResolver')
1212
logger.debug('Resolving using plugin ' + str(os.path.basename(__file__)) + ' ' + str(url))
1313
parsed = urlparse(url)
1414

1515
if parsed.path[1:8] == 'gallery':
1616
logger.debug('Detected imgur gallery.')
17-
r = requests.get(url)
18-
if r.status_code == 200:
19-
soup = BeautifulSoup(r.text)
20-
tag = soup.find('div', {'id':'1','class':'album-image'})
21-
image = re.findall('i\.imgur.com\/.*\.\w+', str(tag))
22-
if len(image) >= 1:
23-
return 'http://' + image[0]
17+
tag = soup.find('div', {'id':'1','class':'album-image'})
18+
image = re.findall('i\.imgur.com\/.*\.\w+', str(tag))
19+
if len(image) >= 1:
20+
return 'http://' + image[0]
2421

2522
elif parsed.path[0:3] == '/a/':
2623
logger.debug('Detected imgur album.')
27-
r = requests.get(url)
28-
if r.status_code == 200:
29-
soup = BeautifulSoup(r.text)
30-
tag = soup.find('meta',{'name':'twitter:image0:src'})
31-
if tag:
32-
return tag['content']
24+
tag = soup.find('meta',{'name':'twitter:image0:src'})
25+
if tag:
26+
return tag['content']
3327

3428
else:
3529
parsed = urlparse(url)

imageresolver/plugins/instagram.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import re
22
import os
3-
import requests
43
import logging
54
from bs4 import BeautifulSoup
65

76
class Plugin:
8-
def get_image(self, url, **kwargs):
7+
def get_image(self, url, soup):
98
if re.search('http(s*):\/\/instagr(\.am|am\.com)\/p\/([^\/]+)', url):
109
logger = logging.getLogger('ImageResolver')
1110
logger.debug('Resolving using plugin ' + str(os.path.basename(__file__)) + ' ' + str(url))
12-
r = requests.get(url)
13-
if r.status_code == 200:
14-
soup = BeautifulSoup(r.text)
15-
tag = soup.find('meta',{'property':'og:image'})
16-
if tag:
17-
return tag['content']
11+
tag = soup.find('meta',{'property':'og:image'})
12+
if tag:
13+
return tag['content']
1814
return None

imageresolver/plugins/twitter.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
import re
22
import os
3-
import requests
43
import logging
54
from urlparse import urlparse
65
from bs4 import BeautifulSoup
76

87
class Plugin:
9-
def get_image(self, url, **kwargs):
8+
def get_image(self, url, soup):
109
if re.search('http(s*):\/\/(mobile\.|m\.)*twitter.com\/[a-zA-z0-9]*\/status\/\d+', url):
1110
logger = logging.getLogger('ImageResolver')
1211
logger.debug('Resolving using plugin ' + str(os.path.basename(__file__)) + ' ' + str(url))
1312
parsed = urlparse(url)
14-
r = requests.get(url)
15-
if r.status_code == 200:
16-
soup = BeautifulSoup(r.text)
17-
if parsed.netloc.split('.')[0] == 'mobile':
18-
tag = soup.find('img',{'class':'CroppedPhoto-img u-block'})
19-
if tag:
20-
return tag['src']
21-
22-
else:
23-
tag = soup.find('meta',{'property':'og:image'})
24-
if tag:
25-
return tag['content']
13+
if parsed.netloc.split('.')[0] == 'mobile':
14+
tag = soup.find('img',{'class':'CroppedPhoto-img u-block'})
15+
if tag:
16+
return tag['src']
17+
18+
else:
19+
tag = soup.find('meta',{'property':'og:image'})
20+
if tag:
21+
return tag['content']

0 commit comments

Comments
 (0)