#####################################################################
#                                                                   #
#  THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE   #
# LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet )  #
# FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk )      #
# WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
# IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
# OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE.        #
#                                                                   #
#      ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS       #
# (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK.  #
# YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
# THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER    #
# VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG   #
# WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ).         #
#                                                                   #
# THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE   #
# NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
# THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT    #
# SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
# THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT.       #
#                                                                   #
# THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL  #
# FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE   #
# THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD   #
# TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT.  #
#                                                                   #
#####################################################################

import os
from subprocess import *
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Pango


from flbry import ui
from flbry import parse
from flbry import fetch

################################################################################

# Markdown. Or .md file format is an easy way to give your simple text documents
# a bit of flare. Stuff like links, images and quotes are supported. Also bold
# an italic characters.

EMOJIS ={         # SIZE | LINK
    ":alien:"      :"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Alien%402x.png)",
    ":angry_1:"    :"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/angry%402x.png)",
    ":angry_2:"    :"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/angry%202%402x.png)",
    ":angry_3:"    :"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/angry%203%402x.png)",
    ":angry_4:"    :"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/angry%204%402x.png)",
    ":blind:"      :"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/blind%402x.png)",
    ":block:"      :"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/block%402x.png)",
    ":bomb:"       :"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/bomb%402x.png)",
    ":brain_chip:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Brain%20chip%402x.png)",
    ":confirm:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/CONFIRM%402x.png)",
    ":confused_1:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/confused%402x-1.png)",
    ":confused_2:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/confused%402x.png)",
    ":cooking_something_nice:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/cooking%20something%20nice%402x.png)",
    ":cry_1:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/cry%402x.png)",
    ":cry_2:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/cry%202%402x.png)",
    ":cry_3:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/cry%203%402x.png)",
    ":cry_4:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/cry%204%402x.png)",
    ":cry_5:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/cry%205%402x.png)",
    ":donut:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/donut%402x.png)",
    ":eggplant_with_condom:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/eggplant%20with%20condom%402x.png)",
    ":eggplant:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/eggplant%402x.png)",
    ":fire_up:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/fire%20up%402x.png)",
    ":flat_earth:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Flat%20earth%402x.png)",
    ":flying_saucer:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Flying%20saucer%402x.png)",
    ":heart_chopper:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/heart%20chopper%402x.png)",
    ":hyper_troll:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/HyperTroll%402x.png)",
    ":ice_cream:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/ice%20cream%402x.png)",
    ":idk:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/IDK%402x.png)",
    ":illuminati_1:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Illuminati%402x-1.png)",
    ":illuminati_2:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Illuminati%402x.png)",
    ":kiss_1:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/kiss%402x.png)",
    ":kiss_2:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/kiss%202%402x.png)",
    ":laser_gun:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/laser%20gun%402x.png)",
    ":laughing_1:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Laughing%402x.png)",
    ":laughing_2:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Laughing%202%402x.png)",
    ":lollipop:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Lollipop%402x.png)",
    ":love_1:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Love%402x.png)",
    ":love_2:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Love%202%402x.png)",
    ":monster:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Monster%402x.png)",
    ":mushroom:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/mushroom%402x.png)",
    ":nail_it:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Nail%20It%402x.png)",
    ":no:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/NO%402x.png)",
    ":ouch:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/ouch%402x.png)",
    ":peace:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/peace%402x.png)",
    ":pizza:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/pizza%402x.png)",
    ":rabbit_hole:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/rabbit%20hole%402x.png)",
    ":rainbow_puke_1:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/rainbow%20puke%402x-1.png)",
    ":rainbow_puke_2:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/rainbow%20puke%402x.png)",
    ":rock:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/ROCK%402x.png)",
    ":sad:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/sad%402x.png)",
    ":salty:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/salty%402x.png)",
    ":scary:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/scary%402x.png)",
    ":sleep:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Sleep%402x.png)",
    ":slime_down:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/slime%20down%402x.png)",
    ":smelly_socks:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/smelly%20socks%402x.png)",
    ":smile_1:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/smile%402x.png)",
    ":smile_2:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/smile%202%402x.png)",
    ":space_chad:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/space%20chad%402x.png)",
    ":space_doge:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/doge%402x.png)",
    ":space_green_wojak:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/space%20wojak%402x-1.png)",
    ":space_julian:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Space%20Julian%402x.png)",
    ":space_red_wojak:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/space%20wojak%402x.png)",
    ":space_resitas:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/resitas%402x.png)",
    ":space_tom:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/space%20Tom%402x.png)",
    ":spock:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/SPOCK%402x.png)",
    ":star:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Star%402x.png)",
    ":sunny_day:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/sunny%20day%402x.png)",
    ":surprised:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/surprised%402x.png)",
    ":sweet:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/sweet%402x.png)",
    ":thinking_1:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/thinking%402x-1.png)",
    ":thinking_2:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/thinking%402x.png)",
    ":thumb_down:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/thumb%20down%402x.png)",
    ":thumb_up_1:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/thumb%20up%402x-1.png)",
    ":thumb_up_2:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/thumb%20up%402x.png)",
    ":tinfoil_hat:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/tin%20hat%402x.png)",
    ":troll_king:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/Troll%20king%402x.png)",
    ":ufo:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/ufo%402x.png)",
    ":waiting:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/waiting%402x.png)",
    ":what:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/what_%402x.png)",
    ":woodoo_doll:":"![20](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/emoticons/48%20px/woodo%20doll%402x.png)"
}

STICKERS = {

    # TOTALLY GRATIS
    
    "<stkr>:CAT:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/CAT/PNG/cat_with_border.png)",
    "<stkr>:FAIL:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/FAIL/PNG/fail_with_border.png)",
    "<stkr>:HYPE:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/HYPE/PNG/hype_with_border.png)",
    "<stkr>:PANTS_1:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/PANTS/PNG/PANTS_1_with_frame.png)",
    "<stkr>:FIRE:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/fire.png)",
    "<stkr>:SLIME:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/SLIME/PNG/slime_with_frame.png)",
    "<stkr>:PISS:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/PISS/PNG/piss_with_frame.png)",
    "<stkr>:BULL_RIDE:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/BULL/PNG/bull-ride.png)",
    "<stkr>:ELIMINATED:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/ELIMINATED/PNG/eliminated.png)",
    "<stkr>:EGG_CARTON:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/egg_carton.png)",
    "<stkr>:BAN:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/BAN/PNG/ban.png)",
    "<stkr>:MONEY_PRINTER:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/money_printer.png)",
    "<stkr>:MOUNT_RUSHMORE:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/mount_rushmore.png)",
    "<stkr>:EGIRL:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/EGIRL/PNG/e-girl.png)",
    "<stkr>:KANYE_WEST:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/kanye_west.png)",
    "<stkr>:TAYLOR_SWIFT:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/taylor_swift.png)",
    "<stkr>:DONALD_TRUMP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/donald_trump.png)",
    "<stkr>:BILL_CLINTON:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/bill_clinton.png)",
    "<stkr>:EPSTEIN_ISLAND:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/epstein_island.png)",
    "<stkr>:KURT_COBAIN:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/kurt_cobain.png)",
    "<stkr>:BILL_COSBY:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/bill_cosby.png)",
    "<stkr>:CHE_GUEVARA:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/che_guevara.png)",
    "<stkr>:CHRIS_CHAN:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/MISC/PNG/chris_chan.png)",
    "<stkr>:PREGNANT_MAN_BLONDE:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/pregnant%20man/png/Pregnant%20man_white%20border_blondie.png)",
    "<stkr>:PREGNANT_WOMAN_BROWN_HAIR:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/pregnant%20woman/png/Pregnant%20woman_white_border_brown%20hair.png)",
    "<stkr>:ROCKET_SPACEMAN:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/ROCKET%20SPACEMAN/PNG/rocket-spaceman_with-border.png)",
    "<stkr>:SALTY:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/SALTY/PNG/salty.png)",
    "<stkr>:SICK_FLAME:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/SICK/PNG/sick2_with_border.png)",
    "<stkr>:SICK_SKULL:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/SICK/PNG/with%20borderdark%20with%20frame.png)",
    "<stkr>:SPHAGETTI_BATH:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/SPHAGETTI%20BATH/PNG/sphagetti%20bath_with_frame.png)",
    "<stkr>:THUG_LIFE:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/THUG%20LIFE/PNG/thug_life_with_border_clean.png)",
    "<stkr>:TRAP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TRAP/PNG/trap.png)",
    "<stkr>:TRASH:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TRASH/PNG/trash.png)",
    "<stkr>:WHUUT:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/WHUUT/PNG/whuut_with-frame.png)",

    # TECHNICALLY PAID ( FROM ODYSEE )

    "<stkr>:TIP_HAND_FLIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/tip_hand_flip_$%20_with_border.png)",
    "<stkr>:TIP_HAND_FLIP_COIN:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/tip_hand_flip_coin_with_border.png)",
    "<stkr>:TIP_HAND_FILP_LBC:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/tip_hand_flip_lbc_with_border.png)",
    "<stkr>:COMET_TIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/$%20comet%20tip%20with%20border.png)",
    "<stkr>:SILVER_ODYSEE_COIN:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/with%20bordersilver_odysee_coinv.png)",
    "<stkr>:LBC_COMET_TIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/LBC%20comet%20tip%20with%20border.png)",
    "<stkr>:SMALL_TIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/with%20bordersmall$_tip.png)",
    "<stkr>:SMALL_LBC_TIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/with%20bordersmall_LBC_tip%20.png)",
    "<stkr>:BITE_TIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/bite_$tip_with%20border.png)",
    "<stkr>:BITE_TIP_CLOSEUP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/bite_$tip_closeup.png)",
    "<stkr>:BITE_LBC_CLOSEUP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/LBC%20bite.png)",
    "<stkr>:MEDIUM_TIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/with%20bordermedium$_%20tip.png)",
    "<stkr>:MEDIUM_LBC_TIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/with%20bordermedium_LBC_tip%20%20%20%20%20%20%20%20%20%20.png)",
    "<stkr>:LARGE_TIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/with%20borderlarge$tip.png)",
    "<stkr>:LARGE_LBC_TIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/with%20borderlarge_LBC_tip%20.png)",
    "<stkr>:BIG_TIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/with%20borderbig$tip.png)",
    "<stkr>:BIG_LBC_TIP:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/big_LBC_TIPV.png)",
    "<stkr>:FORTUNE_CHEST:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/with%20borderfortunechest$_tip.png)",
    "<stkr>:FORTUNE_CHEST_LBC:<stkr>":"![100](https://thumbnails.odycdn.com/optimize/s:0:0/quality:85/plain/https://static.odycdn.com/stickers/TIPS/png/with%20borderfortunechest_LBC_tip.png)",
    
}

