Skip to content

Commit b377ca1

Browse files
committed
Added opengraph plugin.
1 parent 52ec938 commit b377ca1

4 files changed

Lines changed: 27 additions & 16 deletions

File tree

CHANGES.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,5 @@ v0.3, 2015-07-03 --
7575
- Fixed some bugs
7676
- changed data files installation directory after installing
7777
from setup.py
78-
78+
- Added opengraph plugin
7979

README.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,6 @@ By default this exception is skipped and logged but can be enabled with "skip_fe
129129
TODO
130130
-----------------
131131

132-
Still missing the following resolvers:
133-
134-
135-
* OpengraphResolver()
136-
137-
138-
I have no plans to implement a 9gag resolver.
139-
140132
Need to implement better caching. Future plan is to include a configurable cache method so images seen across sessions can be cached for better performance
141133

142134

docs/README.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,6 @@ By default this exception is skipped and logged but can be enabled with "skip_fe
129129
TODO
130130
-----------------
131131

132-
Still missing the following resolvers:
133-
134-
* OpengraphResolver()
135-
136-
137-
I have no plans to implement a 9gag resolver.
138-
139132
Need to implement better caching. Future plan is to include a configurable cache method so images seen across sessions can be cached for better performance
140133

141134

imageresolver/plugins/opengraph.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import re
2+
import os
3+
import logging
4+
from bs4 import BeautifulSoup
5+
6+
class Plugin:
7+
def get_image(self, url, soup):
8+
9+
ogtags = [{'attribute':'property', 'name':'og:image', 'value':'content'},
10+
{'attribute':'rel', 'name':'image_src', 'value':'href'},
11+
{'attribute':'name', 'name':'twitter:image', 'value':'value'},
12+
{'attribute':'name', 'name':'twitter:image', 'value':'content'}]
13+
14+
ogimages = []
15+
16+
for ogtag in ogtags:
17+
tags = soup.find_all('meta', {ogtag['attribute']:ogtag['name']})
18+
ogimages = ogimages + [image[ogtag['value']] for image in tags]
19+
20+
if len(ogimages) >= 1:
21+
logger = logging.getLogger('ImageResolver')
22+
logger.debug('Resolving using plugin ' + str(os.path.basename(__file__)) + ' ' + str(url))
23+
return ogimages[0]
24+
25+
return None
26+

0 commit comments

Comments
 (0)