-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfbq
executable file
·68 lines (55 loc) · 1.74 KB
/
fbq
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
#!/usr/bin/env python2.7
# connect and serve a query to the firstbits db
from __future__ import print_function
import mysql.connector
import getopt
import sys
def ad2fb(cursor,ad):
cursor.execute("SELECT * FROM main WHERE address='%s';" % ad)
reply = cursor.fetchone()
if reply:
print("address: {0}\nfirstbits: {1}\nheight: {2}".format(reply[1], reply[2], reply[3]))
def fb2ad(cursor,fb):
cursor.execute("SELECT * FROM main WHERE firstbits='%s';" % fb)
reply = cursor.fetchone()
if reply:
print("address: {0}\nfirstbits: {1}\nheight: {2}".format(reply[1], reply[2], reply[3]))
try:
opts, args = getopt.getopt(sys.argv[1:],"f:a:c:",["firstbits=","address=","config="])
except getopt.GetoptError as err:
print(str(err),file=sys.stderr)
sys.exit(2)
conf=None
request=None
request_arg=None
for o,a in opts:
if o in ["-f","--firstbits"]:
request='f'
request_arg=a
elif o in ["-a","--address"]:
request='a'
request_arg=a
elif o in ["-c","--config"]:
conf=a
else:
assert False, "unhandled option"
if not conf:
print("need to pass a config file location with -c/--config",file=sys.stderr)
sys.exit(3)
try:
config_lines = open(conf).readlines()
config_dict = dict([l.strip().split('=') for l in config_lines])
except:
print("failed to read info from config file",conf,file=sys.stderr)
sys.exit(1)
cnx = mysql.connector.connect(user=config_dict['mysqluser'],
password=config_dict['mysqlpassword'],
host=config_dict['mysqlhost'],
database=config_dict['mysqldb'])
cursor = cnx.cursor()
if request == 'f':
fb2ad(cursor, request_arg)
elif request == 'a':
ad2fb(cursor, request_arg)
cursor.close()
cnx.close()