def Open(md):


    # Spliting it for the read.

    # EMOJIES CAN BE MORE THEN ONE IN THE TEXT
    for emoji in EMOJIS:
        md = md.replace(emoji, EMOJIS[emoji])

    # STICKERS ONLY ONE PER TEXT ( To make publishing from FastLBRY be compatible with
    # ODYSEE.COM )
    
    for sticker in STICKERS:
        if md == sticker:
            md = STICKERS[sticker]
            break
    
    md = "\n\n"+md
    md = md.split("\n")
    
    # First thing is I was to read the headings and convert it into a tree.
    
    
    tree = []
    indent = 1
    c = []

    skip = 0
    
    for n,line in enumerate(md):

        if skip > n:
            continue
    
        ty = "text"
        te = line
        
        # Here I want to simply get a type of each line. Later we going to parse
        # the links and other things. But first. Let's parse stuff based on
        # lines.

        if line.startswith("```"):

            # THREE ``` aka code block

            # This tag will block any other tags
            # untill it's untagged

            code = ""
            for l in md[n+1:]:
                if not l.startswith("```"):
                    code = code + l + "\n"
                    
                else:
                    skip = n + code.count("\n") + 2
                    break
            
            tree.append(["text_cm", code+"\n"])
            te = ""
            
        elif line.startswith("#"):
            # The titles of the chapter. The Headers are usually written similar
            # to how here in python you write comments. It's a # , space, and the
            # text.
            
            # The amount of hashes. ## or ### gives different sized text. Officialy
            # it should support up to 6 hashes. ######. But why not make it more
            # just in case.
            
            ty = line.count("#") # This might give bugs
        
        elif line.startswith(">"):
            
            # The > sign in the Markdown language is used for quatations. 
            
            ty = "text_q"
            te = te[1:]
            
        tree.append([ty, te+"\n"])        
        
    # Now the stage 0 is over and we parsed the basic things. Now is the hard
    # part to parse out all the images and stuff inside them. It's going to be
    # done per part. And we are going to use the same technique I used for the
    # conversion of the legacy projects. See : studio/story.py ( in VCStudio )
    
    # We are going to itterate over each letter. And decide what to do by that
    
    newtree = []
    
    for block in tree:

        if block[0] == "text_cm":
            newtree.append(block)
            continue
        
        part = ""
        skip = 0
        
        for n, l in enumerate(block[-1]):
            
            if skip > n:
                continue
            
            part = part + l
            
            # Here we are going to do something if a give condition is met.
            # Usually I gonna do something if [part] ends with a given markdown
            # thing. I don't have a manual of markdown on me. So please make it
            # more supported. I guess. I might forget things I rarely use.
            
            # Links are made with [stuff you click on](https://example.com)
            # but similar to it. Images are done ![Tooltip](Image.png)
            # and even weirder you can put one into the other. Like
            # [![Tooltip](Image.png)](https://example.com)
            # Which going to give you a clickable image.
            
            # For this version what we are going to do is next. 
            # If we got [![ then it's a clickable image
            # If we got ![ then it's just image
            # and if we got [ then it's a link.
        
            if part.endswith("[!["):
                
                # IMAGE LINK
                newtree.append([block[0], part[:-3]])

                tooltip = ""
                imageurl = ""
                url = ""
                t = False
                iu = False
                skip = n
                for le in block[-1][n:]: # For letters in the rest of text

                    skip = skip + 1

                    if le == "]":
                        t = True
                    elif le == ")" and t and not iu:
                        iu = True
                    elif le == ")" and t and iu:
                        break
                    elif not t:
                        tooltip = tooltip +le
                    elif t and not iu:
                        imageurl = imageurl + le
                    else:
                        url = url+le

                tooltip = tooltip[tooltip.find("[")+1:]
                imageurl = imageurl[imageurl.find("(")+1:]
                url = url[url.find("(")+1:]
                
                apnd = ["image_link", imageurl, url]
                
                newtree.append(apnd)

                part = ""
                
            
            elif part.endswith("!["):
                
                # IMAGE

                newtree.append([block[0], part[:-2]])
                
                tooltip = ""
                url = ""
                t = False
                skip = n
                for le in block[-1][n:]: # For letters in the rest of text

                    skip = skip + 1

                    if le == "]":
                        t = True
                    elif le == ")" and t:
                        break
                    elif not t:
                        tooltip = tooltip +le
                    else:
                        url = url+le

                tooltip = tooltip[tooltip.find("[")+1:]
                url = url[url.find("(")+1:]

                apnd = ["image", tooltip, url]
                newtree.append(apnd)
                
                part = ""
                
            
            elif part.endswith("[") and not block[-1][n:].startswith('[!['):
                
                # LINK
                newtree.append([block[0], part[:-1]])
                
                
                tooltip = ""
                url = ""
                t = False
                skip = n
                for le in block[-1][n:]: # For letters in the rest of text

                    skip = skip + 1

                    if le == "]":
                        t = True
                    elif le == ")" and t:
                        break
                    elif not t:
                        tooltip = tooltip +le
                    else:
                        url = url+le

                tooltip = tooltip[tooltip.find("[")+1:]
                url = url[url.find("(")+1:]
                    
                apnd = ["link", tooltip, url]
                newtree.append(apnd)
                
                part = ""
                
                
                
            # Now I want to deal with `, *, ** and ***. If you want to help me you
            # can implement other types. Such as _, __, ___ and so on. Markdown is
            # a very rich language. I'm going to use the cut down version I see other
            # people use.

            # BTW this is the time. Feb 28. When I switched from Gedit to GNU Emacs.
            # Interesting feeling using this programm. I kind a love it even tho
            # so many stuff in not intuitive. Like saving is not Ctrl - S but
            # Ctrl - X -> Ctrl - S.

            # Things like Alt-; to comment multiple lines at ones is HUGE. Also it
            # was built by programmers for programmers. So it's a very good tool.

            elif part.endswith("**") and not block[-1][n+2:].startswith('*'):
                
                # DOUBLE **
                
                newtree.append([block[0], part[:-2]])

                if block[0] == "text":
                    block[0] = "text_b"
                else:
                    block[0] = "text"

                part = ""
            
            elif part.endswith("*") and not block[-1][n+1:].startswith('*'):
                
                # SINGLE *
                
                newtree.append([block[0], part[:-1]])

                if block[0] == "text":
                    block[0] = "text_i"
                else:
                    block[0] = "text"

                part = ""


            elif part.endswith("`"):
                
                # SINGLE `
                
                newtree.append([block[0], part[:-1]])

                tmpart = block[-1][n+1:]
                tmpart = tmpart[:tmpart.find("`")]

                newtree.append(["text_c", tmpart])

                skip = n+len(tmpart)+2
                
                block[0] = "text"

                part = ""
            
        newtree.append([block[0], part])
        
        
    #newtree.append(["text", "\n"*20+" [END OF DOCUMENT] "])
    tree = newtree
        
    return(tree)

def search_convert(s):

    # This function convers a chapter name into a link
    # such links are use in notabug.org to link to chapters
    # for example example.com/file.md#chapter-name
    # With this url it will load the example.com/file.md and
    # then skip to the "Chapter Name" chapter.

    # This function transforms "Chapter Name" into "chapter-name"
    
    l = " ./\|[]{}()?!@#$%^&*`~:;'\"=,<>"
    s = s.lower().replace(" ","-")
    r = ""
    for i in s:
        if i not in l:
            r = r + i
    return r

def convert(win, text_view, imwidth=800):

    text_buffer = text_view.get_buffer()
    st = text_buffer.get_start_iter()
    en = text_buffer.get_end_iter()
    text = text_buffer.get_text(st, en, True)
    text_buffer.set_text("")

    try:
        text_buffer.create_tag("text", justification=Gtk.Justification.FILL, left_margin=64, right_margin=64)
        text_buffer.create_tag("text_c", background="#eeeeee", foreground="#000000", font="Monospace", left_margin=64, right_margin=64, justification=Gtk.Justification.LEFT)
        text_buffer.create_tag("text_b", font="Bold", justification=Gtk.Justification.FILL, left_margin=64, right_margin=64)
        text_buffer.create_tag("text_i", font="Italic", justification=Gtk.Justification.FILL, left_margin=64, right_margin=64)
        text_buffer.create_tag("text_q", justification=Gtk.Justification.FILL, left_margin=200, right_margin=200)
        text_buffer.create_tag("header", size_points=30, justification=Gtk.Justification.CENTER, left_margin=64, right_margin=64)
    except:
        pass
    text_view.set_justification(Gtk.Justification.CENTER)
    
    md = Open(text)
    markup = ""
    
    for i in md:
        
        en = text_buffer.get_end_iter()

        
        
        if type(i[0]) == str and i[0].startswith("text") and not i[0] == "text_cm":

            if i[-1].startswith("___") or i[-1].startswith("---"):

                # Inspired by ( and partially copied from ) LBRY-GTK by MorsMortium
                
                text_buffer.insert(en, "\n---")
                
                anchor = Gtk.TextChildAnchor()
                text_buffer.insert_child_anchor(en, anchor)

                def Expand(Widget, Discard):
                    Width = text_view.get_allocated_width()
                    Widget.set_size_request(Width, -1)
    
                
                B = Gtk.Box()
                Separator = Gtk.HSeparator()
                B.pack_start(Separator, 1,1,1)
                B.connect("draw", Expand)

                text_view.add_child_at_anchor(B , anchor)

                text_buffer.insert(en, "\n\n")
                continue

            elif (i[-1].startswith("lbry://") or i[-1].startswith("https://") or i[-1].startswith("http://")) and "youtu" not in i[-1] and i[0] != "text_c":

                # I will try to resolve it as a publication

                def try_resolve(url):
                    out = fetch.lbrynet("resolve", {"urls":parse.bar(url)})
                    
                    try:
                        out = out[parse.bar(url)]
                        is_channel = "value_type" in out and out["value_type"] == "channel"

                        return ["lbry", url, out]
                        
                    except:
                        return ["text", url, out]
                    
                def render_resolved(data):

                    t, url, out = data

                    if t == "text":
                        return Gtk.Label(url)
                    elif t == "lbry":
                        try:
                            return ui.search_item(win, out, True)
                        except:
                            return Gtk.Label(url)
                        
                        

                
                w = ui.load(win, try_resolve, render_resolved, i[-1])
                anchor = Gtk.TextChildAnchor()
                text_buffer.insert_child_anchor(en, anchor)
                text_view.add_child_at_anchor(w , anchor)
                
                continue
            

            try:
                text_buffer.insert_with_tags_by_name(en, i[-1], i[0])
            except:
                text_buffer.insert(en, i[-1])

        elif i[0] == "text_cm":
            codeview = Gtk.TextView()
            #codeview.override_background_color(Gtk.StateType.NORMAL, Gdk.RGBA(0.2,0.2,0.2, 1))
            #codeview.override_color(Gtk.StateType.NORMAL, Gdk.RGBA(0.9,0.9,0.9, 1))
            codeview.override_font(Pango.FontDescription("Monospace"))
            codescrl = Gtk.ScrolledWindow()
            codeframe = Gtk.Frame()
            codeframe.add(codescrl)
            codescrl.set_size_request(imwidth, min(i[-1].count("\n")*20, 500 ))
            codeview.set_editable(True)
            codescrl.add(codeview)
            codebuffer = codeview.get_buffer()
            codebuffer.set_text(i[-1])
            anchor = Gtk.TextChildAnchor()
            text_buffer.insert_child_anchor(en, anchor)
            text_view.add_child_at_anchor(codeframe , anchor)
                
        elif type(i[0]) == int:
            text_buffer.insert_with_tags_by_name(en, i[-1].replace("#", ""), "header")

        elif i[0] == "image_link":
            image = ui.load(win, ui.net_image_calculation, ui.net_image_render, i[1], imwidth, "", True)

            
            def link_launch(w, link):
                os.system("xdg-open "+link)
            def link_resolve(w, link):
                win.url.set_text(link)
                win.url.activate()

            optionsthing = Gtk.Popover()
            optbox = Gtk.VBox()
            optionsthing.add(optbox)
            optbox.pack_start(Gtk.Label("Link"), False, False, 5)
            opt_link = Gtk.Entry()
            opt_link.set_editable(False)
            opt_link.set_text(i[-1])
            optbox.pack_start(opt_link, False, False, False)
            opt_launch = Gtk.Button("Launch")
            optbox.pack_start(opt_launch, False, False, False)
            opt_resolve = Gtk.Button("Resolve")
            optbox.pack_start(opt_resolve, False, False, False)
            opt_launch.connect("clicked", link_launch, i[-1])
            opt_resolve.connect("clicked", link_resolve, i[-1])


            optbox.pack_start(Gtk.HSeparator(), False, False, 5)
            optbox.pack_start(Gtk.Label("Image"), False, False, 5)
            opt_link = Gtk.Entry()
            opt_link.set_editable(False)
            opt_link.set_text(i[1])
            optbox.pack_start(opt_link, False, False, False)
            opt2_open = Gtk.Button("Open")
            optbox.pack_start(opt2_open, False, False, False)
            opt2_launch = Gtk.Button("Launch")
            optbox.pack_start(opt2_launch, False, False, False)
            opt2_resolve = Gtk.Button("Resolve")
            optbox.pack_start(opt2_resolve, False, False, False)
            opt2_open.connect("clicked", link_launch, ui.image_save_name(i[1]))
            opt2_launch.connect("clicked", link_launch, i[1])
            opt2_resolve.connect("clicked", link_resolve, i[1])

            
            optbox.show_all()

            
            link = Gtk.MenuButton(popover=optionsthing)
            link.add(image)
            anchor = Gtk.TextChildAnchor()
            text_buffer.insert_child_anchor(en, anchor)
            text_view.add_child_at_anchor(link, anchor)

                
        elif i[0] == "image":

            try:
                loadw = int(i[1])
            except:
                loadw = imwidth

            
            image = ui.load(win, ui.net_image_calculation, ui.net_image_render, i[-1], loadw, "", True)
            
            def link_launch(w, link):
                os.system("xdg-open "+link)
            def link_resolve(w, link):
                win.url.set_text(link)
                win.url.activate()

            optionsthing = Gtk.Popover()
            optbox = Gtk.VBox()
            optionsthing.add(optbox)
            opt_link = Gtk.Entry()
            opt_link.set_editable(False)
            opt_link.set_text(i[-1])
            optbox.pack_start(opt_link, False, False, False)
            opt_open = Gtk.Button("Open")
            optbox.pack_start(opt_open, False, False, False)
            opt_launch = Gtk.Button("Launch")
            optbox.pack_start(opt_launch, False, False, False)
            opt_resolve = Gtk.Button("Resolve")
            optbox.pack_start(opt_resolve, False, False, False)
            opt_open.connect("clicked", link_launch, ui.image_save_name(i[-1]))
            opt_launch.connect("clicked", link_launch, i[-1])
            opt_resolve.connect("clicked", link_resolve, i[-1])
            optbox.show_all()
                
            link = Gtk.MenuButton(popover=optionsthing)
            link.set_relief(Gtk.ReliefStyle.NONE)
            link.add(image)
            anchor = Gtk.TextChildAnchor()
            text_buffer.insert_child_anchor(en, anchor)
            text_view.add_child_at_anchor(link, anchor)

        elif i[0] == "link":

            # link = Gtk.LinkButton.new_with_label(
            #     uri=i[-1],
            #     label=i[1])
            def link_launch(w, link):
                os.system("xdg-open "+link)
            def link_resolve(w, link):
                win.url.set_text(link)
                win.url.activate()

            optionsthing = Gtk.Popover()
            optbox = Gtk.VBox()
            optionsthing.add(optbox)
            opt_link = Gtk.Entry()
            opt_link.set_editable(False)
            opt_link.set_text(i[-1])
            optbox.pack_start(opt_link, False, False, False)
            opt_launch = Gtk.Button("Launch")
            optbox.pack_start(opt_launch, False, False, False)
            opt_resolve = Gtk.Button("Resolve")
            optbox.pack_start(opt_resolve, False, False, False)
            opt_launch.connect("clicked", link_launch, i[-1])
            opt_resolve.connect("clicked", link_resolve, i[-1])
            optbox.show_all()

            link = Gtk.MenuButton(label=i[1], popover=optionsthing)
            
            link.set_tooltip_text(i[-1])
            anchor = Gtk.TextChildAnchor()
            text_buffer.insert_child_anchor(en, anchor)

            text_view.add_child_at_anchor(link, anchor)


