Skip to content

Commit a4460aa

Browse files
committed
plugin cleanup
1 parent 0564005 commit a4460aa

5 files changed

Lines changed: 74 additions & 71 deletions

File tree

imageresolver/abpy

Submodule abpy updated from 3391402 to bf6c9d8

imageresolver/plugins/flickr.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import re
22
import os
3-
import requests
43
import logging
5-
from bs4 import BeautifulSoup
64

7-
class Plugin:
8-
def get_image(self, url, soup):
5+
6+
class Plugin(object):
7+
@staticmethod
8+
def get_image(url, soup):
99
if re.search('http(s*):\/\/www.flickr.com\/photos\/([^\/]*)\/([^\/]*)\/(.*)', url):
1010
logger = logging.getLogger('ImageResolver')
11-
logger.debug('Resolving using plugin ' + str(os.path.basename(__file__)) + ' ' + str(url))
12-
tag = soup.find('img', {'class':'main-photo'})
11+
logger.debug('Resolving using plugin {} {}'.format(os.path.basename(__file__), url))
12+
tag = soup.find('img', {'class': 'main-photo'})
1313
if tag:
14-
return 'https:' + tag['src']
14+
return 'https: {}'.format(tag['src'])
1515
return None

imageresolver/plugins/imgur.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
import re
22
import os
3-
import requests
4-
from bs4 import BeautifulSoup
5-
from urlparse import urlparse
63
import logging
74

8-
class Plugin:
5+
from future.standard_library import install_aliases
6+
install_aliases()
7+
from urllib.parse import urlparse
8+
9+
10+
class Plugin(object):
911
def get_image(self, url, soup):
1012
if re.search('http(s*):\/\/(i\.|m\.)*imgur.com\/(gallery\/){0,1}(.*)', url):
1113
logger = logging.getLogger('ImageResolver')
12-
logger.debug('Resolving using plugin ' + str(os.path.basename(__file__)) + ' ' + str(url))
14+
logger.debug('Resolving using plugin {} {}'.format(os.path.basename(__file__), url))
1315
parsed = urlparse(url)
1416

1517
if parsed.path[1:8] == 'gallery':
1618
logger.debug('Detected imgur gallery.')
17-
tag = soup.find('div', {'id':'1','class':'album-image'})
19+
tag = soup.find('div', {'id': '1', 'class': 'album-image'})
1820
image = re.findall('i\.imgur.com\/.*\.\w+', str(tag))
1921
if len(image) >= 1:
20-
return 'http://' + image[0]
22+
return 'https://' + image[0]
2123

2224
elif parsed.path[0:3] == '/a/':
2325
logger.debug('Detected imgur album.')
24-
tag = soup.find('meta',{'name':'twitter:image0:src'})
26+
tag = soup.find('meta', {'name': 'twitter:image0:src'})
2527
if tag:
2628
return tag['content']
2729

2830
else:
2931
parsed = urlparse(url)
3032
if re.search('imgur.com(:80)*', parsed.netloc) and os.path.basename(parsed.path):
31-
return 'http://i.imgur.com/' + os.path.basename(parsed.path) + '.jpg'
33+
return 'https://i.imgur.com/' + os.path.basename(parsed.path) + '.jpg'
3234
return None
3335

imageresolver/plugins/opengraph.py

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
11
import re
22
import os
33
import logging
4-
from bs4 import BeautifulSoup
54
from operator import itemgetter
65

7-
class Plugin:
8-
def get_image(self, url, soup):
9-
10-
ogtags = [{'type':'facebook','attribute':'property', 'name':'og:image', 'value':'content'},
11-
{'type':'facebook','attribute':'rel', 'name':'image_src', 'value':'href'},
12-
{'type':'twitter','attribute':'name', 'name':'twitter:image', 'value':'value'},
13-
{'type':'twitter','attribute':'name', 'name':'twitter:image', 'value':'content'},
14-
{'type':'twitter','attribute':'property', 'name':'twitter:image', 'value':'content'},
15-
{'type':'image','attribute':'itemprop', 'name':'image', 'value':'content'}]
16-
17-
ogimages = []
18-
19-
for ogtag in ogtags:
20-
tags = soup.find_all('meta', {ogtag['attribute']:ogtag['name']})
21-
if tags != []:
22-
for image in tags:
23-
try:
24-
ogimages = ogimages + [{'url':image[ogtag['value']], 'type':ogtag['type'], 'score':0} for image in tags]
25-
except KeyError as e:
26-
pass
27-
28-
ogimages_len = len(ogimages)
29-
30-
# if more than 1 image, score and return the best one
31-
if ogimages_len >= 1:
32-
if ogimages_len == 1:
33-
logger = logging.getLogger('ImageResolver')
34-
logger.debug('Resolving using plugin ' + str(os.path.basename(__file__)) + ' ' + str(url))
35-
resolved_image = ogimages[0]['url']
36-
else:
37-
for image in ogimages:
38-
if re.search('(large|big)', image['url'], re.IGNORECASE):
39-
image['score'] += 1
40-
if image['type'] == 'twitter':
41-
image['score'] += 1
42-
43-
ogimages.sort(key=itemgetter('score'), reverse=True)
44-
resolved_image = ogimages[0]['url']
45-
46-
if not re.search('^https?:', resolved_image):
47-
if resolved_image.startswith('//'):
48-
return 'http:' + resolved_image
49-
else:
50-
return resolved_image
51-
52-
53-
return None
546

7+
class Plugin(object):
8+
def get_image(self, url, soup):
9+
10+
ogtags = [{'type': 'facebook', 'attribute': 'property', 'name': 'og:image', 'value': 'content'},
11+
{'type': 'facebook', 'attribute': 'rel', 'name': 'image_src', 'value': 'href'},
12+
{'type': 'twitter', 'attribute': 'name', 'name': 'twitter:image', 'value': 'value'},
13+
{'type': 'twitter', 'attribute': 'name', 'name': 'twitter:image', 'value': 'content'},
14+
{'type': 'twitter', 'attribute': 'property', 'name': 'twitter:image', 'value': 'content'},
15+
{'type': 'image', 'attribute': 'itemprop', 'name': 'image', 'value': 'content'}]
16+
17+
ogimages = []
18+
19+
for ogtag in ogtags:
20+
tags = soup.find_all('meta', {ogtag['attribute']: ogtag['name']})
21+
if tags:
22+
try:
23+
ogimages.extend([{'url': image[ogtag['value']], 'type': ogtag['type'], 'score': 0}
24+
for image in tags])
25+
except KeyError as e:
26+
pass
27+
28+
ogimages_len = len(ogimages)
29+
30+
# if more than 1 image, score and return the best one
31+
if ogimages_len >= 1:
32+
if ogimages_len == 1:
33+
logger = logging.getLogger('ImageResolver')
34+
logger.debug('Resolving using plugin ' + str(os.path.basename(__file__)) + ' ' + str(url))
35+
resolved_image = ogimages[0]['url']
36+
else:
37+
for image in ogimages:
38+
if re.search('(large|big)', image['url'], re.IGNORECASE):
39+
image['score'] += 1
40+
if image['type'] == 'twitter':
41+
image['score'] += 1
42+
43+
ogimages.sort(key=itemgetter('score'), reverse=True)
44+
resolved_image = ogimages[0]['url']
45+
46+
if not re.search('^https?:', resolved_image):
47+
if resolved_image.startswith('//'):
48+
return 'https:' + resolved_image
49+
else:
50+
return resolved_image
51+
52+
return None

imageresolver/plugins/twitter.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import re
22
import os
33
import logging
4-
from urlparse import urlparse
5-
from bs4 import BeautifulSoup
64

7-
class Plugin:
5+
from future.standard_library import install_aliases
6+
install_aliases()
7+
from urllib.parse import urlparse
8+
9+
10+
class Plugin(object):
811
def get_image(self, url, soup):
912
if re.search('http(s*):\/\/(mobile\.|m\.)*twitter.com\/[a-zA-z0-9]*\/status\/\d+', url):
1013
logger = logging.getLogger('ImageResolver')
11-
logger.debug('Resolving using plugin ' + str(os.path.basename(__file__)) + ' ' + str(url))
14+
logger.debug('Resolving using plugin ' + str(os.path.basename(__file__)) + ' ' + str(url))
1215
parsed = urlparse(url)
1316
if parsed.netloc.split('.')[0] == 'mobile':
14-
tag = soup.find('img',{'class':'CroppedPhoto-img u-block'})
17+
tag = soup.find('img', {'class': 'CroppedPhoto-img u-block'})
1518
if tag:
1619
return tag['src']
1720

1821
else:
19-
tag = soup.find('meta',{'property':'og:image'})
22+
tag = soup.find('meta', {'property': 'og:image'})
2023
if tag:
2124
return tag['content']

0 commit comments

Comments
 (0)