Convert python source to version 3

This commit is contained in:
Iron Blogger Bot 2019-03-08 13:43:50 +01:00
parent d12e511c1b
commit 2709e81679
6 changed files with 56 additions and 49 deletions

View File

@ -1,44 +1,49 @@
#!/usr/bin/python #!/usr/bin/python3
from lxml import html from lxml import html
import yaml import yaml
import sys import sys
import urllib2 import urllib.request
import urlparse import urllib.parse
with open('bloggers.yml') as f: with open('bloggers.yml') as f:
users = yaml.safe_load(f.read()) users = yaml.safe_load(f.read())
def fetch_links(url): def fetch_links(url):
print >>sys.stderr, "Looking for feeds in %s" % (url,) print("Looking for feeds in %s" % (url,), file=sys.stderr)
tree = html.fromstring(urllib2.urlopen(url).read()) try:
links = tree.xpath( tree = html.document_fromstring(urllib.request.urlopen(url).read())
'//link[@rel="alternate"][contains(@type, "rss") or ' + links = tree.xpath(
'contains(@type, "atom") or contains(@type, "rdf")]') '//link[@rel="alternate"][contains(@type, "rss") or ' +
candidates = [l for l in links if 'contains(@type, "atom") or contains(@type, "rdf")]')
candidates = [l for l in links if
'atom' in l.attrib['type'] and 'atom' in l.attrib['type'] and
'comments' not in l.attrib['href'].lower() and 'comments' not in l.attrib['href'].lower() and
'comments' not in l.attrib.get('title','')] 'comments' not in l.attrib.get('title','')]
except:
candidates = []
links = []
if candidates: if candidates:
return candidates[0].attrib['href'] return candidates[0].attrib['href']
elif links: elif links:
return links[0].attrib['href'] return links[0].attrib['href']
else: else:
print >>sys.stderr, "No link found for %s" % (url,) print("No link found for %s" % (url,), file=sys.stderr)
return None return None
for (name, u) in users.items(): for (name, u) in list(users.items()):
print("Processing user %s" % (name,), file=sys.stderr)
for e in u['links']: for e in u['links']:
(title, url) = e[1:3] (title, url) = e[1:3]
try: try:
e[1] = e[1].strip() e[1] = e[1].strip()
except: except:
e[1] = e[1] e[1] = e[1]
if len(e) == 4: if len(e) == 4:
continue continue
link = fetch_links(url) link = fetch_links(url)
if link: if link:
if not link.startswith('http:'): if not link.startswith('http:'):
link = urlparse.urljoin(url, link) link = urllib.parse.urljoin(url, link)
e.append(link) e.append(link)
with open('bloggers.yml', 'w') as f: with open('bloggers.yml', 'w') as f:

View File

