Exemplo n.º 1
0
 /// <summary>
 /// Saves the user data to cache
 /// </summary>
 /// <param name="user">TvdbUser</param>
 public void SaveToCache(TvdbUser user)
 {
   if (user != null)
   {
     if (!Directory.Exists(_rootFolder)) Directory.CreateDirectory(_rootFolder);
     _filestream = new FileStream(_rootFolder + Path.DirectorySeparatorChar + "user_" + user.UserIdentifier + ".ser", FileMode.Create);
     _formatter.Serialize(_filestream, user);
     _filestream.Close();
   }
 }
Exemplo n.º 2
0
    /// <summary>
    /// Extract user data from
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    internal List<TvdbUser> ExtractUser(String data)
    {
      //Stopwatch watch = new Stopwatch();
      //watch.Start();
      XDocument xml = XDocument.Parse(data);
      var allUsers = from episode in xml.Descendants("User")
                     select new
                     {

                       Identifier = episode.Element("Identifier").Value,
                       Name = episode.Element("Name").Value,
                       Favorites = episode.Element("Favorites"),
                       Preferred = episode.Element("PreferredLanguage")
                     };

      List<TvdbUser> userList = new List<TvdbUser>();
      foreach (var a in allUsers)
      {
        TvdbUser user = new TvdbUser
                          {
                            UserIdentifier = a.Identifier,
                            UserName = a.Name,
                            UserPreferredLanguage = a.Preferred.HasAttributes
                                                      ? TvDbUtils.ParseLanguage(a.Preferred.FirstAttribute.NextAttribute.Value)
                                                      : TvdbLanguage.DefaultLanguage
                          };
        List<int> favList = new List<int>();
        foreach (String f in a.Favorites.Value.Split(','))
        {
          int val;
          if (Int32.TryParse(f, out val))
            favList.Add(val);
        }
        user.UserFavorites = favList;
        userList.Add(user);
      }
      //watch.Stop();
      //Log.Debug("Extracted " + userList.Count + " actors in " + watch.ElapsedMilliseconds + " milliseconds");
      return userList;
    }
Exemplo n.º 3
0
    /// <summary>
    /// Create the xml content to save a TvdbUser to file
    /// </summary>
    /// <param name="user">User to store</param>
    /// <returns>xml content</returns>
    internal String CreateUserData(TvdbUser user)
    {
      XElement xml = new XElement("Data");

      StringBuilder favBuilder = new StringBuilder();
      if (user.UserFavorites != null && user.UserFavorites.Count > 0)
      {
        foreach (int f in user.UserFavorites)
        {
          favBuilder.Append(f);
          favBuilder.Append(",");
        }
      }

      XElement preferred = new XElement("PreferredLanguage");
      if (user.UserPreferredLanguage != null)
      {
        preferred.Add(new XAttribute("Id", user.UserPreferredLanguage.Id));
        preferred.Add(new XAttribute("Abbreviation", user.UserPreferredLanguage.Abbriviation));
        preferred.Add(new XAttribute("Name", user.UserPreferredLanguage.Name));
      }

      xml.Add(new XElement("User",
                  new XElement("Name", user.UserName),
                  new XElement("Identifier", user.UserIdentifier),
                  new XElement("Favorites", favBuilder.ToString()),
                  preferred
             ));

      return xml.ToString();
    }
Exemplo n.º 4
0
 /// <summary>
 /// Write the user data to file
 /// </summary>
 /// <param name="user">User to store</param>
 /// <param name="path">Path on disk</param>
 /// <returns>true if the file could be stored, false otherwise</returns>
 internal bool WriteUserData(TvdbUser user, String path)
 {
   String fileContent = CreateUserData(user);
   try
   {
     FileInfo info = new FileInfo(path);
     if (!info.Directory.Exists) info.Directory.Create();
     File.WriteAllText(info.FullName, fileContent);
     return true;
   }
   catch (Exception)
   {
     return false;
   }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Saves the user data to cache
 /// </summary>
 /// <param name="user">TvdbUser object</param>
 public void SaveToCache(TvdbUser user)
 {
   if (user == null)
     return;
   if (!Directory.Exists(_rootFolder)) Directory.CreateDirectory(_rootFolder);
   _xmlWriter.WriteUserData(user, _rootFolder + Path.DirectorySeparatorChar + "user_" + user.UserIdentifier + ".xml");
 }