-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSiglinpac.cs
152 lines (126 loc) · 5.65 KB
/
Siglinpac.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json;
using HtmlAgilityPack;
using Newtonsoft.Json;
namespace Siglinpac_core
{
/// <summary>
/// Static tool to fetch stickers
/// </summary>
public static class Siglinpac
{
/// <summary>
/// Returns a meta object of the sticker pack given from the link
/// </summary>
/// <param name="html_path"></param>
/// <returns></returns>
public static SLP_Meta get_sticker_pack_meta(string html_path)
{
// Create the sticker pack descriptor
SLP_Meta sticker_pack = new SLP_Meta();
// Start the HAP and load the path to the HTML
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument product_page = web.Load(html_path);
// Populate relevant titles
sticker_pack.Title = product_page.DocumentNode.SelectSingleNode("//p[@class='mdCMN38Item01Ttl']").InnerText;
sticker_pack.Author = product_page.DocumentNode.SelectSingleNode("//a[@class='mdCMN38Item01Author']").InnerText;
sticker_pack.Description = product_page.DocumentNode.SelectSingleNode("//p[@class='mdCMN38Item01Txt']").InnerText;
sticker_pack.product_link = html_path;
// Then get the links to the images
sticker_pack.image_links = get_sticker_image_links(html_path);
// Return the meta for the sticker pack
return sticker_pack;
}
private static List<string> get_sticker_image_links(string html_path)
{
// Iniitalize HAP to load the HTML content
/// Create the doc and load it in memory
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument product_page = web.Load(html_path);
// Place where we store the links
List<string> spanTags = new List<string>();
// Grab the main sticker image
string main_img_link = product_page.DocumentNode.SelectSingleNode("//img[@class='FnImage']").GetAttributeValue("src", null);
main_img_link = main_img_link.Replace(";compress=true", null);
spanTags.Add(main_img_link);
// Grab all the sticker pack images
/// Extract all span tags
/// Specifically, the ones with a style attribute, which we will use to filter out the unecessary fluff
/// example: "background-image:url(https://stickershop.line-scdn.net/stickershop/v1/sticker/297435927/android/sticker.png;compress=true);"
foreach (HtmlNode link in product_page.DocumentNode.SelectNodes("//span[@style]"))
{
// Get the value of the Style tag
HtmlAttribute att = link.Attributes["style"];
// Put the value into a string so we can manipulate it
string img_link = att.Value;
img_link = img_link.Replace("background-image:url(", null);
img_link = img_link.Replace(";compress=true);", null);
spanTags.Add(img_link);
}
// Return the list of image links
return spanTags.Distinct().ToList();
}
/// <summary>
/// Download stickers from the given sticker pack meta into the given path
/// </summary>
/// <param name="sticker_pack"></param>
/// <param name="path_to_save_files_in"></param>
public static void download_stickers(SLP_Meta sticker_pack, string path_to_save_files_in)
{
string path_to_sticker_folder = path_to_save_files_in + "\\" + sticker_pack.Author + "_" + sticker_pack.Title;
// Create the directory if it does not exist to store the stickers
if (!Directory.Exists(path_to_sticker_folder))
{
/// Makes a directory with the name of author_title
Directory.CreateDirectory(path_to_sticker_folder);
}
// Write out the meta information
string jsonString = JsonConvert.SerializeObject(sticker_pack, Formatting.Indented);
File.WriteAllText(path_to_sticker_folder + "\\meta.json", jsonString);
// Download the images with the WebClient
int increment = 0;
WebClient wc = new WebClient();
foreach (string str_link in sticker_pack.image_links)
{
wc.DownloadFile(str_link, path_to_sticker_folder + "\\image_" + increment + ".png");
increment++;
}
}
public static string get_path_to_stickers(SLP_Meta sticker_pack, string path_to_save_files_in)
{
return path_to_save_files_in + "\\" + sticker_pack.Author + "_" + sticker_pack.Title;
}
/// <summary>
/// Descriptor class for the sticker pack
/// </summary>
public class SLP_Meta
{
[JsonProperty]
/// <summary>
/// The title of the sticker pack
/// </summary>
public string Title;
[JsonProperty]
/// <summary>
/// The author of the sticker pack
/// </summary>
public string Author;
[JsonProperty]
/// <summary>
/// The description of the sticker pack
/// </summary>
public string Description;
[JsonProperty]
public string product_link;
[JsonProperty]
/// <summary>
/// The links to the sticker pack images
/// </summary>
public List<String> image_links;
}
}
}