Skip to content

Instantly share code, notes, and snippets.

@nonefffds
Last active November 15, 2022 16:42
Show Gist options
  • Save nonefffds/5349ccc28e39f5693535e03402a3d2e5 to your computer and use it in GitHub Desktop.
Save nonefffds/5349ccc28e39f5693535e03402a3d2e5 to your computer and use it in GitHub Desktop.
bilibili av bv converter w/ AppleScript
#bilibili-av-bv-converter
#Date: March 29,2020
#Language: AppleScript
#License: WTFPL licence
#Algorithm Implementations
#Global Varibles
global S
global table
global xor
global add
global tr
set S to {11, 10, 3, 8, 4, 6}
set table to every character of "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF"
set xor to 177451812
set add to "8728348608"
set tr to {}
#Position Looking from certain list, copying from https://www.macosxautomation.com/applescript/sbrt/sbrt-07.html
on list_position(this_item, this_list)
#AppleScript is Case-insensitive while doing things, so we need to manually make it to consider it is cased or not.
considering case
repeat with i from 1 to the count of this_list
if item i of this_list is equal to this_item then return i
end repeat
return 0
end considering
end list_position
#List to String method, copying from https://stackoverflow.com/questions/37289223/convert-applescript-list-into-string
on list2string(theList, theDelimiter)
-- First, we store in a variable the current delimiter to restore it later
set theBackup to AppleScript's text item delimiters
-- Set the new delimiter
set AppleScript's text item delimiters to theDelimiter
-- Perform the conversion
set theString to theList as string
-- Restore the original delimiter
set AppleScript's text item delimiters to theBackup
return theString
end list2string
#BtoA Function Implementation
on BtoA(Input)
#Preparations
set X to every character of text returned of Input
set R to 0
#algorithm of BtoA
repeat with i from 1 to 6
set a to item i of S
set b to item (a + 1) of X
set t to (list_position(b, table))
set t to (t - 1)
set i to (i - 1)
set curr to (t * (58 ^ i))
set R to R + curr as inches as string #Kinda annoying it would output a form of scientific notation when big number is involved, so we need to make it to any type of units, like inches, kilos etc.
end repeat
set beforeResult to (R - add) as string
set resultBtoA to (do shell script "echo $(( (" & R & "-" & add & ") ^ " & xor & ")) ")
end BtoA
##AtoB function Implementation
on AtoB(Input)
#Preparations
set X to every character of text returned of Input
set R to 0
#Algorithms of AtoB
set X to (do shell script "echo $(( (" & X & " ^ " & xor & ")+" & add & ")) ")
set R to {"B", "V", "1", "", "", "4", "", "1", "7", "", "", ""}
repeat with i from 1 to 6
set a to item i of S
set c to X div (58 ^ (i - 1)) mod 58
set item (a + 1 as number) of R to item (c + 1) of table
end repeat
set resultAtoB to list2string(R, "")
end AtoB
#Check Implementation(Input1 for AV Number, Input2 for BV Number, reverse is not appicable.)
#This check is algorithm based, the real check from bilibili site is not used in this case, maybe will add that later
on Check(Input1, Input2)
if AtoB(Input1) is equal to Input2 then
display dialog "Valid"
else
display dialog "Not valid"
end if
#JavaScript may be involved later, now I just put it like this
#do javascript (file "Macintosh\ HD/Users/share/check-avbv.jsx") with arguments{"Input1", "Input2"}
end Check
#UI Implementation
#Mode Selection(AppleScript only support 3 buttons, so no cancel button after adding check)
set stringOfDialog to "How to convert?"
set returnRecord to display dialog stringOfDialog buttons {"A to B", "B to A", "Check"} default button 2
set buttonPressed to button returned of returnRecord
#Stage2
if returnRecord is {button returned:"A to B"} then
set Input to display dialog "Input the AV number you wish to be converted with" default answer ""
display dialog AtoB(Input)
else if returnRecord is {button returned:"B to A"} then
set Input to display dialog "Input the BV number you wish to be converted with" default answer ""
display dialog BtoA(Input)
else if returnRecord is {button returned:"Check"} then
set Input1 to display dialog "Input the AV Number you wish to be checked with" default answer ""
set Input2 to display dialog "Input the BV Number you wish to be checked with" default answer ""
display dialog Check(Input1, Input2)
end if
#Here's some examples for testing
#BV17x411w7KC <-> 170001
#BV1Q541167Qg <-> 455017605
#BV1mK4y1C7Bz <-> 882584971
@nonefffds
Copy link
Author

nonefffds commented Mar 28, 2020

Original Code by mcdx on Zhihu with Python written for reference, original post see here
Another derivative
This is a piece of code rewritten with AppleScript, just for studying AppleScript, UI is implemented, and now is theoretically functional but partially not.(The check part is only support local algorithm checking, but not checking with bilibili websites, so there's maybe some issues)
Under WTFPL licence as well as the original one.

@nonefffds
Copy link
Author

Bug Detected:
av837532303 -> BV1Jg4y1a9M(Not Valid), BV1Jg4y1a79M(Valid)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment