FAbrowser/scripts/SubViewer.gd
2024-07-22 11:06:25 -04:00

181 lines
5.5 KiB
GDScript

extends Container
class_name SubViewer
signal downloaded
const VIEW_URL_PREFIX = "https://www.furaffinity.net/view/"
const VIEW_URL_PREFIX_ALT = "http://www.furaffinity.net/view/"
export var id:int
var download_url:String
var clicked_link:String
var url:String
onready var dl_menu:PopupMenu = $"%Download".get_popup()
var user:String
# minified version of user name used in urls. all lowercase and no underscores:
var userurl:String
onready var main:Control = get_parent().owner
onready var grid:Control = main.get_node("%ThumbGrid")
onready var gather:GalleryGather = main.get_node("GalleryGather")
var FA = FAParser.new()
func _ready():
url = "https://www.furaffinity.net/view/%d/"%[id]
for i in Settings.settings.DownloadDirs.keys():
dl_menu.add_item(i)
dl_menu.connect("index_pressed",self,"download")
$"%User".get_popup().connect("id_pressed",self,"user_name_menu")
# check for cache file and load it if it exists. otherwise request from site.
var cache = File.new()
if cache.file_exists(str(Global.cache_dir,id)):
cache.open(str(Global.cache_dir,id),File.READ)
set_data(
cache.get_pascal_string(),cache.get_pascal_string(),
cache.get_pascal_string(),cache.get_pascal_string(),
cache.get_pascal_string()
)
cache.close()
else:
$http.request(url,Global.headers)
func _on_http_request_completed(result, response_code, headers, body):
FA.open_buffer(body)
var iurl = FA.get_sub_img_url()
set_data(
FA.get_sub_title(),FA.get_sub_user(),FA.get_sub_desc(),
iurl,FA.get_sub_download_url()
)
# create cache file for metadata if not error
if not iurl.empty():
var cache = File.new()
cache.open(str(Global.cache_dir,id),File.WRITE)
cache.store_pascal_string($"%name".text)
cache.store_pascal_string(user)
cache.store_pascal_string($"%desc".bbcode_text)
cache.store_pascal_string($"%img".url)
cache.store_pascal_string(download_url)
cache.close()
func set_data(title:String,uuser:String,desc:String,iurl:String,durl:String):
$"%name".text = title
user = uuser
userurl = uuser.replace("_","").to_lower()
if iurl.empty():
$"%User".text = "ERROR"
$"%User".modulate = Color(1,0,0,8)
$"%desc".bbcode_text = FA.get_sub_err()
$"%ImgScroll".visible = false
return
$"%User".text = "By "+uuser
$"%User".disabled = false
$"%desc".bbcode_text = desc
$"%img".url = iurl
download_url = durl
$http.queue_free()
$"%Download".disabled = false
func _on_img_gui_input(event:InputEvent):
if event.is_action_pressed("click") and event.is_pressed():
var isc = $"%ImgScroll"
var i = $"%img"
var mouse = i.get_local_mouse_position() / i.rect_size
$"%ImgScroll".scroll_horizontal
$"%img".expand = not $"%img".expand
yield(get_tree(),"idle_frame")
isc.scroll_horizontal = mouse.x * i.rect_size.x - (isc.rect_size.x*0.5)
isc.scroll_vertical = mouse.y * i.rect_size.y - (isc.rect_size.y*0.5)
func _on_desc_meta_clicked(meta:String):
if meta.begins_with("/"):
meta = "https://www.furaffinity.net" + meta
if meta.begins_with(VIEW_URL_PREFIX) or meta.begins_with(VIEW_URL_PREFIX_ALT):
var new_view_id:int = meta.trim_prefix(VIEW_URL_PREFIX).to_int()
get_parent().owner.open_preview(new_view_id)
return
clicked_link = meta
$LinkConf.dialog_text = "Do you want to open link:\n%s"%[meta]
$LinkConf.popup_centered_clamped()
func _on_LinkConf_confirmed():
OS.shell_open(clicked_link)
func _on_OpenBrowser_pressed():
OS.shell_open(url)
func download(index):
var dir:String = Settings.settings.DownloadDirs[dl_menu.get_item_text(index)]
print("downloading ",download_url)
var copy = Directory.new()
# copy file from cache to download folder if its in cache
var from = Global.cache_dir+download_url.md5_text()
var to = dir+"/"+download_url.get_file()
$"%Download".disabled = true
if copy.file_exists(to):
$"%DLStatus".dialog_text = "File already exists."
$"%DLStatus".popup_centered_clamped()
emit_signal("downloaded",id)
return
if copy.file_exists(from):
print("copying from %s to %s"%[from,to])
copy.copy(from,to)
$"%DLStatus".dialog_text = "Download successful."
$"%DLStatus".popup_centered_clamped()
else:
$dl.download_file = to
$dl.request(download_url)
var http_status = yield($dl,"request_completed")
if http_status[0] == OK and http_status[1] == 200:
$"%DLStatus".dialog_text = "Download successful."
else:
$"%DLStatus".dialog_text = "Download probably failed."
$"%DLStatus".popup_centered_clamped()
emit_signal("downloaded",id)
$"%Download".disabled = false
func user_name_menu(index):
match index:
0: # view gallery
#ok here we go! testing if this will work
gather.pages = ["/gallery/%s"%[userurl]]
1: # view scraps
gather.pages = ["/scraps/%s"%[userurl]]
2: # view both
gather.pages = ["/gallery/%s"%[userurl],"/scraps/%s"%[userurl]]
3: # view favorites
gather.pages = ["/favorites/%s"%[userurl]]
4: # open in browser
OS.shell_open("https://www.furaffinity.net/user/%s/"%[userurl])
5: # watch user
Settings.settings.Watches.push_back(user)
Settings.save_profile()
$"%Alert".dialog_text = "User added to watch list."
$"%Alert".popup_centered_clamped(Vector2(240,0))
6: # blacklist user
print(Settings.settings.UserBL)
Settings.settings.UserBL.push_back(user)
print(Settings.settings.UserBL)
Settings.save_profile()
$"%Alert".dialog_text = "User added to blacklist."
$"%Alert".popup_centered_clamped(Vector2(240,0))
for i in grid.get_children():
if i.user == user:
i.queue_free()
if index in range(4):
# clear grid
main.clear_grid()
gather.gather()
func _on_img_image_scaled():
$"%ScaleIndicator".visible = true