Skip to content

Instantly share code, notes, and snippets.

@Shiroizu
Created October 9, 2023 18:33
Show Gist options
  • Save Shiroizu/b2bb2600e4128ccdd8cb1b43193e4ee8 to your computer and use it in GitHub Desktop.
Save Shiroizu/b2bb2600e4128ccdd8cb1b43193e4ee8 to your computer and use it in GitHub Desktop.
import requests, json, time, os
# Recheck files containing Niconico links (to see if a song entry exists on VocaDB)
# Accepted line formats:
# http://www.nicovideo.jp/watch/sm42561422 13161 サマーリウム feat.小春六花
# http://www.nicovideo.jp/watch/sm42561422
# sm42561422
event_id =
file_dir = ""
script_user = ""
login = {
"UserName": "",
"password": ""
}
# ----- # ----- # ----- # ----- # ----- # ----- #
# 1. Recheck files line-by-line
found_songs = []
event_missing = []
file_dir = file_dir if (file_dir[-1] == "/") else (file_dir + "/")
for file in os.listdir(file_dir):
missing_songs = []
with open(file_dir + file, encoding="utf-8") as f:
lines = f.read().splitlines()
print(f"Re-checking file '{file}' ({len(lines)} lines)")
for line in lines:
nico_url = line.split()[0]
if "/watch/" in nico_url:
nico_id = nico_url.split("/")[-1]
else:
nico_id = nico_url
vocadb_api_url = "https://vocadb.net/api/songs/byPv?pvService=NicoNicoDouga&fields=ReleaseEvent&pvId="
entry = requests.get(vocadb_api_url + nico_id).json()
if entry:
song_id = entry["id"]
print(f"Entry found for {nico_id}: {song_id}")
found_songs.append(song_id)
entry_events_ids = [event["id"] for event in entry["releaseEvents"]]
if event_id in entry_events_ids:
#print(f" Event {event_id} found")
pass
else:
print(f" Event missing")
event_missing.append(song_id)
else:
print(f"Entry not found for {nico_id}")
missing_songs.append(line)
time.sleep(0.5)
print(f"Total of {len(lines) - len(missing_songs)} songs added from the list")
with open(file_dir + "(recheck) " + file, "w", encoding="utf-8") as f:
f.write("\n".join(missing_songs))
with open(f"found songs (E{event_id}, recheck).txt", "w") as f:
for song in found_songs:
f.write(f"https://vocadb.net/S/{song}\n")
print(f"\nTotal of {len(found_songs)} songs added since the last check")
print(f"{len(event_missing)} of those had the release event missing\n")
if not event_missing:
exit()
# ----- # ----- # ----- # ----- # ----- # ----- #
# 2. Update the VocaDB entries for missing events
pause = input("Press enter to launch the Event Adder.\n")
entries_updated = 0
with requests.Session() as s:
website = "https://vocadb.net"
login = s.post(website + "/User/Login", data=login)
token = s.get(website + "/api/antiforgery/token")
request_token = token.cookies.get_dict()["XSRF-TOKEN"]
s.headers.update({"requestVerificationToken": request_token})
s.headers.update({'user-agent': 'event adder bot v4'})
s.headers.update({"X-XSRF-TOKEN": request_token})
s.cookies["XSRF-TOKEN"] = request_token
for song_id in event_missing:
request_entry_data = s.get(f"{website}/api/songs/{song_id}/for-edit")
entry_data = request_entry_data.json()
print(f"Adding the event {event_id} to S/{song_id}")
entry_event_ids = [event["id"] for event in entry_data["releaseEvents"]]
if event_id in entry_event_ids:
print(f"Entry {entry_data['id']} already has the event added!")
continue
else:
entry_data["releaseEvents"].append({"id": event_id})
entry_data["updateNotes"] = f"added event {event_id} (by {script_user})"
request_save = s.post(f"{website}/api/songs/{song_id}", {"contract": json.dumps(entry_data)})
if (request_save.status_code != 200):
print("Entry update failed:")
print(request_save.request.url)
print(request_save.status_code)
print(request_save.reason)
break
entries_updated += 1
time.sleep(1)
print(f"\n{entries_updated} entries updated.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment