-
Notifications
You must be signed in to change notification settings - Fork 0
/
neogrfetch.py
executable file
·92 lines (71 loc) · 2.93 KB
/
neogrfetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/home/slzatz/sonos-companion/bin/python
import sys
from io import BytesIO
from math import ceil
import sqlite3
import textwrap
from display_image import show_image, get_screen_size, resize_show_image
db_file = "/home/slzatz/sonos-companion/gr.db"
display_size = 100
conn = sqlite3.connect(db_file)
cur = conn.cursor()
if __name__ == "__main__":
'''
Displays image, quote and bio of select authors
'''
line_count = 2
x = get_screen_size()
if x.cell_width == 0:
sys.exit(1)
# bug that values sometimes 2x what they should be
if x.cell_width > 12:
x.width = x.width//2
x.height = x.height//2
x.cell_width = x.cell_width//2
x.cell_height = x.cell_height//2
if len(sys.argv) == 1:
cur.execute("SELECT book_gr_id,quote FROM quotes ORDER BY RANDOM() LIMIT 1;")
row = cur.fetchone()
book_gr_id = row[0]
quote = row[1]
sql = "SELECT authors.id,authors.name,authors.bio, books.title FROM books " \
"INNER JOIN authors ON authors.id=books.author_id AND books.gr_id=?;" #(book_gr_id)
cur.execute(sql, (book_gr_id,))
row = cur.fetchone()
author_id, name, bio, title = row
else:
sql = "SELECT authors.id,authors.name,authors.bio,books.title, books.gr_id FROM books " \
"INNER JOIN authors ON authors.id=books.author_id AND authors.name=? ORDER BY RANDOM() LIMIT 1;"
cur.execute(sql, (sys.argv[1].title(),))
row = cur.fetchone()
author_id, name, bio, title, gr_id = row
cur.execute("SELECT quote FROM quotes WHERE book_gr_id=?ORDER BY RANDOM() LIMIT 1;", (gr_id,))
row = cur.fetchone()
quote = row[0]
cur.execute("SELECT image,size FROM images WHERE author_id=? ORDER BY RANDOM() LIMIT 1;", (author_id,))
row = cur.fetchone()
image = BytesIO(row[0])
image_size = row[1]
indent_cols = ceil(display_size/x.cell_width)
indent = indent_cols * ' '
max_chars_line = x.cols - 5
quote = textwrap.wrap(quote, max_chars_line, initial_indent=indent, subsequent_indent=indent)
line_count += len(quote)
quote = "\n".join(quote)
# just using one sentence bios but bios have up to 10 sentences
bio = bio[:bio.find('.') + 1]
bio = textwrap.wrap(bio, max_chars_line, initial_indent=indent, subsequent_indent=indent)
line_count += len(bio)
bio = "\n".join(bio)
print() # line feed
q = f"\x1b[3m{quote}\x1b[0m\n{indent}-- \x1b[1m{title} by {name}\x1b[0m\n\n{bio} {indent_cols=} {x.width=} {x.height=} {x.cell_width=} {x.cell_height=}"
print(q)
sys.stdout.buffer.write(f"\x1b[{line_count}A".encode('ascii')) # move back up line_count lines
if display_size == image_size:
show_image(image)
else:
resize_show_image(image, display_size, display_size)
print()
image_rows = display_size//x.cell_height
if line_count > image_rows:
print((line_count-image_rows)*"\n")