-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtarot.py
More file actions
167 lines (129 loc) · 3.53 KB
/
Copy pathtarot.py
File metadata and controls
167 lines (129 loc) · 3.53 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
import quantumrandom
import requests
import textwrap
from lxml import html
import random
import re
#Get string from user
question = input('Question: ')
lucky_number = sum([x for x in map(ord, question)])
#print('Lucky number:', lucky_number) #DEBUG
#Get a random interger
try:
print('Connecting to quantum random number generator...')
ran = int(quantumrandom.randint(0, 78))
except:
print('Failed, falling back to pseudo-random number generator...')
ran = int(random.randint(0, 77))
#print('Random integer:', ran) #DEBUG
#Combine two numners to get card index
i = (ran + lucky_number) % 78
#print('Card index:', i) #DEBUG
## Major Arcana
if i < 22:
card = "maj" + str(i).zfill(2)
## Minor Arcana
else:
if i < 36:
suit = "w"
n = str(i-21)
elif i < 50:
suit = "c"
n = str(i-35)
elif i < 64:
suit = "s"
n = str(i-49)
else:
suit = "p"
n = str(i-63)
## Convert card number to letters for face cards
if n == '1':
n = 'a'
elif n == '11':
n = 'pg'
elif n == '12':
n = 'kn'
elif n == '13':
n = 'qn'
elif n == '14':
n = 'kg'
card = suit + n
## Make request and setup xpath
print('Retrieving card description...\n')
uri = "http://www.learntarot.com/" + card + ".htm"
try:
r = requests.get(uri)
except:
print('No internet connection, exiting...')
exit()
tree = html.fromstring(r.text)
x = tree.xpath
## Save text by xpath
title = x('//h1/text()')[0]
keywords = x('//ul[1]/li//text()')
divider = '=' * 70
action_terms_raw = x('//dl/dt//text()')
action_definitions_raw = x('//dd//text()')
boldwords = x('//dl/dt/b/text()')
## Process action section
action_terms = ''.join(action_terms_raw).split('\r\n')[:-1]
action_definitions_list = ''.join(action_definitions_raw).split('\r\n')
action_definitions = []
sublist = []
for i in range(len(action_definitions_list)):
if action_definitions_list[i] != '':
sublist.append(action_definitions_list[i])
else:
action_definitions.append(sublist)
sublist = []
## Apply bold words
b = '\033[1m'
bb = '\033[0m'
for i in range(len(action_terms)):
action_terms[i] = action_terms[i].replace(boldwords[i],
b + boldwords[i] + bb)
## Get description
lines = r.text.split('\n')
desc_html = []
mark = False
for line in lines:
if '<HR>' in line:
mark = False
if mark:
desc_html.append(line)
if 'DESCRIPTION' in line:
mark = True
## Convert html formatting to bash formatting
description = []
for line in desc_html:
# <a>
mod = re.sub('\<[aA].*?\>', '\033[4m', line)
mod = re.sub('(.)\<\/[aA]\>', '\g<1>\033[24m', mod)
# <i>
mod = re.sub('\<[iI].*?\>', '\033[1m', mod)
mod = re.sub('(.)\<\/[iI]\>', '\g<1>\033[0m', mod)
# <b>
mod = re.sub('\<[bB]\>', '\033[1m', mod)
mod = re.sub('(.)\<\/[bB]\>', '\g<1>\033[0m', mod)
# <BR>
mod = re.sub('\<BR\>', '', mod)
# [note]
mod = re.sub('\[.*\]', '', mod)
description.append(mod)
## Print it all out
print(title, '\n')
for i in range(len(keywords)):
print('•', keywords[i])
print('\n' + divider)
print('ACTIONS\n')
for i in range(len(action_terms)):
print(action_terms[i])
for j in action_definitions[i]:
print(" ", j)
print("")
print(divider)
print('DESCRIPTION\n')
for line in description:
if not line.startswith('<') and not line.startswith('See also'):
print(textwrap.fill(line), '\n')