-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbib.selection
executable file
·99 lines (75 loc) · 3 KB
/
bib.selection
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
93
94
95
96
97
98
99
#!/usr/bin/env python3
import json
import urwid
import urllib.request
import urllib.error
import argparse
import subprocess
import asyncio
import aiohttp
_cache = {}
parser = argparse.ArgumentParser(description="bibtex entries via DBLP search")
parser.add_argument('query', type=str, help='query')
args = parser.parse_args()
data = subprocess.check_output( [ 'bib.get' , args.query ] )
object = json.loads( data.decode() )
results = object['results']
publications = results['publications']
choices = sorted( publications , key = lambda x : ( -int(x['date']) , x['title'] ) )
def menu(title, choices):
body = [urwid.Text(title), urwid.Divider()]
for c in choices:
button = urwid.Button('{date} {title}'.format( **c ) )
urwid.connect_signal(button, 'click', item_chosen, c)
body.append(urwid.AttrMap(button, None, focus_map='reversed'))
return urwid.ListBox(urwid.SimpleFocusListWalker(body))
def item_chosen(button, choice):
original_widget = main.original_widget
def back ( button ) :
main.original_widget = original_widget
head = urwid.Text(['You chose ', choice['title'], '\n'])
url = choice['bibtex']['condensed']
downloading = urwid.Text( ['Downloading ', url, '\n'])
main.original_widget = urwid.Filler(urwid.Pile([head,downloading]))
async_loop.create_task(display(url, choice , back))
async def display ( url , choice , back ) :
try:
if url not in _cache:
# with urllib.request.urlopen( url ) as response :
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
_cache[url] = await response.read( )
raw = _cache[url]
bibtex = raw.decode()
result = urwid.Text(['Copied to clipboard\n\n',bibtex])
p = subprocess.Popen( [ 'xsel' , '-bi' ] , stdin = subprocess.PIPE )
p.communicate( raw )
# except urllib.error.HTTPError:
except aiohttp.ClientError:
result = urwid.Text(['Failed to download ', url, '\n'])
head = urwid.Text(['You chose ', choice['title'], '\n'])
done_button = urwid.Button('Exit')
urwid.connect_signal(done_button, 'click', exit_program)
back_button = urwid.Button('Back')
urwid.connect_signal(back_button, 'click', back)
main.original_widget = urwid.Filler(urwid.Pile([
head,
result,
urwid.AttrMap(back_button, None, focus_map='reversed'),
urwid.AttrMap(done_button, None, focus_map='reversed'),
]))
def exit_program(button):
raise urwid.ExitMainLoop()
async_loop = asyncio.get_event_loop()
event_loop = urwid.AsyncioEventLoop(loop=async_loop)
main = urwid.Padding(menu('List of publications', choices), left=2, right=2)
top = urwid.Overlay(main, urwid.SolidFill('\N{MEDIUM SHADE}'),
align='center', width=('relative', 60),
valign='middle', height=('relative', 60),
min_width=20, min_height=9)
loop = urwid.MainLoop(
top,
palette=[('reversed', 'standout', '')],
event_loop=event_loop,
)
loop.run()