Deprecated-Unsupported/Python Projects/gtk/AnimeFreakDL/signal_classes/SearchWindow.py

101 lines
3.4 KiB
Python
Raw Normal View History

# Gtk imports
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk
# Python imports
import threading, subprocess, os, requests, json as JSON
# Application imports
def threaded(fn):
def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
return wrapper
class SearchWindow:
def __init__(self, settings):
self.settings = settings
self.builder = self.settings.returnBuilder()
self.searchStore = self.builder.get_object("searchStore")
self.endRangeStepper = self.builder.get_object("endRangeStepper")
self.baseUrlEntry = self.builder.get_object("baseUrlEntry")
self.titleLabel = self.builder.get_object("titleLabel")
self.scrptPth = os.path.dirname(os.path.realpath(__file__)) + "/"
def search(self, widget):
queryStr = widget.get_text().strip()
if queryStr != "":
self.searchStore.clear()
url = 'https://www.animefreak.tv/search/topSearch?q=' + queryStr
r = requests.get(url)
if r.status_code == 200:
json = JSON.loads(r.text)
data = self.parseJSON(json)
self.fillImageStore(data)
else:
print("::Error occured while getting JSON::")
print("Status code is: " + str(r.status_code))
def handleSelected(self, selection):
if selection:
model = selection.get_selected()[0] # Getting model
itr = selection.get_selected()[1] # Getting iter
title = model.get_value(itr, 0)
link = model.get_value(itr, 1)
count = model.get_value(itr, 2)
self.titleLabel.set_text(title)
self.baseUrlEntry.set_text(link)
self.endRangeStepper.set_value(int(count))
@threaded
def fillImageStore(self, data):
for show in data:
path = self.scrptPth + "../thumbnails/" + show["thumbnailName"]
if not os.path.isfile(path):
self.downloadThumbnail(show["thumbnailLink"], path)
pixbuf = gtk.Image.new_from_file(path).get_pixbuf()
self.searchStore.append([show["title"], show["link"],
show["count"], pixbuf])
def parseJSON(self, json):
data = []
for obj in json["data"]:
title = obj["name"]
seoName = obj["seo_name"]
count = str(1)
id = str(obj["anime_id"])
if obj["episodes"]:
try:
epiSeoName = obj["episodes"][0]["episode_seo_name"]
if not epiSeoName == "movie":
count = epiSeoName.split("-")[1]
except Exception as e:
print(obj["episodes"])
thumbnailName = seoName + ".jpg"
watchLink = "https://www.animefreak.tv/watch/" + seoName
thumbnailLink = "https://www.animefreak.tv/meta/anime/" + id + "/" + thumbnailName
data.append({"title" : title, "link": watchLink, "thumbnailName": thumbnailName,
"thumbnailLink": thumbnailLink, "count": count})
return data
def downloadThumbnail(self, thumbnailLink, path):
r = requests.get(thumbnailLink, allow_redirects=True)
open(path, 'wb').write(r.content)