Last active
December 28, 2024 17:34
-
-
Save fatihbahceci/ac4fd87f42aa333ba13b5b89ba46f883 to your computer and use it in GitHub Desktop.
transfer-pictures-from-lagim-cukuru
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
test_text = """<xml bla bla hebele | |
hübele | |
cart curt""" | |
pattern = r'(https?://soz\.lk/[^:\s]+)' | |
matches = re.findall(pattern, test_text) | |
for url in matches: | |
print(url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
namespace SozlkDownloader | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
// 1) Buraya size verilen URL listesini koyabilirsiniz. | |
// İsterseniz bu string'i programın dışından okuyarak da alabilirsiniz. | |
#region url | |
string urlList = | |
@"https://soz.lk/i/a | |
https://soz.lk/i/b | |
https://soz.lk/i/c | |
"; | |
#endregion | |
// 2) Satır satır ayrıştır. | |
var lines = urlList | |
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) | |
.Select(l => l.Trim()) | |
.Where(l => !string.IsNullOrWhiteSpace(l)) | |
.ToList(); | |
// 3) İndirme yapılacak klasörü hazırlayalım. | |
string downloadFolder = "pic_downloads"; | |
if (!Directory.Exists(downloadFolder)) | |
{ | |
Directory.CreateDirectory(downloadFolder); | |
} | |
// 4) HttpClient ayarları | |
// Cloudflare ve benzeri korumalarda yardımcı olması için | |
// Tarayıcı benzeri User-Agent kullanıyoruz. | |
HttpClientHandler handler = new HttpClientHandler | |
{ | |
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, | |
// Bazı durumlarda sertifika hatasını aşmak isterseniz: | |
// ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true | |
}; | |
using (HttpClient client = new HttpClient(handler)) | |
{ | |
// Tarayıcı benzeri header'lar ekleyelim | |
client.DefaultRequestHeaders.Add("User-Agent", | |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " | |
+ "(KHTML, like Gecko) Chrome/112.0.5615.49 Safari/537.36"); | |
client.DefaultRequestHeaders.Add("Accept", | |
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); | |
client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.9,tr;q=0.8"); | |
// 5) Her bir URL için işlem yap | |
foreach (var shortUrl in lines) | |
{ | |
try | |
{ | |
// a) HTML içeriğini alalım | |
string html = await client.GetStringAsync(shortUrl); | |
// b) Örnek HTML'den anlaşıldığı üzere <a id="image-zoom" href="..."> ya da | |
// <img id="image" src="..."> gibi etiketlerde link bulunabiliyor. | |
// Aşağıdaki regex, <a id="image-zoom" href="..."> şeklini yakalamaya çalışır. | |
// Gerekirse <meta property="og:image" content="..."> da yakalanabilir. | |
var match = Regex.Match(html, @"<a\s+id=""image-zoom""\s+href=""([^""]+)""", | |
RegexOptions.IgnoreCase); | |
if (!match.Success) | |
{ | |
// Yedek olarak <img id="image" src="..." şeklinde de arayalım: | |
match = Regex.Match(html, @"<img\s+id=""image""\s+src=""([^""]+)""", | |
RegexOptions.IgnoreCase); | |
} | |
if (!match.Success) | |
{ | |
// Hiçbiri yoksa parse edilemedi | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine($"[ERR] {shortUrl} (Resim linki bulunamadı)"); | |
Console.ResetColor(); | |
continue; | |
} | |
// c) Bulduğumuz gerçek resim URL'sini alalım | |
string realImageUrl = match.Groups[1].Value; | |
// d) Orijinal short link'ten son parçayı çekelim | |
// Örn: https://soz.lk/i/r43faw87 => r43faw87 | |
// Sonra .png uzantısı ekleyeceğiz. | |
string fileNamePart = shortUrl.Split('/').Last(); | |
string localFileName = fileNamePart + ".png"; | |
string localFilePath = Path.Combine(downloadFolder, localFileName); | |
// e) Resmi indirmeye çalışalım | |
using (HttpResponseMessage response = await client.GetAsync(realImageUrl)) | |
{ | |
response.EnsureSuccessStatusCode(); // hata durumunda throw | |
using (var fs = new FileStream(localFilePath, FileMode.Create)) | |
{ | |
await response.Content.CopyToAsync(fs); | |
} | |
} | |
// f) Başarılıysa yeşil bas | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine($"[OK] {shortUrl} -> {localFileName}"); | |
} | |
catch (Exception ex) | |
{ | |
// Gözardı edilemeyen hatalar varsa kırmızı bas | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine($"[ERR] {shortUrl} ({ex.Message})"); | |
} | |
finally | |
{ | |
Console.ResetColor(); | |
} | |
} | |
} | |
Console.WriteLine("İşlem tamamlandı. Bir tuşa basarak çıkabilirsiniz."); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment