Skip to content

Commit

Permalink
Fix most of the pylint/pyflake issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryuno-Ki committed Mar 6, 2018
1 parent 2b0a406 commit aab996a
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 75 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Expand Up @@ -34,3 +34,22 @@ nosetests.xml
.pydevproject

.env

# Created by https://www.gitignore.io/api/vim

### Vim ###
# swap
.sw[a-p]
.*.sw[a-p]
# session
Session.vim
# temporary
.netrwhist
*~
# auto-generated tag files
tags

# End of https://www.gitignore.io/api/vim#

# Vim Rope
.ropeproject
30 changes: 15 additions & 15 deletions bin/demo.py
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from webmentiontools.urlinfo import UrlInfo
Expand All @@ -7,7 +7,7 @@
# If you have an access token from webmention.io,
# set it here. Some calls require it.

webmention_io_token = None
webmention_io_token = None

wio = WebmentionIO(webmention_io_token)

Expand All @@ -16,21 +16,21 @@
ret = wio.linksToURL(target_url)

if not ret:
print wio.error
print(wio.error)
else:
for link in ret['links']:
print
print 'Webmention.io ID: %s' % link['id']
print ' Source: %s' % link['source']
print ' Verification Date: %s' % link['verified_date']
print('')
print('Webmention.io ID: %s' % link['id'])
print(' Source: %s' % link['source'])
print(' Verification Date: %s' % link['verified_date'])

# Now use UrlInfo to get some more information about the source.
# Most web apps showing webmentions, will probably do something
# Most web apps showing webmentions, will probably do something
# like this.
info = UrlInfo(link['source'])
print ' Source URL info:'
print ' Title: %s' % info.title()
print ' Pub Date: %s' % info.pubDate()
print ' in-reply-to: %s' % info.inReplyTo()
print ' Author image: %s' % info.image()
print ' Snippet: %s' % info.snippetWithLink(target_url)
print(' Source URL info:')
print(' Title: %s' % info.title())
print(' Pub Date: %s' % info.pubDate())
print(' in-reply-to: %s' % info.inReplyTo())
print(' Author image: %s' % info.image())
print(' Snippet: %s' % info.snippetWithLink(target_url))
35 changes: 17 additions & 18 deletions bin/webmention-tools
@@ -1,8 +1,8 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Usage:
webmention-tools [options] send SOURCE TARGET
webmention-tools [options] send SOURCE TARGET
webmention-tools [options] send -f [FILE]
webmention-tools [options] urlinfo URL
webmention-tools -h, --help
Expand All @@ -22,35 +22,35 @@ from pprint import pprint
args = docopt(__doc__, version=__version__)

if args['send'] and args['SOURCE'] and args['TARGET']:
from webmentiontools.send import WebmentionSend
from webmentiontools.send import WebmentionSend
# This is how you can use the library.
# Just initialize WebmentionSend with source, target and call send().
#
print 'Sending webmention from %s to %s... ' % (args['SOURCE'], args['TARGET']),
#
print('Sending webmention from %s to %s... ' % (args['SOURCE'], args['TARGET']))
mention = WebmentionSend(source=args['SOURCE'], target=args['TARGET'])
if mention.send():
print 'Success!'
print('Success!')
else:
print 'Failed'
print('Failed')
if args['--debug']:
pprint(mention.error)

if args['send'] and args['-f']:
import sys
from webmentiontools.send import WebmentionSend
from webmentiontools.send import WebmentionSend
if args['FILE']:
f = open(args['FILE'], 'r')
else:
f = sys.stdin
for line in f:
params=line.strip().split()
params = line.strip().split()
if len(params) == 2:
print 'Sending webmention from %s to %s... ' % (params[0], params[1]) ,
print('Sending webmention from %s to %s... ' % (params[0], params[1]))
mention = WebmentionSend(source=params[0], target=params[1])
if mention.send():
print 'Success!'
print('Success!')
else:
print 'Failed'
print('Failed')
if args['--debug']:
pprint(mention.error)
if f is not sys.stdin:
Expand All @@ -61,10 +61,9 @@ if args['urlinfo']:
url = args['URL']
i = UrlInfo(url)
if i.error:
print 'There was an error getting %s' % url
print('There was an error getting %s' % url)
else:
print 'in-reply-to link: %s' % i.inReplyTo()
print 'publication date: %s' % i.pubDate()
print 'page title: %s' % i.title()
print 'image link: %s' % i.image()

print('in-reply-to link: %s' % i.inReplyTo())
print('publication date: %s' % i.pubDate())
print('page title: %s' % i.title())
print('image link: %s' % i.image())
2 changes: 1 addition & 1 deletion webmentiontools/__init__.py
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__version__ = '0.4.0'
35 changes: 24 additions & 11 deletions webmentiontools/send.py
@@ -1,12 +1,13 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import itertools
import urlparse
import re
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup


class WebmentionSend():

LINK_HEADER_RE = re.compile(
Expand All @@ -30,7 +31,7 @@ def _discoverEndpoint(self):
r = requests.get(self.target_url, verify=False, **self.requests_kwargs)
if r.status_code != 200:
self.error = {
'code':'BAD_TARGET_URL',
'code': 'BAD_TARGET_URL',
'error_description': 'Unable to get target URL.',
'request': 'GET %s' % self.target_url,
'http_status': r.status_code,
Expand All @@ -45,21 +46,25 @@ def _discoverEndpoint(self):
for link in r.headers.get('link', '').split(','):
match = self.LINK_HEADER_RE.search(link)
if match:
self.receiver_endpoint = urlparse.urljoin(self.target_url,
match.group(1))
self.receiver_endpoint = urljoin(self.target_url,
match.group(1))
return

# look in the content
soup = BeautifulSoup(self.html)
tag = None
for name, rel in itertools.product(('link', 'a'), ('webmention', 'http://webmention.org/')):
for name, rel in itertools.product(
('link', 'a'), ('webmention', 'https://webmention.org/')
):
tag = soup.find(name, attrs={'rel': rel})
if tag:
break

if tag and tag.get('href'):
# add the base scheme and host to relative endpoints
self.receiver_endpoint = urlparse.urljoin(self.target_url, tag['href'])
self.receiver_endpoint = urljoin(
self.target_url, tag['href']
)
else:
self.error = {
'code': 'NO_ENDPOINT',
Expand All @@ -68,13 +73,21 @@ def _discoverEndpoint(self):

def _notifyReceiver(self):
payload = {'source': self.source_url, 'target': self.target_url}
headers = {'Accept': '*/*'}
r = requests.post(self.receiver_endpoint, verify=False, data=payload, **self.requests_kwargs)
r = requests.post(
self.receiver_endpoint,
verify=False,
data=payload,
**self.requests_kwargs
)

request_str = 'POST %s (with source=%s, target=%s)' % (self.receiver_endpoint, self.source_url, self.target_url)
request_str = 'POST %s (with source=%s, target=%s)' % (
self.receiver_endpoint,
self.source_url,
self.target_url
)
if r.status_code / 100 != 2:
self.error = {
'code':'RECEIVER_ERROR',
'code': 'RECEIVER_ERROR',
'request': request_str,
'http_status': r.status_code,
}
Expand Down
44 changes: 23 additions & 21 deletions webmentiontools/urlinfo.py
@@ -1,9 +1,10 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup
from urlparse import urljoin
from urllib.parse import urljoin


class UrlInfo():

Expand All @@ -20,17 +21,17 @@ def fetchHTML(self):
if r.status_code != 200:
self.error = True
return
# use apparent_encoding, seems to work better in the cases I tested.
r.encoding = r.apparent_encoding
# use apparent_encoding, seems to work better in the cases I tested.
r.encoding = r.apparent_encoding
self.soup = BeautifulSoup(r.text)

def inReplyTo(self):
if self.data.has_key('in_reply_to'):
if 'in_reply_to' in self.data:
return self.data['in_reply_to']
# Identify first class="u-in-reply-to" or rel="in-reply-to" link
ir2 = self.soup.find('a', attrs={'class':'u-in-reply-to'})
ir2 = self.soup.find('a', attrs={'class': 'u-in-reply-to'})
if not ir2:
ir2 = self.soup.find('a', attrs={'rel':'in-reply-to'})
ir2 = self.soup.find('a', attrs={'rel': 'in-reply-to'})
if not ir2:
return None
ir2_link = ir2['href']
Expand All @@ -39,28 +40,28 @@ def inReplyTo(self):

def pubDate(self):
# Get the time of the reply, if possible
if self.data.has_key('pubDate'):
if 'pubDate' in self.data:
return self.data['pubDate']

ir2_time = self.soup.find(True, attrs={'class':'dt-published'})
if ir2_time and ir2_time.has_attr('datetime') :
ir2_time = self.soup.find(True, attrs={'class': 'dt-published'})
if ir2_time and ir2_time.has_attr('datetime'):
self.data['pubDate'] = ir2_time['datetime']
return ir2_time['datetime']

def title(self):
if self.data.has_key('title'):
if 'title' in self.data:
return self.data['title']
# Get the title
title = self.soup.find('title').string
self.data['title'] = title
return title

def image(self):
if self.data.has_key('image'):
if 'image' in self.data:
return self.data['image']

#Try using p-author
author = self.soup.find(True, attrs={'class':'p-author'})
# Try using p-author
author = self.soup.find(True, attrs={'class': 'p-author'})
if author:
image = author.find('img')
if image:
Expand All @@ -69,15 +70,17 @@ def image(self):
return self.data['image']

# Try using h-card
hcard = self.soup.find(True, attrs={'class':'h-card'})
hcard = self.soup.find(True, attrs={'class': 'h-card'})
if hcard:
image = hcard.find('img', attrs={'class':'u-photo'})
image = hcard.find('img', attrs={'class': 'u-photo'})
if image:
self.data['image'] = urljoin(self.url, image['src'])
return self.data['image']

# Last resort: try using rel="apple-touch-icon-precomposed"
apple_icon = self.soup.find('link', attrs={'rel':'apple-touch-icon-precomposed'})
apple_icon = self.soup.find(
'link', attrs={'rel': 'apple-touch-icon-precomposed'}
)
if apple_icon:
image = apple_icon['href']
if image:
Expand All @@ -92,11 +95,10 @@ def snippetWithLink(self, url):
link = self.soup.find("a", attrs={'href': url})
if link:
for p in link.parents:
if p.name in ('p','div'):
if p.name in ('p', 'div'):
return ' '.join(p.text.split()[0:30])
return None


def linksTo(self, url):
# Check if page links to a specific URL.
# please note that the test is done on the *exact* URL. If
Expand All @@ -111,4 +113,4 @@ def linksTo(self, url):
return False

if __name__ == '__main__':
pass
pass
19 changes: 10 additions & 9 deletions webmentiontools/webmentionio.py
@@ -1,23 +1,24 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests
import json


class WebmentionIO():
# Basic library for http://webmention.io/ API
# Basic library for https://webmention.io/ API

def __init__(self, access_token=None):
self.access_token = access_token
self.api_endpoint = 'http://webmention.io/api'
self.api_endpoint = 'https://webmention.io/api'

def api_links_req(self, k, v):
if k not in (None, 'target', 'domain'):
return False
url = "%s/links" % self.api_endpoint
headers = {'Accept': 'application/json'}
payload = { k:v, 'access_token': self.access_token }
r = requests.get(url, headers=headers, params=payload)
payload = {k: v, 'access_token': self.access_token}
r = requests.get(url, headers=headers, params=payload)
if r.status_code != 200:
self.error = r.text
return False
Expand All @@ -36,7 +37,7 @@ def linksToDomain(self, domain):
return False
else:
return links

def linksToAll(self):
pass

Expand All @@ -47,8 +48,8 @@ def linksToAll(self):
wio = WebmentionIO(webmention_io_token)
ret = wio.linksToURL('http://indiewebcamp.com/webmention')
if not ret:
print wio.error
print(wio.error)
else:
for l in ret['links']:
print l['id'], l['source'], l['verified_date']
"""
print(l['id'], l['source'], l['verified_date'])
"""

0 comments on commit aab996a

Please sign in to comment.