1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
//! Represent Google Trend geo maps. //! //! See in which location your keyword was most popular during the specified time frame. //! Values are calculated on a scale from 0 to 100, where 100 is the location with the most popularity as a fraction of total searches in that location, a value of 50 indicates a location which is half as popular. //! A value of 0 indicates a location where there was not enough data for this term. use crate::errors::KeywordNotSet; use crate::request_handler::Query; use crate::{Client, Country}; use serde_json::Value; // Correpond to Multiline request => Google trend interest curve #[derive(Debug, Clone)] pub struct RegionInterest { pub client: Client, pub resolution: &'static str, } impl Default for RegionInterest { fn default() -> Self { Self { client: Client::default(), resolution: "REGION", } } } impl RegionInterest { /// Create a `RegionInterest` Instance. /// /// Returns a `RegionInterest` instance pub fn new(client: Client) -> Self { let res; if client.country.eq(&Country::ALL) { res = "COUNTRY"; } else { res = "REGION"; } Self { client, resolution: res, } } /// Add a geographic filter. /// You can filter result by "REGION" and "CITY". /// /// Warning : When making a request on all countries, use "COUNTRY" instead of "REGION" else it will panic /// /// Returns a `RegionInterest` instance. /// /// # Example /// ``` /// # use rtrend::{Country, Keywords, Client, RegionInterest}; /// let keywords = Keywords::new(vec!["hacker"]); /// let country = Country::US; /// let client = Client::new(keywords, country).build(); /// /// let region_interest = RegionInterest::new(client).with_filter("CITY").get(); /// /// println!("{}", region_interest); /// ``` /// /// # Panics /// By default, on google trend, when making request on all countries, the country are called region (when you use filter). /// But we can't use the keyword REGION to filter by COUNTRY. So instead use the keyword "COUNTRY" /// /// This example will panic /// ```should_panic /// # use rtrend::{Country, Keywords, Client, RegionInterest}; /// let keywords = Keywords::new(vec!["hacker"]); /// let country = Country::ALL; /// let client = Client::new(keywords, country).build(); /// /// let region_interest = RegionInterest::new(client).with_filter("REGION").get(); /// /// println!("{}", region_interest); /// ``` /// /// Instead do not filter and let the default value or use the "COUNTRY" filter /// ``` /// # use rtrend::{Country, Keywords, Client, RegionInterest}; /// let keywords = Keywords::new(vec!["hacker"]); /// let country = Country::ALL; /// let client = Client::new(keywords, country).build(); /// /// let region_interest = RegionInterest::new(client).with_filter("COUNTRY").get(); /// // or /// // let region_interest = RegionInterest::new(client).get(); /// // will return the same result /// /// println!("{}", region_interest); /// ``` /// pub fn with_filter(mut self, scale: &'static str) -> Self { self.resolution = scale; self } /// Retrieve maps data for all keywords. /// /// Retrieve data for all keywords set within the client. /// /// Returns a JSON serde Value (`serde_json::Value`). /// /// # Example /// ```rust /// # use rtrend::{Country, Keywords, Client, RegionInterest}; /// let keywords = Keywords::new(vec!["hacker"]); /// let country = Country::US; /// let client = Client::new(keywords, country).build(); /// /// let region_interest = RegionInterest::new(client).get(); /// /// println!("{}", region_interest); /// ``` /// /// # Panics /// Panic if the client have not been built. /// /// ```rust,should_panic /// # use rtrend::{Country, Keywords, Client, RegionInterest}; /// let keywords = Keywords::new(vec!["hacker"]); /// let country = Country::US; /// /// // Client not built /// let client = Client::new(keywords, country); /// /// let region_interest = RegionInterest::new(client).get(); /// ``` pub fn get(&self) -> Value { self.send_request()[0].clone() } /// Retrieve maps data for a specific keywords. /// /// Retrieve the data for one keywords set within the client. /// /// Returns a JSON serde Value (`serde_json::Value`). /// /// # Example /// ``` /// # use rtrend::{Country, Keywords, Client, RegionInterest}; /// let keywords = Keywords::new(vec!["PS4","XBOX","PC"]); /// let country = Country::ALL; /// /// let client = Client::new(keywords, country).build(); /// /// let region_interest = RegionInterest::new(client).get_for("PS4"); /// /// println!("{}", region_interest); /// ``` /// /// # Panics /// Will panic if input keyword have not been set previously for the client. /// /// ```should_panic /// # use rtrend::{Country, Keywords, Client, RegionInterest}; /// let keywords = Keywords::new(vec!["PS4","XBOX","PC"]); /// let country = Country::ALL; /// /// let client = Client::new(keywords, country).build(); /// /// let region_interest = RegionInterest::new(client).get_for("WII"); /// ``` pub fn get_for(&self, keyword: &str) -> Value { let index = self .client .keywords .keywords .iter() .position(|&x| x == keyword); let keyword_index = match index { Some(k) => k, None => Err(KeywordNotSet).unwrap(), }; let response_index = keyword_index + 1; self.send_request()[response_index].clone() } }