|
| 1 | +use crate::spider::Information; |
| 2 | +use serde_json::Value; |
| 3 | +use std::{ |
| 4 | + env, |
| 5 | + fs::{self, File}, |
| 6 | + io::prelude::*, |
| 7 | + io::Error, |
| 8 | + path::Path, |
| 9 | + result::Result, |
| 10 | +}; |
| 11 | +fn create_storage_before() { |
| 12 | + let home = env::var("HOME").unwrap(); |
| 13 | + fs::create_dir_all(home + "/.config/tv2ray").unwrap(); |
| 14 | +} |
| 15 | +fn create_json_file() -> Result<String, Error> { |
| 16 | + let home = env::var("HOME").unwrap(); |
| 17 | + let location = format!("{}/.config/tv2ray/storage.json", home); |
| 18 | + let path = Path::new(location.as_str()); |
| 19 | + let mut file = File::create(&path)?; |
| 20 | + let storge: String = "[]".to_string(); |
| 21 | + file.write_all(storge.as_bytes())?; |
| 22 | + Ok(storge) |
| 23 | +} |
| 24 | +fn get_json() -> Result<String, Error> { |
| 25 | + let home = env::var("HOME").unwrap(); |
| 26 | + let location = format!("{}/.config/tv2ray/storage.json", home); |
| 27 | + let mut file = File::open(location)?; |
| 28 | + let mut output = String::new(); |
| 29 | + file.read_to_string(&mut output).unwrap(); |
| 30 | + Ok(output) |
| 31 | +} |
| 32 | +pub fn start() -> Vec<Information> { |
| 33 | + create_storage_before(); |
| 34 | + let messages = match get_json() { |
| 35 | + Ok(output) => output, |
| 36 | + Err(_) => create_json_file().unwrap(), |
| 37 | + }; |
| 38 | + let mut informations = Vec::new(); |
| 39 | + let v: Value = serde_json::from_str(&messages.as_str()).unwrap(); |
| 40 | + let mut index = 0; |
| 41 | + while v[index] != Value::Null { |
| 42 | + let the_url = v[index]["url"].to_string(); |
| 43 | + let lenghth = the_url.len(); |
| 44 | + let instore = &the_url[1..lenghth - 1]; |
| 45 | + informations.push(Information { |
| 46 | + urls: instore.to_string(), |
| 47 | + func: v[index]["func"].to_string(), |
| 48 | + add: v[index]["add"].to_string(), |
| 49 | + aid: v[index]["aid"].to_string(), |
| 50 | + host: v[index]["host"].to_string(), |
| 51 | + id: v[index]["id"].to_string(), |
| 52 | + net: v[index]["net"].to_string(), |
| 53 | + path: v[index]["path"].to_string(), |
| 54 | + port: v[index]["port"].to_string(), |
| 55 | + ps: v[index]["ps"].to_string(), |
| 56 | + tls: v[index]["tls"].to_string(), |
| 57 | + typpe: v[index]["type"].to_string(), |
| 58 | + }); |
| 59 | + //let names = v[index]["ps"].to_string(); |
| 60 | + //start.add_item(remove_quotation(names), url); |
| 61 | + index += 1; |
| 62 | + } |
| 63 | + informations |
| 64 | +} |
0 commit comments