1
+ import sys
2
+
1
3
import click
4
+ from rich .console import Console
5
+
2
6
from pywhat import identifier , printer
7
+ from pywhat .distribution import Distribution
8
+ from pywhat .helper import AvailableTags , InvalidTag
9
+
10
+
11
+ def print_tags (ctx , opts , value ):
12
+ if value :
13
+ tags = sorted (AvailableTags ().get_tags ())
14
+ console = Console ()
15
+ console .print ("[bold #D7Afff]" + "\n " .join (tags ) + "[/bold #D7Afff]" )
16
+ sys .exit ()
17
+
18
+
19
+ def parse_options (rarity , include_tags , exclude_tags ):
20
+ filter = dict ()
21
+ if rarity is not None :
22
+ rarities = rarity .split (":" )
23
+ if len (rarities ) != 2 :
24
+ print ("Invalid rarity range format ('min:max' expected)" )
25
+ sys .exit (1 )
26
+ try :
27
+ if not rarities [0 ].isspace () and rarities [0 ]:
28
+ filter ["MinRarity" ] = float (rarities [0 ])
29
+ if not rarities [1 ].isspace () and rarities [1 ]:
30
+ filter ["MaxRarity" ] = float (rarities [1 ])
31
+ except ValueError :
32
+ print ("Invalid rarity argument (float expected)" )
33
+ sys .exit (1 )
34
+ if include_tags is not None :
35
+ filter ["Tags" ] = list (map (str .strip , include_tags .split (',' )))
36
+ if exclude_tags is not None :
37
+ filter ["ExcludeTags" ] = list (map (str .strip , exclude_tags .split (',' )))
38
+
39
+ try :
40
+ distribution = Distribution (filter )
41
+ except InvalidTag :
42
+ print ("Passed tags are not valid.\n " \
43
+ "You can check available tags by using: 'pywhat --tags'" )
44
+ sys .exit (1 )
45
+
46
+ return distribution
3
47
4
48
5
49
@click .command (
8
52
)
9
53
)
10
54
@click .argument ("text_input" , required = True )
11
- def main (text_input ):
55
+ @click .option ("-t" , "--tags" , is_flag = True , expose_value = False , callback = print_tags , help = "Show available tags and exit." )
56
+ @click .option ("-r" , "--rarity" , help = "Filter by rarity. This is in the range of 0:1. To filter only items past 0.5, use 0.5: with the colon on the end." )
57
+ @click .option ("-i" , "--include_tags" , help = "Only print entries with included tags." )
58
+ @click .option ("-e" , "--exclude_tags" , help = "Exclude tags." )
59
+ def main (text_input , rarity , include_tags , exclude_tags ):
12
60
"""
13
61
What - Identify what something is.\n
14
62
15
63
Made by Bee https://twitter.com/bee_sec_san\n
16
64
17
65
https://github.com/bee-san\n
18
66
67
+ Filtration:\n
68
+ --rarity min:max\n
69
+ Only print entries with rarity in range [min,max]. min and max can be omitted.\n
70
+ --include_tags list\n
71
+ Only include entries containing at least one tag in a list. List is a comma separated list.\n
72
+ --include_tags list\n
73
+ Exclude specified tags. List is a comma separated list.\n
74
+
19
75
Examples:
20
76
21
77
* what "HTB{this is a flag}"
@@ -24,22 +80,27 @@ def main(text_input):
24
80
25
81
* what -- 52.6169586, -1.9779857
26
82
83
+ * what --rarity 0.6: [email protected]
84
+
27
85
Your text must either be in quotation marks, or use the POSIX standard of "--" to mean "anything after -- is textual input".
28
86
29
87
"""
30
88
31
- what_obj = What_Object ()
89
+ what_obj = What_Object (
90
+ parse_options (rarity , include_tags , exclude_tags )
91
+ )
32
92
identified_output = what_obj .what_is_this (text_input )
33
93
34
94
p = printer .Printing ()
35
95
p .pretty_print (identified_output )
36
96
37
97
38
98
class What_Object :
39
- def __init__ (self ):
40
- self .id = identifier .Identifier ()
99
+ def __init__ (self , distribution ):
100
+ self .id = identifier .Identifier (distribution )
41
101
42
- def what_is_this (self , text : str ) -> dict :
102
+ def what_is_this (
103
+ self , text : str ) -> dict :
43
104
"""
44
105
Returns a Python dictionary of everything that has been identified
45
106
"""
0 commit comments