summary refs log blame commit diff stats
path: root/pkg/apod/json.go
blob: d484209309781ab425886411c5aff07b8bdd5b59 (plain) (tree)
1
2
3
4
5
6
7
8






                       
                                                




































                                                                                               
                                                           

                                
package apod

import (
	"encoding/json"
	"fmt"
	"regexp"

	"framagit.org/andinus/cetus/pkg/request"
)

// Res holds the response from the api.
type Res struct {
	Copyright      string `json:"copyright"`
	Date           string `json:"date"`
	Explanation    string `json:"explanation"`
	HDURL          string `json:"hdurl"`
	MediaType      string `json:"media_type"`
	ServiceVersion string `json:"service_version"`
	Title          string `json:"title"`
	URL            string `json:"url"`

	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

// UnmarshalJson will take body as input & unmarshal it to res
func UnmarshalJson(res *Res, body string) error {
	err := json.Unmarshal([]byte(body), res)
	if err != nil {
		return fmt.Errorf("UnmarshalJson failed\n%s", err.Error())
	}
	return nil
}

// GetJson returns json response received from the api
func GetJson(reqInfo map[string]string) (string, error) {
	re := regexp.MustCompile("((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])")
	if !re.MatchString(reqInfo["date"]) {
		return "", fmt.Errorf("%s does not match format 'YYYY-MM-DD'", reqInfo["date"])
	}

	params := make(map[string]string)
	params["api_key"] = reqInfo["apiKey"]
	params["date"] = reqInfo["date"]

	body, err := request.GetRes(reqInfo["api"], params)
	return string(body), err
}
)} @files.map!{|basename| Entry.new(@path, basename)} end attr_reader(:path, :files, :pos, :width, :files_raw, :file_size, :read) attr_accessor(:scheduled) def self.filter=(x) @@filter = Regexp.new(x, Regexp::IGNORECASE) rescue nil end def self.filter() @@filter end def scheduled?() @scheduled end def read?() @read end def pos=(x) # if @files.size <= 1 or x < 0 # x = 0 # elsif x > @files.size # x = @files.size - 1 # end @pos = x make_sure_cursor_is_in_range() @pointed_file = @files[x] resize end def recheck_stuff() # log "pointed file: #@pointed_file" # log @files_raw # log "" if test = @files_raw.index(@pointed_file) # log("if") @pos = test else # log("else") make_sure_cursor_is_in_range() end end def make_sure_cursor_is_in_range() if @files.size <= 1 or @pos < 0 @pos = 0 elsif @pos > @files.size @pos = @files.size - 1 end end def find_file(x) x = File.basename(x) files.each_with_index do |file, i| if file.basename == x self.pos = i end end end def empty?() Dir.entries(@path).size <= 2 end def restore() for f in @files f.marked = false end end def pointed_file=(x) if @files_raw.include?(x) @pointed_file = x @pos = @files_raw.index(x) else self.pos = 0 end resize end def size() @files.size end def resize() pos = Fm.get_offset(self, lines) if @files.empty? @width = 0 else @width = 0 @files[pos, lines-2].each_with_index do |fn, ix| ix += pos sz = fn.basename.size + fn.infostring.size + 2 @width = sz if @width < sz end # @width = @files[pos,lines-2].map{|x| File.basename(x).size}.max end end def get_file_info() @file_size = 0 @files.each do |f| f.refresh @file_size += f.size if f.file? end @read = true end # def refresh() # @files = Dir.new(@path).to_a # if OPTIONS['hidden'] # @files -= ['.', '..', 'lost+found'] # else # @files.reject!{|x| x[0] == ?. or x == 'lost+found'} # end # if @files.empty? # @files = ['.'] # end # @files.map!{|basename| Entry.new(@path, basename)} # # if @pos >= @files.size # @pos = @files.size - 1 # elsif @files.include?(@pointed_file) # @pos = @files.index(@pointed_file) # end # end def refresh(info=false) if File.mtime(@path) != @mtime read_dir end if info log("getting file info of #{@path}") get_file_info end sort end def schedule() Scheduler << self end def refresh!() oldfile = @pointed_file read_dir get_file_info sort if @files.include? oldfile self.pointed_file = oldfile end end def sort_sub(x, y) case OPTIONS['sort'] when :name x.basename <=> y.basename when :ext x.ext <=> y.ext when :type x.ext.filetype <=> y.ext.filetype when :size x.size <=> y.size when :ctime x.ctime <=> y.ctime when :mtime x.mtime <=> y.mtime else x.basename <=> y.basename end end def sort() @files = @files.sort {|x,y| if OPTIONS['dir_first'] if x.dir? if y.dir? then sort_sub(x, y) else -1 end else if y.dir? then 1 else sort_sub(x, y) end end else sort_sub(x, y) end } @files.reverse! if OPTIONS['sort_reverse'] @files_raw = @files.map{|x| x.to_s} end end