@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/python3
# This Python file uses the following encoding: utf-8 # This Python file uses the following encoding: utf-8
import yaml import yaml
from dateutil.parser import parse from dateutil.parser import parse
@ -29,6 +29,7 @@ def get_balance(acct):
def get_debts(): def get_debts():
p = subprocess.Popen(['ledger', '-f', os.path.join(HERE, 'ledger'), p = subprocess.Popen(['ledger', '-f', os.path.join(HERE, 'ledger'),
'-n', 'balance', 'Pool:Owed:'], '-n', 'balance', 'Pool:Owed:'],
universal_newlines=True,
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
(out, _) = p.communicate() (out, _) = p.communicate()
debts = [] debts = []
@ -48,7 +49,7 @@ def parse_skip(rec):
out = [] out = []
for s in spec: for s in spec:
if isinstance(s, list): if isinstance(s, list):
out.append(map(to_week_num, s)) out.append(list(map(to_week_num, s)))
else: else:
out.append(to_week_num(s)) out.append(to_week_num(s))
return out return out
@ -72,7 +73,7 @@ def render_template(path, week=None, **kwargs):
else: else:
week = START week = START
week = (week - START).days / 7 week = int( (week - START).days / 7)
week_start = START + (week * datetime.timedelta(7)) week_start = START + (week * datetime.timedelta(7))
week_end = START + ((week + 1) * datetime.timedelta(7)) week_end = START + ((week + 1) * datetime.timedelta(7))
@ -84,7 +85,7 @@ def render_template(path, week=None, **kwargs):
class User(object): class User(object):
pass pass
for (un, rec) in users.items(): for (un, rec) in list(users.items()):
u = User() u = User()
u.username = un u.username = un
u.name = rec['name'] u.name = rec['name']
@ -97,8 +98,8 @@ def render_template(path, week=None, **kwargs):
u.stop = rec.get('stop') u.stop = rec.get('stop')
u.skip = parse_skip(rec) u.skip = parse_skip(rec)
u.posts = report.get(un, {}) u.posts = report.get(un, {})
u.goodblogs = [] u.goodblogs = []
u.lameblogs = [] u.lameblogs = []
userlist.append(u) userlist.append(u)
@ -121,13 +122,13 @@ def render_template(path, week=None, **kwargs):
continue continue
if should_skip(u.skip, week): if should_skip(u.skip, week):
skipped_users.append(u) skipped_users.append(u)
continue continue
elif user_start > week_start: elif user_start > week_start:
skip.append(u) skip.append(u)
continue continue
for blog in u.links: for blog in u.links:
b=blog[0] b=blog[0]
weeks=u.posts[b] weeks=u.posts[b]
if len(weeks) <= week or not weeks[week]: if len(weeks) <= week or not weeks[week]:
u.lameblogs.append(b) u.lameblogs.append(b)
else: else:
@ -144,10 +145,10 @@ def render_template(path, week=None, **kwargs):
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) < 2: if len(sys.argv) < 2:
print >>sys.stderr, "Usage: %s TEMPLATE [WEEK]" print("Usage: %s TEMPLATE [WEEK]", file=sys.stderr)
sys.exit(1) sys.exit(1)
template = sys.argv[1] template = sys.argv[1]
week = None week = None
if len(sys.argv) > 2: week = sys.argv[2] if len(sys.argv) > 2: week = sys.argv[2]
print render_template(template, week) print(render_template(template, week))

View File

@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/python3
import yaml import yaml
import feedparser import feedparser
import datetime import datetime
@ -37,26 +37,26 @@ def get_link(post):
return post.link return post.link
def parse_feeds(weeks, uri): def parse_feeds(weeks, uri):
feedparser.USER_AGENT = "IronBloggerBot/0.1 +http://ironblogger.de/" feedparser.USER_AGENT = "IronBloggerBot/0.2 +http://ironblogger.de/"
feed = feedparser.parse(uri) feed = feedparser.parse(uri)
if not feed.entries: if not feed.entries:
print >>sys.stderr, "WARN: no entries for ", uri print("WARN: no entries for ", uri, file=sys.stderr)
for post in feed.entries: for post in feed.entries:
date = parse_published(get_date(post)) date = parse_published(get_date(post))
if date < START: if date < START:
continue continue
wn = (date - START).days / 7 wn = int ( (date - START).days / 7 )
while len(weeks) <= wn: while len(weeks) <= wn:
weeks.append([]) weeks.append([])
if post.has_key('title'): if 'title' in post:
post = dict(date=date, post = dict(date=date,
title=post.title, title=post.title,
url=get_link(post)) url=get_link(post))
if not post.has_key('title'): if 'title' not in post:
post = dict(date=date, post = dict(date=date,
title="", title="",
url=get_link(post)) url=get_link(post))
@ -67,13 +67,13 @@ if len(sys.argv) > 1:
for username in sys.argv[1:]: for username in sys.argv[1:]:
blogs = log.setdefault(username, {}) blogs = log.setdefault(username, {})
for l in users[username]['links']: for l in users[username]['links']:
weeks = blogs.setdefault(l[0], []) weeks = blogs.setdefault(l[0], [])
parse_feeds(weeks, l[3]) parse_feeds(weeks, l[3])
else: else:
for (username, u) in users.items(): for (username, u) in list(users.items()):
blogs = log.setdefault(username, {}) blogs = log.setdefault(username, {})
for l in u['links']: for l in u['links']:
weeks = blogs.setdefault(l[0], []) weeks = blogs.setdefault(l[0], [])
parse_feeds(weeks, l[3]) parse_feeds(weeks, l[3])
with open('out/report.yml', 'w') as f: with open('out/report.yml', 'w') as f:

View File

@ -1,8 +1,8 @@
#!/usr/bin/python #!/usr/bin/python
import ConfigParser, os import configparser, os
def load_settings(): def load_settings():
configfile = ConfigParser.ConfigParser() configfile = configparser.ConfigParser()
configfile.read('settings.cfg') configfile.read('settings.cfg')
config=dict() config=dict()
config['mail']=configfile.get("general","mail") config['mail']=configfile.get("general","mail")

View File

@ -1,15 +1,15 @@
#!/usr/bin/python #!/usr/bin/python3
import render import render
import os import os
import sys import sys
import xmlrpclib import xmlrpc.client
import subprocess import subprocess
import settings import settings
config=settings.load_settings() config=settings.load_settings()
x = xmlrpclib.ServerProxy(config['xmlrpc_endpoint']) x = xmlrpc.client.ServerProxy(config['xmlrpc_endpoint'])
page = x.wp.getPage(config['blog_id'], config['participants_page_id'], config['username'], config['password']) page = x.wp.getPage(config['blog_id'], config['participants_page_id'], config['username'], config['password'])
text = render.render_template('templates/users.tmpl') text = render.render_template('templates/users.tmpl')

View File

@ -1,9 +1,9 @@
#!/usr/bin/python #!/usr/bin/python3
# This Python file uses the following encoding: utf-8 # This Python file uses the following encoding: utf-8
import render import render
import os import os
import sys import sys
import xmlrpclib import xmlrpc.client
import subprocess import subprocess
import datetime import datetime
import yaml import yaml
@ -22,7 +22,7 @@ if len(args)>0:
if args[0] == '-q': if args[0] == '-q':
dry_run = True dry_run = True
quick_view = True quick_view = True
send_mail = False send_mail = False
args = args[1:] args = args[1:]
if args[0] == '-r': if args[0] == '-r':
@ -32,14 +32,15 @@ if len(args)>0:
if args[0] == '-n': if args[0] == '-n':
dry_run = True dry_run = True
send_mail = False send_mail = False
args = args[1:] args = args[1:]
date = args[0] date = args[0]
with open('ledger', 'a') as f: with open('ledger', 'a') as f:
f.write("\n") f.write("\n")
f.write(render.render_template('templates/ledger', date)) # print(render.render_template('templates/ledger', date).decode("utf-8"))
f.write(render.render_template('templates/ledger', date).decode("utf-8"))
if not dry_run: if not dry_run:
subprocess.check_call(["git", "commit", "ledger", subprocess.check_call(["git", "commit", "ledger",
@ -61,7 +62,7 @@ with open('ledger', 'a') as f:
if not dry_run: if not dry_run:
text = render.render_template('templates/week.tmpl', date, punt=punt) text = render.render_template('templates/week.tmpl', date, punt=punt).decode("utf-8")
lines = text.split("\n") lines = text.split("\n")
title = lines[0] title = lines[0]
@ -69,16 +70,16 @@ if not dry_run:
page = dict(title = title, description = body) page = dict(title = title, description = body)
x = xmlrpclib.ServerProxy(config['xmlrpc_endpoint']) x = xmlrpc.client.ServerProxy(config['xmlrpc_endpoint'])
x.metaWeblog.newPost(config['blog_id'], config['username'], config['password'], page, True) x.metaWeblog.newPost(config['blog_id'], config['username'], config['password'], page, True)
if not reminder: if not reminder:
email = render.render_template('templates/email.txt', date, punt=punt,mail=config['mail']) email = render.render_template('templates/email.txt', date, punt=punt,mail=config['mail'])
else: else:
email = render.render_template('templates/reminder.txt', date, punt=punt,mail=config['mail']) email = render.render_template('templates/reminder.txt', date, punt=punt,mail=config['mail'])
if quick_view: if quick_view:
print(render.render_template('templates/quick_view.tmpl',date,punt=punt)) print((render.render_template('templates/quick_view.tmpl',date,punt=punt)))
if dry_run and not quick_view: if dry_run and not quick_view:
print email print(email)
if send_mail: if send_mail:
# p = subprocess.Popen(['mutt', '-H', '/dev/stdin'], # p = subprocess.Popen(['mutt', '-H', '/dev/stdin'],
p = subprocess.Popen(['/usr/sbin/sendmail', '-oi', '-t'], p = subprocess.Popen(['/usr/sbin/sendmail', '-oi', '-t'],