A photo manager for VRChat.
1use serde::ser::{Serialize, SerializeStruct, Serializer};
2use serde_json::json;
3
4#[derive(Clone)]
5pub struct World {
6 id: String,
7 name: String,
8 author: String,
9 author_id: String,
10 desc: String,
11 img: String,
12 max_users: u64,
13 visits: u64,
14 favourites: u64,
15 tags: String,
16 from: String,
17 from_site: String,
18 found: bool,
19}
20
21impl World {
22 pub fn new(world_id: String) -> World {
23 println!("Fetching world data for {}", &world_id);
24
25 let mut world = World {
26 id: world_id.clone(),
27 name: "".into(),
28 author: "".into(),
29 author_id: "".into(),
30 desc: "".into(),
31 img: "".into(),
32 max_users: 0,
33 visits: 0,
34 favourites: 0,
35 tags: "".into(),
36 from: "https://vrclist.com/worlds/".into(),
37 from_site: "vrclist.com".into(),
38 found: false,
39 };
40
41 let client = reqwest::blocking::Client::new();
42
43 let world_id_str = world_id.to_owned();
44 let fixed_id_req = client
45 .post("https://api.vrclist.com/worlds/id-convert")
46 .header("Content-Type", "application/json")
47 .header("User-Agent", "VRChat-Photo-Manager-Rust/0.0.1")
48 .body(json!({ "world_id": world_id_str }).to_string())
49 .send()
50 .unwrap()
51 .text()
52 .unwrap();
53
54 if &fixed_id_req == "" {
55 println!("World {} not found", world_id);
56 return world;
57 }
58
59 world.found = true;
60
61 let fixed_id: serde_json::Value = serde_json::from_str(&fixed_id_req).unwrap();
62 world.from = format!("https://vrclist.com/worlds/{}", fixed_id["id"].to_string());
63
64 let world_data = client
65 .post("https://api.vrclist.com/worlds/single")
66 .header("Content-Type", "application/json")
67 .header("User-Agent", "VRChat-Photo-Manager-Rust/0.0.1")
68 .body(json!({ "id": fixed_id["id"].to_string() }).to_string())
69 .send()
70 .unwrap()
71 .text()
72 .unwrap();
73
74 let world_data: serde_json::Value = serde_json::from_str(&world_data).unwrap();
75
76 world.name = world_data["name"].to_string();
77 world.author = world_data["authorName"].to_string();
78 world.author_id = world_data["authorId"].to_string();
79 world.desc = world_data["description"].to_string();
80 world.img = world_data["imageUrl"].to_string();
81 world.tags = world_data["tags"].to_string();
82
83 match world_data["vrchat_visits"].as_u64() {
84 Some(visits) => world.visits = visits,
85 None => {}
86 }
87
88 match world_data["capacity"].as_u64() {
89 Some(cap) => {
90 world.max_users = cap;
91 }
92 None => {}
93 }
94
95 println!("Fetched world data for {}", &world_id);
96 world
97 }
98}
99
100impl Serialize for World {
101 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
102 where
103 S: Serializer,
104 {
105 let mut s = serializer.serialize_struct("World", 7)?;
106 s.serialize_field("id", &self.id)?;
107 s.serialize_field("name", &self.name)?;
108 s.serialize_field("author", &self.author)?;
109 s.serialize_field("authorId", &self.author_id)?;
110 s.serialize_field("desc", &self.desc)?;
111 s.serialize_field("img", &self.img)?;
112 s.serialize_field("maxUsers", &self.max_users)?;
113 s.serialize_field("visits", &self.visits)?;
114 s.serialize_field("favourites", &self.favourites)?;
115 s.serialize_field("tags", &self.tags)?;
116 s.serialize_field("from", &self.from)?;
117 s.serialize_field("fromSite", &self.from_site)?;
118 s.serialize_field("found", &self.found)?;
119
120 s.end()
121 }
122}