Add conkys and wallpapers
This commit is contained in:
54
.conky/gmail/.conkyrc-gmail
Executable file
54
.conky/gmail/.conkyrc-gmail
Executable file
@@ -0,0 +1,54 @@
|
||||
conky.config = {
|
||||
use_spacer='none',
|
||||
use_xft=true,
|
||||
font='DejaVu Sans:size=9',
|
||||
text_buffer_size=2048,
|
||||
update_interval=600,
|
||||
total_run_times=0,
|
||||
|
||||
own_window=true,
|
||||
own_window_transparent=true,
|
||||
own_window_type='normal',
|
||||
own_window_hints='undecorated,skip_taskbar,skip_pager',
|
||||
own_window_class='Conky-gmail',
|
||||
own_window_argb_visual=true,
|
||||
own_window_argb_value=0,
|
||||
|
||||
draw_shades=false,
|
||||
draw_outline=false,
|
||||
draw_borders=false,
|
||||
stippled_borders=0,
|
||||
double_buffer=true,
|
||||
|
||||
default_color='white',
|
||||
default_shade_color='black',
|
||||
--Minimum size of text area
|
||||
maximum_width=1200 ,
|
||||
minimum_width=1200 ,
|
||||
|
||||
--alignment=top_right,
|
||||
--gap_x=20,
|
||||
--gap_y=20,
|
||||
|
||||
no_buffers=true,
|
||||
net_avg_samples=2,
|
||||
|
||||
override_utf8_locale=true,
|
||||
|
||||
use_spacer=none,
|
||||
|
||||
short_units=on,
|
||||
|
||||
default_color=000,
|
||||
default_shade_color=black,
|
||||
color1 = '0099ff',
|
||||
color2 = '0000ff',
|
||||
color3 = '3a3a3a',
|
||||
color4 = 'dddddd',
|
||||
default_outline_color='2edd2e'
|
||||
};
|
||||
conky.text = [[
|
||||
${alignc}${font FontAwesome:size=24}${font Liberation Sans:size=24}mail${font}
|
||||
${color7}${hr}${color}
|
||||
# ${execp ~/.conky/gmail/gmail_imap.py}
|
||||
]];
|
||||
116
.conky/gmail/gmail_imap.py
Executable file
116
.conky/gmail/gmail_imap.py
Executable file
@@ -0,0 +1,116 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import imaplib
|
||||
import email
|
||||
import email.header
|
||||
import datetime
|
||||
from textwrap import wrap # For pretty printing assistance
|
||||
import json
|
||||
import time
|
||||
import os.path
|
||||
from os.path import expanduser
|
||||
|
||||
WRAP_LIMIT = 80
|
||||
path = "/home/kuba/.conky/gmail/last.json"
|
||||
|
||||
def process_mailbox(M):
|
||||
rv, data = M.search(None, "ALL")
|
||||
if rv != 'OK':
|
||||
print ("No messages found!")
|
||||
return
|
||||
numbers = data[0].split()
|
||||
ln = len(numbers)
|
||||
timestamp = str(round(time.time()))
|
||||
|
||||
parsed_data = {} #Find and save last 10 emails
|
||||
parsed_data['timestamp'] = timestamp
|
||||
emails = []
|
||||
for num in numbers[ln:ln-10:-1]:
|
||||
rv, data = M.fetch(num, '(RFC822)')
|
||||
if rv != 'OK':
|
||||
print ("ERROR getting message"), num
|
||||
return
|
||||
eml = {}
|
||||
|
||||
msg = email.message_from_bytes(data[0][1])
|
||||
eml.update({'subject' : decode_field(msg['Subject'])})
|
||||
eml.update({'sender' : decode_field(msg['From'])})
|
||||
date_tuple = email.utils.parsedate_tz(msg['Date'])
|
||||
if date_tuple:
|
||||
local_date = datetime.datetime.fromtimestamp(
|
||||
email.utils.mktime_tz(date_tuple))
|
||||
eml.update({'date' : local_date.strftime("%a, %d %b %Y %H:%M:%S")})
|
||||
emails.append(eml)
|
||||
|
||||
parsed_data['emails'] = emails
|
||||
with open(path, 'w') as outfile:
|
||||
json.dump(parsed_data, outfile)
|
||||
|
||||
def fetch_mail():
|
||||
M = imaplib.IMAP4_SSL('imap.gmail.com')
|
||||
|
||||
try:
|
||||
json_data=open(expanduser('~')+'/.conky/scripts/.passwords.json')
|
||||
data = json.load(json_data)
|
||||
username=data['gmail']['username']
|
||||
password=data['gmail']['password']
|
||||
rv, data = M.login(username, password)
|
||||
except imaplib.IMAP4.error:
|
||||
print ("LOGIN FAILED!!! ")
|
||||
sys.exit(1)
|
||||
|
||||
rv, data = M.select()
|
||||
if rv == 'OK':
|
||||
# print("Processing mailbox...\n")
|
||||
process_mailbox(M)
|
||||
M.close()
|
||||
else:
|
||||
print("ERROR: Unable to open mailbox ", rv)
|
||||
|
||||
M.logout()
|
||||
|
||||
def fill(text, width):
|
||||
'''A custom method to assist in pretty printing'''
|
||||
if len(text) < width:
|
||||
return text + ' '*(width-len(text))
|
||||
else:
|
||||
return text
|
||||
|
||||
def decode_field(data):
|
||||
decode = email.header.decode_header(data)[0]
|
||||
if decode[1] == None or decode[1] == 'unknown-8bit':
|
||||
return str(decode[0])
|
||||
else:
|
||||
return str(decode[0], decode[1])
|
||||
|
||||
def print_output(data):
|
||||
timestamp = int(data['timestamp'])
|
||||
updated = time.strftime("%m/%d %H:%M", time.gmtime(timestamp))
|
||||
print ("${alignr}Updated: ${color white}%s" % updated)
|
||||
|
||||
for mail in data['emails']:
|
||||
subject = mail['subject']
|
||||
sender = mail['sender']
|
||||
dte = mail['date']
|
||||
if(len(subject) > WRAP_LIMIT - 3):
|
||||
print ("${color1}%s..." % subject)
|
||||
else:
|
||||
print ("${color1}%s" % subject)
|
||||
|
||||
# Does file exist?
|
||||
if not (os.path.isfile(path)):
|
||||
fetch_mail()
|
||||
with open(path) as infile:
|
||||
data = json.load(infile)
|
||||
|
||||
# Is data recently fetched?
|
||||
ts = int(data['timestamp'])
|
||||
now = round(time.time())
|
||||
if (ts + 3600 < now):
|
||||
fetch_mail()
|
||||
with open(path) as infile:
|
||||
data = json.load(infile)
|
||||
|
||||
# Format output
|
||||
print_output(data)
|
||||
0
.conky/gmail/last.json
Normal file
0
.conky/gmail/last.json
Normal file
147
.conky/gmail/lua.lua
Normal file
147
.conky/gmail/lua.lua
Normal file
@@ -0,0 +1,147 @@
|
||||
line_width = 200
|
||||
function indent_entry(contents,init,break_at)
|
||||
if contents:len() <= break_at then
|
||||
return contents -- Done
|
||||
end
|
||||
|
||||
local begin_tag,end_tag = contents:find("<(.-)>",init)
|
||||
if not begin_tag or begin_tag > break_at then
|
||||
-- No tag exists or next tag is behind preferred break
|
||||
-- Find last space before break, if no space break word at break_at
|
||||
for i=break_at,init,-1 do -- Iterate backwards over string
|
||||
if contents:sub(i,i) == " " then
|
||||
break_at = i
|
||||
break;
|
||||
end
|
||||
end
|
||||
contents = contents:sub(1,break_at) .. "\n" .. contents:sub(break_at+1)
|
||||
init = break_at+1 -- Not necessarily same value as passed to the function
|
||||
break_at = break_at + line_width
|
||||
debug = "No tag or tag behind break."
|
||||
elseif end_tag < break_at then
|
||||
-- Next tag is before preferred break
|
||||
-- Increase break_at by length of tag and iterate (init = end_tag)
|
||||
init = end_tag
|
||||
break_at = break_at + 1 + end_tag - begin_tag
|
||||
debug = "Tag entirely before break"
|
||||
else
|
||||
-- Case break_at in the middle of tag:
|
||||
-- Increase break at by length of tag
|
||||
init = end_tag + 1
|
||||
break_at = break_at + 1 + end_tag - begin_tag
|
||||
debug = "Tag in the middle of break"
|
||||
end
|
||||
return indent_entry(contents,init,break_at)
|
||||
end
|
||||
function parse_paragraph(par)
|
||||
return indent_entry(par:gsub("\n", " "),1,line_width)
|
||||
end
|
||||
function substitute_tags(content)
|
||||
return content:gsub("<code>",("${color4}${font %s}"):format(font_i(9)))
|
||||
:gsub("<b>",("${font_b %s}"):format(font_b(9)))
|
||||
:gsub("</(.-)>","${color}${font}")
|
||||
:gsub("<a(.-)>","")
|
||||
end
|
||||
function parse_entry(contents)
|
||||
local ret = ""
|
||||
for paragraph in contents:gmatch("<p>(.-)</p>") do
|
||||
if ret ~= "" then
|
||||
ret = ret .. "\n\n" -- Add line break on all except first entry
|
||||
end
|
||||
ret = ret .. parse_paragraph(paragraph)
|
||||
end
|
||||
return substitute_tags(ret)
|
||||
end
|
||||
function format_entry(entry)
|
||||
local entry_contents = parse_entry(entry.summary)
|
||||
local res = ('${color2}${font %s}%s\n'):format(font(12),entry.title)
|
||||
res = ('%s${color}${font}%s'):format(res,entry_contents)
|
||||
res = ('%s${color3}${font %s}${alignr} Uppdated: %s\n\n'):format(res,font(8),os.date("%x",entry.updated_parsed))
|
||||
return res
|
||||
end
|
||||
function conky_print()
|
||||
local f = assert(get_file_handle('rb'))
|
||||
local content = f:read("*all")
|
||||
f:close()
|
||||
return content
|
||||
end
|
||||
function fetch_feed()
|
||||
local imap4 = require "imap4"
|
||||
local Message = require "pop3.message"
|
||||
|
||||
local connection = imap4('imap.gmail.com', 993)
|
||||
|
||||
assert(connection:isCapable('IMAP4rev1'))
|
||||
|
||||
connection:login('****', '****')
|
||||
|
||||
-- Select INBOX with read only permissions.
|
||||
local info = connection:examine('INBOX')
|
||||
print(info.exist, info.recent)
|
||||
|
||||
-- List info on the 4 most recent mails.
|
||||
for _,v in pairs(connection:fetch('RFC822', (info.exist-4)..':*')) do
|
||||
print("-------------------------")
|
||||
local msg = Message(v.RFC822)
|
||||
print("ID: ", msg:id())
|
||||
print("subject: ", msg:subject())
|
||||
print("to: ", msg:to())
|
||||
print("from: ", msg:from())
|
||||
print("from addr: ", msg:from_address())
|
||||
print("reply: ", msg:reply_to())
|
||||
print("reply addr: ", msg:reply_address())
|
||||
print("trunc: ", msg:is_truncated())
|
||||
for i,v in ipairs(msg:full_content()) do
|
||||
if v.text then print(" ", i , "TEXT: ", v.type, #v.text)
|
||||
else print(" ", i , "FILE: ", v.type, v.file_name or v.name, #v.data) end
|
||||
end
|
||||
end
|
||||
|
||||
-- close connection
|
||||
connection:logout()
|
||||
-- Fetch over http
|
||||
local http_request = require "http.request"
|
||||
local url = "https://www.archlinux.org/feeds/news/"
|
||||
local headers, stream = assert(http_request.new_from_uri(url):go())
|
||||
local body = assert(stream:get_body_as_string())
|
||||
if headers:get ":status" ~= "200" then
|
||||
error(body)
|
||||
end
|
||||
a,s,c = headers:geti(3)
|
||||
p="%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+) GMT"
|
||||
day,month,year,hour,min,sec=s:match(p)
|
||||
MON={Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12}
|
||||
month=MON[month]
|
||||
offset=os.time()-os.time(os.date("!*t"))
|
||||
retrieved = os.date("%x",os.time({day=day,month=month,year=year,hour=hour,min=min,sec=sec})+offset)
|
||||
|
||||
-- Parse
|
||||
local feedparser = require("feedparser")
|
||||
entries = feedparser.parse(body).entries
|
||||
s = entries[2].summary
|
||||
local res = ('${alignr}${font %s}Retrieved: %s\n'):format(font(8),retrieved)
|
||||
for key,entry in pairs(entries) do
|
||||
res = res .. format_entry(entry)
|
||||
end
|
||||
|
||||
-- Save to cache
|
||||
local file = get_file_handle("w")
|
||||
io.output(file)
|
||||
io.write(res)
|
||||
io.close(file)
|
||||
return parsed
|
||||
end
|
||||
function font(size)
|
||||
return ('DejaVu Sans:size=%d'):format(size)
|
||||
end
|
||||
function font_b(size)
|
||||
return ('DejaVu Sans:bold:size=%d'):format(size)
|
||||
end
|
||||
function font_i(size)
|
||||
return ('DejaVu Sans:italic:size=%d'):format(size)
|
||||
end
|
||||
function get_file_handle(opt)
|
||||
local filename = '/home/kuba/.conky/arch/cache'
|
||||
return io.open(filename, opt)
|
||||
end
|
||||
s = fetch_feed()
|
||||
Reference in New Issue
Block a user