| @@ -639,7 +639,7 @@ def get_contents_of(tmp_file_name): |
| 639 | return temp_file.read() |
639 | return temp_file.read() |
| 640 | |
640 | |
| 641 | def run_script(script: str): |
641 | def run_script(script: str): |
| 642 | os.system(f"{current_dir}/scripts/{script}.sh") |
642 | os.system(f"/tmp/curd/{script}.sh") |
| 643 | |
643 | |
| 644 | def write_to_tmp(tmp_filename:str, content:str): |
644 | def write_to_tmp(tmp_filename:str, content:str): |
| 645 | try: |
645 | try: |
| @@ -784,8 +784,192 @@ def load_config() -> dict: |
| 784 | |
784 | |
| 785 | return config_dict |
785 | return config_dict |
| 786 | |
786 | |
| |
787 | # ----------------------------------------------- Create Bash Script ---------------------------------------- |
| |
788 | |
| |
789 | def create_anime_list(): |
| |
790 | script_path = "/tmp/curd/anime_list.sh" |
| |
791 | with open(script_path, "w") as _: |
| |
792 | _.write(r"""search_anime() { |
| |
793 | search_gql="query( \$search: SearchInput \$limit: Int \$page: Int \$translationType: VaildTranslationTypeEnumType \$countryOrigin: VaildCountryOriginEnumType ) { shows( search: \$search limit: \$limit page: \$page translationType: \$translationType countryOrigin: \$countryOrigin ) { edges { _id name availableEpisodes __typename } }}" |
| |
794 | |
| |
795 | curl -e "$allanime_refr" -s -G "${allanime_api}/api" --data-urlencode "variables={\"search\":{\"allowAdult\":false,\"allowUnknown\":false,\"query\":\"$1\"},\"limit\":40,\"page\":1,\"translationType\":\"$mode\",\"countryOrigin\":\"ALL\"}" --data-urlencode "query=$search_gql" -A "$agent" | sed 's|Show|\ |
| |
796 | |g' | sed -nE "s|.*_id\":\"([^\"]*)\",\"name\":\"([^\"]*)\".*${mode}\":([1-9][^,]*).*|\1 \2 (\3 episodes)NEWLINEFROMHERE|p" |
| |
797 | } |
| |
798 | |
| |
799 | agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0" |
| |
800 | allanime_refr="https://allanime.to" |
| |
801 | allanime_base="allanime.day" |
| |
802 | allanime_api="https://api.${allanime_base}" |
| |
803 | mode="${ANI_CLI_MODE:-sub}" |
| |
804 | download_dir="${ANI_CLI_DOWNLOAD_DIR:-.}" |
| |
805 | quality="${ANI_CLI_QUALITY:-best}" |
| |
806 | case "$(uname -a)" in |
| |
807 | *Darwin*) player_function="${ANI_CLI_PLAYER:-iina}" ;; # mac OS |
| |
808 | *ndroid*) player_function="${ANI_CLI_PLAYER:-android_mpv}" ;; # Android OS (termux) |
| |
809 | *steamdeck*) player_function="${ANI_CLI_PLAYER:-flatpak_mpv}" ;; # steamdeck OS |
| |
810 | *MINGW* | *WSL2*) player_function="${ANI_CLI_PLAYER:-mpv.exe}" ;; # Windows OS |
| |
811 | *ish*) player_function="${ANI_CLI_PLAYER:-iSH}" ;; # iOS (iSH) |
| |
812 | *) player_function="${ANI_CLI_PLAYER:-mpv}" ;; # Linux OS |
| |
813 | esac |
| |
814 | |
| |
815 | # Read the entire file content into a variable |
| |
816 | script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| |
817 | query=$(cat /tmp/curd/curd_query) # This is a shorthand for cat "$filename" |
| |
818 | |
| |
819 | # query="one piece" |
| |
820 | anime_list=$(search_anime "$query") |
| |
821 | |
| |
822 | echo $anime_list > /tmp/curd/curd_anime_list |
| |
823 | """) |
| |
824 | os.chmod(script_path, 0o755) |
| |
825 | |
| |
826 | def create_episode_list(): |
| |
827 | script_path = "/tmp/curd/episode_list.sh" |
| |
828 | with open(script_path, "w") as _: |
| |
829 | _.write(r"""episodes_list() { |
| |
830 | episodes_list_gql="query (\$showId: String!) { show( _id: \$showId ) { _id availableEpisodesDetail }}" |
| |
831 | |
| |
832 | curl -e "$allanime_refr" -s -G "${allanime_api}/api" --data-urlencode "variables={\"showId\":\"$*\"}" --data-urlencode "query=$episodes_list_gql" -A "$agent" | sed -nE "s|.*$mode\":\[([0-9.\",]*)\].*|\1|p" | sed 's|,|\ |
| |
833 | |g; s|"||g' | sort -n -k 1 |
| |
834 | } |
| |
835 | |
| |
836 | # setup |
| |
837 | agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0" |
| |
838 | allanime_refr="https://allanime.to" |
| |
839 | allanime_base="allanime.day" |
| |
840 | allanime_api="https://api.${allanime_base}" |
| |
841 | mode="${ANI_CLI_MODE:-sub}" |
| |
842 | download_dir="${ANI_CLI_DOWNLOAD_DIR:-.}" |
| |
843 | quality="${ANI_CLI_QUALITY:-best}" |
| |
844 | case "$(uname -a)" in |
| |
845 | *Darwin*) player_function="${ANI_CLI_PLAYER:-iina}" ;; # mac OS |
| |
846 | *ndroid*) player_function="${ANI_CLI_PLAYER:-android_mpv}" ;; # Android OS (termux) |
| |
847 | *steamdeck*) player_function="${ANI_CLI_PLAYER:-flatpak_mpv}" ;; # steamdeck OS |
| |
848 | *MINGW* | *WSL2*) player_function="${ANI_CLI_PLAYER:-mpv.exe}" ;; # Windows OS |
| |
849 | *ish*) player_function="${ANI_CLI_PLAYER:-iSH}" ;; # iOS (iSH) |
| |
850 | *) player_function="${ANI_CLI_PLAYER:-mpv}" ;; # Linux OS |
| |
851 | esac |
| |
852 | script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| |
853 | id=$(cat /tmp/curd/curd_id) |
| |
854 | echo $(episodes_list "$id") > /tmp/curd/curd_episode_list |
| |
855 | """) |
| |
856 | os.chmod(script_path, 0o755) |
| |
857 | |
| |
858 | def create_episode_url(): |
| |
859 | script_path = "/tmp/curd/episode_url.sh" |
| |
860 | with open(script_path, "w") as _: |
| |
861 | _.write(r"""# # extract the video links from response of embed urls, extract mp4 links form m3u8 lists |
| |
862 | get_links() { |
| |
863 | episode_link="$(curl -e "$allanime_refr" -s "https://${allanime_base}$*" -A "$agent" | sed 's|},{|\ |
| |
864 | |g' | sed -nE 's|.*link":"([^"]*)".*"resolutionStr":"([^"]*)".*|\2 >\1|p;s|.*hls","url":"([^"]*)".*"hardsub_lang":"en-US".*|\1|p')" |
| |
865 | |
| |
866 | case "$episode_link" in |
| |
867 | *repackager.wixmp.com*) |
| |
868 | extract_link=$(printf "%s" "$episode_link" | cut -d'>' -f2 | sed 's|repackager.wixmp.com/||g;s|\.urlset.*||g') |
| |
869 | for j in $(printf "%s" "$episode_link" | sed -nE 's|.*/,([^/]*),/mp4.*|\1|p' | sed 's|,|\ |
| |
870 | |g'); do |
| |
871 | printf "%s >%s\n" "$j" "$extract_link" | sed "s|,[^/]*|${j}|g" |
| |
872 | done | sort -nr |
| |
873 | ;; |
| |
874 | *vipanicdn* | *anifastcdn*) |
| |
875 | if printf "%s" "$episode_link" | head -1 | grep -q "original.m3u"; then |
| |
876 | printf "%s" "$episode_link" |
| |
877 | else |
| |
878 | extract_link=$(printf "%s" "$episode_link" | head -1 | cut -d'>' -f2) |
| |
879 | relative_link=$(printf "%s" "$extract_link" | sed 's|[^/]*$||') |
| |
880 | curl -e "$allanime_refr" -s "$extract_link" -A "$agent" | sed 's|^#.*x||g; s|,.*|p|g; /^#/d; $!N; s|\ |
| |
881 | | >|' | sed "s|>|>${relative_link}|g" | sort -nr |
| |
882 | fi |
| |
883 | ;; |
| |
884 | *) [ -n "$episode_link" ] && printf "%s\n" "$episode_link" ;; |
| |
885 | esac |
| |
886 | # echo $episode_link >> links |
| |
887 | # [ -z "$ANI_CLI_NON_INTERACTIVE" ] && printf "\033[1;32m%s\033[0m Links Fetched\n" "$provider_name" 1>&2 |
| |
888 | [ -z "$ANI_CLI_NON_INTERACTIVE" ] 1>&2 |
| |
889 | } |
| |
890 | |
| |
891 | # # innitialises provider_name and provider_id. First argument is the provider name, 2nd is the regex that matches that provider's link |
| |
892 | provider_init() { |
| |
893 | provider_name=$1 |
| |
894 | provider_id=$(printf "%s" "$resp" | sed -n "$2" | head -1 | cut -d':' -f2 | sed 's/../&\ |
| |
895 | /g' | sed 's/^01$/9/g;s/^08$/0/g;s/^05$/=/g;s/^0a$/2/g;s/^0b$/3/g;s/^0c$/4/g;s/^07$/?/g;s/^00$/8/g;s/^5c$/d/g;s/^0f$/7/g;s/^5e$/f/g;s/^17$/\//g;s/^54$/l/g;s/^09$/1/g;s/^48$/p/g;s/^4f$/w/g;s/^0e$/6/g;s/^5b$/c/g;s/^5d$/e/g;s/^0d$/5/g;s/^53$/k/g;s/^1e$/\&/g;s/^5a$/b/g;s/^59$/a/g;s/^4a$/r/g;s/^4c$/t/g;s/^4e$/v/g;s/^57$/o/g;s/^51$/i/g;' | tr -d '\n' | sed "s/\/clock/\/clock\.json/") |
| |
896 | } |
| |
897 | |
| |
898 | # # +s links based on given provider |
| |
899 | generate_link() { |
| |
900 | case $1 in |
| |
901 | 1) provider_init "wixmp" "/Default :/p" ;; # wixmp(default)(m3u8)(multi) -> (mp4)(multi) |
| |
902 | 2) provider_init "dropbox" "/Sak :/p" ;; # dropbox(mp4)(single) |
| |
903 | 3) provider_init "wetransfer" "/Kir :/p" ;; # wetransfer(mp4)(single) |
| |
904 | 4) provider_init "sharepoint" "/S-mp4 :/p" ;; # sharepoint(mp4)(single) |
| |
905 | *) provider_init "gogoanime" "/Luf-mp4 :/p" ;; # gogoanime(m3u8)(multi) |
| |
906 | esac |
| |
907 | [ -n "$provider_id" ] && get_links "$provider_id" |
| |
908 | # echo $links >> links |
| |
909 | } |
| |
910 | |
| |
911 | select_quality() { |
| |
912 | case "$1" in |
| |
913 | best) result=$(printf "%s" "$links" | head -n1) ;; |
| |
914 | worst) result=$(printf "%s" "$links" | grep -E '^[0-9]{3,4}' | tail -n1) ;; |
| |
915 | *) result=$(printf "%s" "$links" | grep -m 1 "$1") ;; |
| |
916 | esac |
| |
917 | [ -z "$result" ] && printf "Specified quality not found, defaulting to best\n" 1>&2 && result=$(printf "%s" "$links" | head -n1) |
| |
918 | printf "%s" "$result" | cut -d'>' -f2 |
| |
919 | } |
| |
920 | |
| |
921 | script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| |
922 | # # gets embed urls, collects direct links into provider files, selects one with desired quality into $episode |
| |
923 | get_episode_url() { |
| |
924 | # get the embed urls of the selected episode |
| |
925 | episode_embed_gql="query (\$showId: String!, \$translationType: VaildTranslationTypeEnumType!, \$episodeString: String!) { episode( showId: \$showId translationType: \$translationType episodeString: \$episodeString ) { episodeString sourceUrls }}" |
| |
926 | |
| |
927 | resp=$(curl -e "$allanime_refr" -s -G "${allanime_api}/api" --data-urlencode "variables={\"showId\":\"$id\",\"translationType\":\"$mode\",\"episodeString\":\"$ep_no\"}" --data-urlencode "query=$episode_embed_gql" -A "$agent" | tr '{}' '\n' | sed 's|\\u002F|\/|g;s|\\||g' | sed -nE 's|.*sourceUrl":"--([^"]*)".*sourceName":"([^"]*)".*|\2 :\1|p') |
| |
928 | # generate links into sequential files |
| |
929 | cache_dir="$(mktemp -d)" |
| |
930 | providers="1 2 3 4 5" |
| |
931 | for provider in $providers; do |
| |
932 | generate_link "$provider" >"$cache_dir"/"$provider" & |
| |
933 | done |
| |
934 | wait |
| |
935 | # echo $links > links |
| |
936 | # select the link with matching quality |
| |
937 | links=$(cat "$cache_dir"/* | sed 's|^Mp4-||g;/http/!d' | sort -g -r -s) |
| |
938 | rm -r "$cache_dir" |
| |
939 | episode=$(select_quality "$quality") |
| |
940 | echo $links > /tmp/curd/curd_links |
| |
941 | [ -z "$episode" ] && die "Episode not released!" |
| |
942 | } |
| |
943 | |
| |
944 | # setup |
| |
945 | agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0" |
| |
946 | allanime_refr="https://allanime.to" |
| |
947 | allanime_base="allanime.day" |
| |
948 | allanime_api="https://api.${allanime_base}" |
| |
949 | sub_or_dub=$(cat /tmp/curd/curd_mode) |
| |
950 | mode="${ANI_CLI_MODE:-$sub_or_dub}" |
| |
951 | download_dir="${ANI_CLI_DOWNLOAD_DIR:-.}" |
| |
952 | quality="${ANI_CLI_QUALITY:-best}" |
| |
953 | case "$(uname -a)" in |
| |
954 | *Darwin*) player_function="${ANI_CLI_PLAYER:-iina}" ;; # mac OS |
| |
955 | *ndroid*) player_function="${ANI_CLI_PLAYER:-android_mpv}" ;; # Android OS (termux) |
| |
956 | *steamdeck*) player_function="${ANI_CLI_PLAYER:-flatpak_mpv}" ;; # steamdeck OS |
| |
957 | *MINGW* | *WSL2*) player_function="${ANI_CLI_PLAYER:-mpv.exe}" ;; # Windows OS |
| |
958 | *ish*) player_function="${ANI_CLI_PLAYER:-iSH}" ;; # iOS (iSH) |
| |
959 | *) player_function="${ANI_CLI_PLAYER:-mpv}" ;; # Linux OS |
| |
960 | esac |
| |
961 | id=$(cat /tmp/curd/curd_id) |
| |
962 | ep_no=$(cat /tmp/curd/curd_ep_no) |
| |
963 | get_episode_url |
| |
964 | """) |
| |
965 | os.chmod(script_path, 0o755) |
| |
966 | |
| 787 | # ----------------------------------------------- Start of script ---------------------------------------- |
967 | # ----------------------------------------------- Start of script ---------------------------------------- |
| 788 | |
968 | |
| |
969 | create_anime_list() |
| |
970 | create_episode_list() |
| |
971 | create_episode_url() |
| |
972 | |
| 789 | current_dir = Path(__file__).parent |
973 | current_dir = Path(__file__).parent |
| 790 | # current_dir = os.path.dirname(os.path.abspath(__file__)) |
974 | # current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 791 | # print(current_dir) |
975 | # print(current_dir) |