Skip to content

Instantly share code, notes, and snippets.

@and7ey
Created March 17, 2024 16:43
Show Gist options
  • Save and7ey/310f914d6f5714e2c3a0ec4ab39e8aeb to your computer and use it in GitHub Desktop.
Save and7ey/310f914d6f5714e2c3a0ec4ab39e8aeb to your computer and use it in GitHub Desktop.
Скрипт на python для синхронизации списка Буду смотреть Кинопоиска между аккаунтами и добавления торрентов на TorrServer
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
import time
import os
import json
import sys
import requests
import re
# mode = 'SYNC_MOVIES_TO_WATCH'
mode = 'GET_MOVIES_DATA'
if mode == 'SYNC_MOVIES_TO_WATCH':
GET_MOVIES_LIST = True
DELETE_OLD_LIST = True
AUTHORIZE = True
PUT_LIKES = True
GET_TORRENTS = False
elif mode == 'GET_MOVIES_DATA':
GET_MOVIES_LIST = True
DELETE_OLD_LIST = False
AUTHORIZE = False
PUT_LIKES = False
GET_TORRENTS = True
driver = webdriver.Chrome()
wait = WebDriverWait(driver, timeout=120)
movies = []
if GET_MOVIES_LIST:
movies_list = 'https://www.kinopoisk.ru/user/12345/movies/list/type/3575/sort/default/vector/desc/perpage/200/#list' # change to your user id
print ('getting the page movies list...')
driver.implicitly_wait(10)
driver.get(movies_list)
time.sleep(10)
print ('starting to find movies...')
movies_li = driver.find_elements(By.XPATH, '//li[starts-with(@id,"film_")]')
if movies_li:
for li in movies_li:
url = None
kinopoisk_id = None
name = None
name_orig = None
year = None
poster = None
link = li.find_elements(By.CLASS_NAME, 'name')
if link:
url = link[0].get_attribute('href')
kinopoisk_id = url.replace('https://www.kinopoisk.ru/film/','').replace('/','')
name = link[0].text
if '(сериал)' in name:
name = name.replace(' (сериал)', '')
mtype = 'series'
else:
mtype = 'movie'
data = li.find_elements(By.TAG_NAME, 'span')
if data:
for d in data:
if 'мин' in d.text:
pattern = r"(?P<text>.*?)?\s?\((\d{4})\)"
match = re.search(pattern, data[0].text)
if match:
name_orig = match.group('text').strip()
year = match.group(2)
break
img = li.find_elements(By.TAG_NAME, 'img')
if img and len(img)>0:
poster = img[0].get_attribute('src')
if poster == 'https://st.kp.yandex.net/images/spacer.gif':
poster = None
movies.append({'url': url,
'id': kinopoisk_id,
'name': name,
'name_orig': name_orig,
'year': year,
'type': mtype,
'poster': poster})
print('movies found: ' + str(len(movies)))
if len(movies)>0: print('first movie: ' + str(movies[0]))
if AUTHORIZE:
print('logging to account...')
driver.implicitly_wait(5)
driver.get('https://kinopoisk.ru')
login_button = driver.find_element(By.XPATH, '//button[text()="Войти"]')
login_button.click()
wait.until(expected_conditions.title_is('Авторизация'))
# timeouts are needed for proper login
elem = driver.find_elements(By.NAME, 'login')[0]
elem.send_keys('user-id') # change to your user id
time.sleep(3)
elem.send_keys(Keys.RETURN)
time.sleep(3)
elem = driver.find_elements(By.NAME, 'passwd')[0]
elem.send_keys('password') # change to your password
time.sleep(3)
elem.send_keys(Keys.RETURN)
time.sleep(3)
print('check the browser to manually login...')
wait.until(expected_conditions.title_is('Кинопоиск. Все фильмы планеты.'))
if DELETE_OLD_LIST and AUTHORIZE: # doesn't work without authorization
print ('deleting old list...')
driver.get('https://www.kinopoisk.ru/mykp/folders/3575/?limit=100')
wait.until(expected_conditions.title_is('Кинопоиск — Все фильмы планеты'))
select_all = driver.find_element(By.ID, 'selectAll_div')
if select_all:
select_all.click()
delete_selected = driver.find_element(By.ID, 'delete_selected_upper_button')
if delete_selected:
delete_selected.click()
alert = wait.until(expected_conditions.alert_is_present())
alert.accept()
if PUT_LIKES and AUTHORIZE:
driver.implicitly_wait(3)
for m in movies:
print('opening movie ' + m['url'])
driver.get(m['url'])
try:
elem = driver.find_elements(By.CLASS_NAME, 'watch-online-button')
if elem:
print('... found online version')
time.sleep(3)
like_button = driver.find_element(By.XPATH, '//button[@title="Буду смотреть"]')
if like_button:
like_button.click()
print('... liked')
except Exception:
pass
if GET_TORRENTS and GET_MOVIES_LIST:
for m in movies:
print('looking for torrents for '+m['name']+'...')
url = 'https://jacred.ru/api/v1.0/torrents?search=%2B' + m['id']
time.sleep(5)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if len(data)>0:
filtered_data = [record for record in data if record['tracker'] in ['rutracker', 'nnmclub'] and
record['quality'] == 480 and
m['type'] in record['types'] and
m['year'] in record['title'] and
('BDRip' in record['title'] or 'HDRip' in record['title'] or 'HDTVRip' in record['title'] or 'DVDRip' in record['title'])]
if len(filtered_data)>0:
selected_record = max(filtered_data, key=lambda x: x['sid'])
magnet = selected_record.get('magnet')
title = selected_record.get('title')
print('... found magnet: ' + magnet)
print('adding torrent to TorrServer... ')
url = 'http://192.168.1.1:8090/torrents' # change to your TorrServer ip
data = {
'action': 'add',
'link': magnet,
'poster': m['poster'],
# 'data': title,
"save_to_db": True,
'title': m['name'] + ' (' + m['year'] + ')'
}
response = requests.post(url, data=json.dumps(data))
print('... result code: ' + str(response.status_code))
else:
print('... no torrents with needed quality found')
else:
print('... no torrents found')
elif response.status_code == 429:
tine.sleep(10)
print('... sleeping for 10 seconds')
else:
print('... failed to get torrents, result code: ' + str(response.status_code))
driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment