linggle 0.04.1 → 0.04.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 288f9d2334757b69761e3a59c603fdf0b0c57384
4
- data.tar.gz: ac3838c6149adac131c599f31a037788a07ba8ee
3
+ metadata.gz: 802e94722af8ae524eba3fd6935d0613cf85c80e
4
+ data.tar.gz: f31502a544bcdf605d1cb4afa92ad8491a530429
5
5
  SHA512:
6
- metadata.gz: 739f7b165eb9abef1f3fe054be76aa516e49ce10b2bf566b575126eb2af4666ac120ae9e47d337ff1f44eb540218a32e049c9486d986f2454857b4f891fd817a
7
- data.tar.gz: 31e7582ecb61d58f60ea8853251cf52a251bfedaf125c2764402613019732e0d8b8561a3de1973520a3ad2408096dfca1dbd85d69861b563c9d522b86f1581a1
6
+ metadata.gz: e932afdae70f562b0cc7d64ee574ffca8c2c9a1f6925afa93f73ce728fdd942a6b23fda0bfdc5b66a3ac4523af3c0072b565ca216912d912084793ff5d1e5f5e
7
+ data.tar.gz: 2a05c2b614eecc4e43d71a5a140bd3f14d05b0205ea44e6602b020b1dea5303e72e5cfa85d7b579872d4b3c6cb3f038f4d622c58a228d1a61d332fa63f6351ac
@@ -14,9 +14,9 @@ module Linggle
14
14
  # Your code goes here...
15
15
  def self.execute()
16
16
  args = parse()
17
- query_str = args.join(' ')
17
+ return if args.nil? && args.size < 1
18
+ query_str = args.drop(0).join(' ')
18
19
 
19
- # TODO rescue network exception, and follow redirections..
20
20
  results = Api.new.query(query_str)
21
21
  =begin
22
22
  {
@@ -33,23 +33,36 @@ module Linggle
33
33
  percent = ( (x[1].to_f/results['total']).round(2) * 100 ).to_s
34
34
  "#{x[0].green} Percent: #{percent.yellow} #{x[1]} \n #{percent.percent_line} \n \n"
35
35
  end
36
- puts entries
37
36
  end
38
37
 
39
38
 
40
39
  def self.parse()
41
40
  OptionParser.new do |parser|
42
41
 
42
+ parser.banner = 'Linggle Command Line Interface:\n availale options: \n \n'
43
43
  if ARGV.size == 0
44
+ puts parser
44
45
  puts HELPS.map{|k,v| v }
45
46
  exit 0
46
47
  end
47
48
 
48
- parser.on("-h", "--help") do
49
+ parser.on("-u", "--usage", 'Show Usages') do
49
50
  puts HELPS.map{|k,v| v }
50
51
  exit 0
51
52
  end
52
53
 
54
+ parser.on("-h", "--help", 'Show This Help') do
55
+ puts parser
56
+ puts HELPS.map{|k,v| v }
57
+ end
58
+
59
+ parser.on('-V', '--version', 'Show Version') do
60
+ puts "Linggle CLI Version: #{VERSION}"
61
+ exit 0
62
+ end
63
+
64
+
65
+ parser
53
66
  end.parse!
54
67
 
55
68
  end
@@ -1,5 +1,10 @@
1
+ require 'fileutils'
2
+ require 'sdbm'
3
+ require 'date'
4
+ require 'logger'
1
5
  module Linggle
2
6
  API_BASE = 'http://linggle.com'.freeze
7
+ STORE_PATH = "#{ENV['HOME']}/linggle_cli".freeze
3
8
  TIMEOUT = 3
4
9
  COLORS = {
5
10
  e: :yellow, # example
@@ -11,7 +16,7 @@ module Linggle
11
16
  def initialize
12
17
  @conn = Faraday.new(url: API_BASE) do |f|
13
18
  f.request :url_encoded
14
- f.response :logger
19
+ f.response :logger, ::Logger.new('/dev/null')
15
20
  f.adapter Faraday.default_adapter
16
21
  end
17
22
  end
@@ -22,33 +27,37 @@ module Linggle
22
27
  #
23
28
 
24
29
  def query(query_str)
25
-
26
-
27
-
28
30
  # puts "conent #{content}"
29
31
  res = conn.post '/query/', {query: query_str, time: Time.now.to_i}.to_json do |req|
30
- puts csrf_token
31
32
  req.headers['Content-Type'] = 'application/json'
32
33
  req.headers['X-Requested-With'] = 'XMLHttpRequest'
33
34
  req['Cookie'] = "csrftoken=#{csrf_token}"
34
35
  req.headers['X-CSRFToken'] = csrf_token
35
36
  end
36
- puts res
37
- puts res.body
38
37
  JSON.parse(res.body)
39
38
  end
40
39
 
41
40
 
41
+ def example
42
+ url = 'https://nlp-ultron.cs.nthu.edu.tw/coca/example'
43
+ # 'https://nlp-ultron.cs.nthu.edu.tw/coca/example/' -H 'Origin: http://linggle.com' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' -H 'Content-Type: application/json' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Referer: http://linggle.com/' -H 'Connection: keep-alive' -H 'X-CSRFToken: aCT5jTOwy93GZXnHOpPrXXmRSNIngtTN3ChZ22It0933dALsqMNnmAl9vv0J3ufG' -H 'DNT: 1' --data-binary '{"ngram":"go to school"}' --compressed
44
+ end
45
+
42
46
  private
43
47
  def csrf_token
44
48
  return @csrf_token if @csrf_token
45
- cookies = conn.get.env.response_headers['set-cookie']
46
- # TODO save token with'sdbm'
47
- # File.open '.linggle_cli'
48
- crsf_token_data = cookies.split('; ').map {|x| x.split('=', 0) }.to_h
49
-
50
49
 
51
- @csrf_token = crsf_token_data['csrftoken']
50
+ FileUtils.mkdir STORE_PATH unless File.exists?(STORE_PATH)
51
+ SDBM.open "#{STORE_PATH}/.linggle_cli" do |db|
52
+ expires = DateTime.parse(db['expires']) rescue nil
53
+ if expires && expires > DateTime.now && (@csrf_token = db['csrf_token'])
54
+ return @csrf_token
55
+ else
56
+ cookies = conn.get.env.response_headers['set-cookie'].split('; ').map {|x| x.split('=', 0) }.to_h
57
+ db['expires'] = cookies['expires']
58
+ @csrf_token = db['csrf_token'] = cookies['csrftoken']
59
+ end
60
+ end
52
61
  end
53
62
  end
54
63
  end
@@ -1,3 +1,3 @@
1
1
  module Linggle
2
- VERSION = "0.04.1"
2
+ VERSION = "0.04.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linggle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.04.1
4
+ version: 0.04.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - fangxing
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-15 00:00:00.000000000 Z
11
+ date: 2018-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler