Skip to content

Instantly share code, notes, and snippets.

@cuteribs-1
Last active October 1, 2022 09:10
Show Gist options
  • Save cuteribs-1/2df4eef0415cd5cc2a3fec7ce0247c0e to your computer and use it in GitHub Desktop.
Save cuteribs-1/2df4eef0415cd5cc2a3fec7ce0247c0e to your computer and use it in GitHub Desktop.
void Main()
{
var text = File.ReadAllText(@"C:\Users\Eric\Desktop\5000-random-people.json");
var users = JsonConvert.DeserializeAnonymousType(text, new { Results = default(IList<UserProfile>) }).Results;
using var sw = new StreamWriter(@"C:\Users\Eric\Desktop\5000-random-people.vcf");
foreach(var u in users){
sw.WriteLine(ConvertToVCard(u));
}
}
static string ConvertToVCard(UserProfile user){
var fullName = $"{SanitizeValue(user.Name?.Title)} {SanitizeValue(user.Name?.First)} {SanitizeValue(user.Name?.Last)}".Trim();
var address = $"{user.Location?.Street?.Number} {SanitizeValue(user.Location?.Street?.Name)}".Trim();
var vcard = $@"
BEGIN:VCARD
VERSION:3.0
FN:{fullName}
UID:b850444a-9caf-40a8-b29a-656d6e3e65a7
N:{SanitizeValue(user.Name?.Last)};{SanitizeValue(user.Name?.First)};;;
BDAY:{user.DOB?.Date.ToString("yyyyMMdd")}19681016
EMAIL:{SanitizeValue(user.Email)}
TEL:{SanitizeValue(user.Phone)}
TEL:{SanitizeValue(user.Cell)}
ADR:;;{SanitizeValue(user.Location?.Postcode)};{SanitizeValue(user.Location?.City)};{SanitizeValue(user.Location?.State)};{SanitizeValue(user.Location?.Postcode)};{SanitizeValue(user.Location?.Country)}
{ConvertPhoto(user.Picture?.Large)}
END:VCARD
";
return vcard;
}
static string SanitizeValue(string value)
{
if(value == null) return value;
var sb = new StringBuilder();
foreach(var c in value.ToArray()){
if(c == '\\' || c == ';' || c==',' || c=='\n'){
sb.Append('\\');
}
sb.Append(c);
}
return sb.ToString();
}
static string ConvertPhoto(string url)
{
return "PHOTO:";
if(string.IsNullOrWhiteSpace(url)) return "PHOTO:";
var client = new WebClient();
var data = client.DownloadData(url);
var str = $"PHOTO;ENCODING=BASE64:{Convert.ToBase64String(data)}";
var sb = new StringBuilder();
var limit = 75;
for (int i = 0; i < str.Length / limit; i++)
{
sb.AppendLine(str.Substring(i * limit, limit));
}
return sb.ToString();
}
static void DownloadPhotos()
{
var urlPattern = "https://randomuser.me/api/portraits/{type}/{index}.jpg";
var indexes = Enumerable.Range(-1, 200);
var types = new string[] { "men", "women", "lego" };
var list = indexes.SelectMany(
i => types.Select(t => new { Type = t, Index = i == -1 ? "undefined" : i.ToString() })
);
var tasks = list.Select(
item => Task.Factory.StartNew(
state =>
{
using var client = new WebClient();
var url = urlPattern.Replace("{type}", item.Type).Replace("{index}", item.Index);
client.DownloadFile(
url,
$@"C:\Users\Eric\Desktop\fakeusers\{item.Type}-" + Path.GetFileName(url)
);
Console.WriteLine(url);
}, TaskCreationOptions.None
)
);
Task.WhenAll(tasks);
Console.WriteLine("Complete");
}
// You can define other methods, fields, classes and namespaces here
public class UserProfile
{
public string Gender { get; set; }
public NameProfile Name { get; set; }
public LocationProfile Location { get; set; }
public string Email { get; set; }
public LoginProfile Login { get; set; }
public DOBProfile DOB { get; set; }
public string Phone { get; set; }
public string Cell { get; set; }
public PictureProfile Picture { get; set; }
public class NameProfile{
public string Title { get; set; }
public string First { get; set; }
public string Last { get; set; }
}
public class LocationProfile
{
public StreetProfile Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string Postcode { get; set; }
public CoodinatesProfile Coodinates { get; set; }
}
public class StreetProfile
{
public int Number { get; set; }
public string Name { get; set; }
}
public class CoodinatesProfile
{
public float Latitude { get; set; }
public float Longitude { get; set; }
}
public class LoginProfile
{
public string UUID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
public class DOBProfile
{
public DateTime Date { get; set; }
public int Age { get; set; }
}
public class PictureProfile
{
public string Large { get; set; }
public string Medium { get; set; }
public string Thumbnail { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment