Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
nanobot4
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Cool Fire
nanobot4
Commits
dba5a075
Commit
dba5a075
authored
Feb 19, 2018
by
Cool Fire
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated for new API
parent
dff82be8
Pipeline
#534
failed with stages
in 4 minutes and 55 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
30 deletions
+23
-30
data/googlesearch.json.example
data/googlesearch.json.example
+2
-2
plugins/search.rb
plugins/search.rb
+21
-28
No files found.
data/googlesearch.json.example
View file @
dba5a075
{
"host" : "https://www.googleapis.com",
"path" : "/customsearch/v1
element
",
"path" : "/customsearch/v1",
"key" : "Yourcustomsearchapikey",
"num" : "1",
"cx" : "some-cx:id"
}
\ No newline at end of file
}
plugins/search.rb
View file @
dba5a075
...
...
@@ -21,11 +21,11 @@ class Search
@googleapiconf
=
'googlesearch.json'
@googleurl
=
''
@isbndbapihost
=
"isbndb.com"
@isbndbapipath
=
"/api/books.xml?access_key=%APIKEY%&index1=isbn&value1="
@isbndbapifile
=
"isbndb.apikey"
loadisbndbapikey
loadgoogleconfig
end
...
...
@@ -41,12 +41,12 @@ class Search
arguments
.
gsub!
(
/ /
,
"+"
)
# Sanitize search request
# Retreive JSON
agent
=
Mechanize
.
new
page
=
agent
.
get
(
"
#{
@googleurl
}#{
CGI
::
escape
(
arguments
)
}
"
)
json
=
page
.
body
res
=
JSON
.
parse
(
json
)
puts
res
.
inspect
result
=
"
#{
res
[
'items'
][
0
][
'link'
]
}
-
#{
res
[
'items'
][
0
][
'title'
]
}
:
#{
res
[
'items'
][
0
][
'snippet'
]
}
"
page
=
agent
.
get
(
"
#{
@googleurl
}#{
CGI
::
escape
(
arguments
)
}
"
)
json
=
page
.
body
res
=
JSON
.
parse
(
json
)
result
=
"
#{
res
[
'results'
][
0
][
'unescapedUrl'
]
}
-
#{
res
[
'results'
][
0
][
'titleNoFormatting'
]
}
"
if
(
result
.
empty?
)
result
=
"Error: No result."
end
...
...
@@ -60,23 +60,23 @@ class Search
@irc
.
message
(
from
,
result
)
end
end
# Function to search book titles by ISBN number
def
isbn
(
nick
,
user
,
host
,
from
,
msg
,
arguments
,
con
)
if
(
!
arguments
.
nil?
&&
!
arguments
.
empty?
)
arguments
.
gsub!
(
/[^0-9\-]/
,
""
)
# Sanitize ISBN number
# Retreive XML
xml
=
Net
::
HTTP
.
get
(
@isbndbapihost
,
@isbndbapipath
+
arguments
)
result
=
isbndbxmlparse
(
xml
)
if
(
result
.
empty?
)
result
=
"Error: No result."
end
else
result
=
"Invalid input. Expecting search query."
end
if
(
con
)
@output
.
c
(
result
+
"
\n
"
)
else
...
...
@@ -107,54 +107,47 @@ class Search
# XML parser for ISBNdb results
def
isbndbxmlparse
(
xmldoc
)
xmldoc
=
Nokogiri
::
XML
(
xmldoc
)
title
=
xmldoc
.
at_xpath
(
"//ISBNdb/BookList/BookData/Title"
)
if
(
title
.
instance_of?
NilClass
)
return
""
else
return
"Book title:
#{
title
.
text
}
"
end
end
# Load API key for ISBNdb
def
loadisbndbapikey
()
# Check if a appid is stored on disk
if
File
.
exist?
(
@config
.
datadir
+
'/'
+
@isbndbapifile
)
# Make sure there's a variable instance
apikey
=
""
# Read database from file
file
=
File
.
open
(
@config
.
datadir
+
'/'
+
@isbndbapifile
)
file
.
each
do
|
line
|
apikey
<<
line
end
apikey
.
gsub!
(
/[:blank:\n\r]/
,
""
)
file
.
close
# Replace string token with actual key
@isbndbapipath
.
gsub!
(
/%APIKEY%/
,
apikey
)
else
@output
.
bad
(
"Could not load API key from
#{
@config
.
datadir
}
/
#{
@isbndbapifile
}
for ISBNdb search."
)
@output
.
bad
(
"Could not load API key from
#{
@config
.
datadir
}
/
#{
@isbndbapifile
}
for ISBNdb search.
\n
"
)
end
end
# Load google custom search api config
def
loadgoogleconfig
if
File
.
exist?
(
@config
.
datadir
+
'/'
+
@googleapiconf
)
cnf
=
JSON
.
parse
File
.
read
(
"
#{
@config
.
datadir
}
/
#{
@googleapiconf
}
"
)
jsonline
=
""
File
.
open
(
@config
.
datadir
+
'/'
+
@googleapiconf
)
do
|
file
|
file
.
each
do
|
line
|
jsonline
<<
line
end
end
cnf
=
JSON
.
parse
(
jsonline
)
@googleurl
=
"
#{
cnf
[
'host'
]
}#{
cnf
[
'path'
]
}
?key=
#{
cnf
[
'key'
]
}
&num=
#{
cnf
[
'num'
]
}
&cx=
#{
cnf
[
'cx'
]
}
&q="
else
@output
.
bad
(
"Could not load API data from
#{
@config
.
datadir
}
/
#{
@googleapiconf
}
for Google custom search."
)
@output
.
bad
(
"Could not load API data from
#{
@config
.
datadir
}
/
#{
@googleapiconf
}
for Google custom search.
\n
"
)
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment