Spaces:
Building
Building
import libtorrent as lt | |
import streamlit as st | |
import os | |
import re | |
import time | |
import requests | |
def sesi(info, lokasi_file): | |
st.session_state.info = info | |
st.session_state.lokasi_file = lokasi_file | |
if 'info' in st.session_state: | |
num_lines = st.session_state.info.count('\n') + 1 | |
text_area_height = 25 * num_lines | |
st.text_area("Hasil Redirect", st.session_state.info, height=text_area_height) | |
with open(st.session_state.lokasi_file, 'rb') as f: | |
file_contents = f.read() | |
st.download_button( | |
label="Download File TXT", | |
data=file_contents, | |
file_name=st.session_state.lokasi_file.replace('/home/user/app/', '').title().replace('Txt', 'txt'), | |
mime='text/plain' | |
) | |
return st.session_state.info, st.session_state.lokasi_file | |
def simpan_txt(nama_file, teks): | |
with open(nama_file + '.txt', 'w') as f: | |
f.write(teks) | |
lokasi_file = os.path.abspath(nama_file + '.txt') | |
return lokasi_file | |
def link_redirect(url): | |
# Kamus untuk menerjemahkan kode status | |
status_codes = { | |
200: 'OK', | |
301: 'Dipindahkan Secara Permanen', | |
302: 'Ditemukan', | |
303: 'Lihat Lainnya', | |
304: 'Tidak Dimodifikasi', | |
307: 'Pengalihan Sementara', | |
400: 'Permintaan Buruk', | |
401: 'Tidak Sah', | |
403: 'Dilarang', | |
404: 'Tidak Ditemukan', | |
500: 'Kesalahan Server Internal', | |
502: 'Gerbang Buruk', | |
503: 'Layanan Tidak Tersedia' | |
} | |
info = '' | |
response = requests.get(url) | |
if response.history: | |
info += "Redirects: \n" | |
for resp in response.history: | |
status = status_codes.get(resp.status_code, 'Kode Status Tidak Diketahui') | |
info += f"[{resp.status_code} {status}] \n {resp.url}\n\n" | |
info += "Final destination: \n" | |
status = status_codes.get(response.status_code, 'Kode Status Tidak Diketahui') | |
info += f"[{response.status_code} {status}] \n {response.url}\n" | |
else: | |
info += "No Redirects: \n" | |
return info | |
def format_info(info): | |
# Menghapus angka '3D' dari awal string dan mengambil 10 karakter terakhir | |
info = info.split('%')[-10].replace('3D1', '').replace('3D', '') | |
# Pola regex untuk mencocokkan pola yang dijelaskan | |
pattern = r'([a-z]+)(\d+)' | |
# Mencari kecocokan dengan pola regex | |
matches = re.findall(pattern, info, re.IGNORECASE) | |
if matches: | |
alfabet, number = matches[0] | |
# Mengubah alfabet menjadi huruf besar dan menghapus '0' dari awal nomor | |
number = number.lstrip('0') | |
# Jika panjang nomor kurang dari 3, tambahkan '0' di depan | |
if len(number) < 3: | |
number = '0' + number | |
info = f"{alfabet.upper()}-{number}" | |
return info |