ya trash
This commit is contained in:
1 parent
ef3fa2b0c7
commit
1ce449320c
75 files changed
+103953
-1264
No files matched your search
@@ -0,0 +1,7 @@
|
||||
{
|
||||
vk: {
|
||||
appId: '4202466',
|
||||
appSecret: '5NMWIbgb2ImAQeMeODEd'
|
||||
},
|
||||
outputDir: '/Users/tmshv/Yandex.Disk.localized/Hudozka/Site/Events'
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
#!/usr/local/bin/python3
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import string
|
||||
from datetime import datetime
|
||||
from functools import reduce
|
||||
from os import makedirs
|
||||
|
||||
import json5
|
||||
import sys
|
||||
|
||||
import requests
|
||||
|
||||
from utils import until, copy
|
||||
from vk import vk_init, vk_wall_get_by_id, vk_photos_get_albums, vk_photos_get
|
||||
|
||||
config = json5.load(open('config.json5', 'r'))
|
||||
vk_init(config['vk']['appId'], config['vk']['appSecret'])
|
||||
|
||||
pattern = re.compile(r'https?://vk\.com/wall([-\d]+)_(\d+)')
|
||||
ms = []
|
||||
|
||||
size_priority = ['w', 'z', 'y']
|
||||
|
||||
|
||||
# get_album_item = until(size_priority)
|
||||
|
||||
|
||||
def get_item_by_size(size, item):
|
||||
for s in item:
|
||||
if s['type'] == size:
|
||||
return s
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def get_album_item(item):
|
||||
for s in size_priority:
|
||||
img = get_item_by_size(s, item)
|
||||
if img:
|
||||
return img
|
||||
return None
|
||||
|
||||
|
||||
def is_valid_url(i: str):
|
||||
return pattern.match(i) is not None
|
||||
|
||||
|
||||
def add_middleware(var: str, fn):
|
||||
ms.append((var, fn))
|
||||
|
||||
|
||||
def apply_middleware(data: str, mw: tuple):
|
||||
key, fn = mw
|
||||
value = fn(data)
|
||||
form = {}
|
||||
form[mw[0]] = ''
|
||||
data.format(**form)
|
||||
return data
|
||||
|
||||
|
||||
def apply_middlewares(output_dir, template, data):
|
||||
result = template
|
||||
for mw in ms:
|
||||
key, fn = mw
|
||||
form = {key: fn(output_dir, data)}
|
||||
result = string.Template(result)
|
||||
result = result.safe_substitute(**form)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def attach_album(output_dir, data):
|
||||
"""
|
||||
{
|
||||
'size': 13,
|
||||
'title': 'Каллиграфия 27.11.2016',
|
||||
'owner_id': -132442859,
|
||||
'updated': 1480340404,
|
||||
'description': '',
|
||||
'id': '238711857',
|
||||
'thumb': {'user_id': 100, 'access_key': '428f3dad13bdd5d730', 'owner_id': -132442859,
|
||||
'date': 1480340404,
|
||||
'photo_807': 'https://pp.vk.me/c837134/v837134463/11056/ZaoO1h5Idaw.jpg',
|
||||
'id': 456239050,
|
||||
'width': 1024, 'photo_75': 'https://pp.vk.me/c837134/v837134463/11053/aHOALfgr0VU.jpg',
|
||||
'photo_130': 'https://pp.vk.me/c837134/v837134463/11054/czcuc7o-bN4.jpg', 'text': '',
|
||||
'photo_604': 'https://pp.vk.me/c837134/v837134463/11055/P7yUE6WitSA.jpg', 'height': 683,
|
||||
'photo_1280': 'https://pp.vk.me/c837134/v837134463/11057/eLG5Akgx3fk.jpg',
|
||||
'album_id': 238711857
|
||||
},
|
||||
'created': 1480340311
|
||||
}
|
||||
:param data:
|
||||
:return:
|
||||
"""
|
||||
oid = data['owner_id']
|
||||
aid = data['id']
|
||||
|
||||
album = vk_photos_get(oid, aid)['response']['items']
|
||||
|
||||
files = []
|
||||
for file in album:
|
||||
img = get_album_item(file['sizes'])
|
||||
if img:
|
||||
files.append(img['src'])
|
||||
# files.append(file['sizes'])
|
||||
|
||||
download_files(files, output_dir)
|
||||
return '\n'.join(list(
|
||||
map(
|
||||
lambda i: '' % i,
|
||||
map(
|
||||
lambda f: os.path.basename(f),
|
||||
files
|
||||
)
|
||||
)
|
||||
))
|
||||
|
||||
|
||||
def get_date(ts: int):
|
||||
date = datetime.fromtimestamp(ts)
|
||||
return date.strftime("%Y.%m.%d")
|
||||
|
||||
|
||||
def get_folder_name(post):
|
||||
date_formatted = get_date(post['date'])
|
||||
|
||||
return '{date} vk-{owner_id}-{id}'.format(
|
||||
date=date_formatted,
|
||||
owner_id=post['owner_id'],
|
||||
id=post['id'],
|
||||
)
|
||||
|
||||
|
||||
def download_files(files, output):
|
||||
for url in files:
|
||||
file_out = os.path.join(output, os.path.basename(url))
|
||||
req = requests.get(url)
|
||||
data = req.content
|
||||
f = open(file_out, 'wb')
|
||||
f.write(data)
|
||||
f.close()
|
||||
|
||||
|
||||
attachment_types = {
|
||||
'album': attach_album
|
||||
}
|
||||
|
||||
|
||||
def get_post_attachments(output_dir, data):
|
||||
attachments = []
|
||||
for a in data['attachments']:
|
||||
a_type = a['type']
|
||||
if a_type in attachment_types:
|
||||
fn = attachment_types[a_type]
|
||||
|
||||
attachment = fn(output_dir, a[a_type])
|
||||
attachments.append(attachment)
|
||||
|
||||
return '\n'.join(attachments)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
add_middleware('id', lambda _, data: 'vk-{owner_id}-{id}'.format(
|
||||
owner_id=data['owner_id'],
|
||||
id=data['id'],
|
||||
))
|
||||
|
||||
add_middleware('date', lambda _, data: get_date(data['date']))
|
||||
add_middleware('text', lambda _, data: data['text'])
|
||||
add_middleware('attachments', get_post_attachments)
|
||||
|
||||
article_file = open('template.md', 'r', encoding='utf-8')
|
||||
article = article_file.read()
|
||||
article_file.close()
|
||||
|
||||
urls = sys.argv[1:]
|
||||
for url in urls:
|
||||
if not is_valid_url(url):
|
||||
raise Exception('Cannot perform URL %s' % url)
|
||||
|
||||
vid = '%s_%s' % pattern.findall(url)[0]
|
||||
res = vk_wall_get_by_id(vid)
|
||||
if 'response' in res:
|
||||
post = res['response'][0]
|
||||
|
||||
if 'copy_history' in post:
|
||||
copy(post['copy_history'][0], post)
|
||||
|
||||
output_dir = os.path.join(config['outputDir'], get_folder_name(post))
|
||||
makedirs(output_dir, exist_ok=True)
|
||||
|
||||
article = apply_middlewares(output_dir, article, post)
|
||||
|
||||
article_file = open(os.path.join(output_dir, 'vk.md'), 'w', encoding='utf-8')
|
||||
article_file.write(article)
|
||||
article_file.close()
|
||||
|
||||
# print(json.dumps(post, indent=4, ensure_ascii=False))
|
||||
@@ -0,0 +1,8 @@
|
||||
---
|
||||
id: ${id}
|
||||
date: ${date}
|
||||
---
|
||||
|
||||
${text}
|
||||
|
||||
${attachments}
|
||||
@@ -0,0 +1,13 @@
|
||||
def until(keys: list):
|
||||
def select(value):
|
||||
for key in keys:
|
||||
if key in value:
|
||||
return value[key]
|
||||
return None
|
||||
|
||||
return select
|
||||
|
||||
|
||||
def copy(source: dict, destination: dict):
|
||||
for k, v in source.items():
|
||||
destination[k] = v
|
||||
@@ -0,0 +1,166 @@
|
||||
import requests
|
||||
from urllib.parse import urlencode
|
||||
|
||||
client_id = '4362480'
|
||||
client_secret = 'CI8hARpbqa5amp4OfTS6'
|
||||
access_token = '39c1712d4005b24bd9faddf34b7f4647a26afb4c7b1a935ba7680b24378a9acb3e0faed5b3af8a1e7879c'
|
||||
api_version = '5.52'
|
||||
VK_API = 'https://api.vk.com/method/'
|
||||
|
||||
|
||||
def vk_init(app_id, app_secret):
|
||||
global client_id
|
||||
global client_secret
|
||||
client_id = app_id
|
||||
client_secret = app_secret
|
||||
|
||||
|
||||
def http_get(url):
|
||||
req = requests.get(url)
|
||||
return req.json()
|
||||
|
||||
|
||||
def upload_file(url, file):
|
||||
req = requests.post(url, files={'photo': file})
|
||||
return req.json()
|
||||
|
||||
|
||||
def vk_auth(client_id, redirect_uri, scope, response_type):
|
||||
q = urlencode({
|
||||
'client_id': client_id,
|
||||
'scope': ','.join(scope),
|
||||
'v': api_version,
|
||||
'redirect_uri': redirect_uri,
|
||||
'display': 'page',
|
||||
'response_type': response_type
|
||||
})
|
||||
return 'https://oauth.vk.com/authorize?' + q
|
||||
|
||||
|
||||
def vk_get_access_token(client_id, client_secret, redirect_uri, code):
|
||||
q = urlencode({
|
||||
'client_id': client_id,
|
||||
'client_secret': client_secret,
|
||||
'code': code,
|
||||
'redirect_uri': redirect_uri,
|
||||
})
|
||||
uri = 'https://oauth.vk.com/access_token?' + q
|
||||
return http_get(uri)
|
||||
|
||||
|
||||
def vk_photos_get_wall_upload_server():
|
||||
q = urlencode({
|
||||
'access_token': access_token,
|
||||
})
|
||||
uri = VK_API + 'photos.getWallUploadServer?' + q
|
||||
return http_get(uri)['response']
|
||||
|
||||
|
||||
def vk_photos_save_wall_photo(server, hash, photo):
|
||||
q = urlencode({
|
||||
'access_token': access_token,
|
||||
'v': api_version,
|
||||
'server': server,
|
||||
'hash': hash,
|
||||
'photo': photo,
|
||||
})
|
||||
uri = VK_API + 'photos.saveWallPhoto?' + q
|
||||
return http_get(uri)
|
||||
|
||||
|
||||
def vk_wall_get_by_id(ids):
|
||||
if not isinstance(ids, list):
|
||||
ids = [ids]
|
||||
|
||||
q = urlencode({
|
||||
'posts': ','.join(ids),
|
||||
'v': api_version,
|
||||
})
|
||||
uri = VK_API + 'wall.getById?{q}'.format(q=q)
|
||||
return http_get(uri)
|
||||
|
||||
|
||||
def vk_wall_post(owner_id, message, attachments):
|
||||
q = urlencode({
|
||||
'access_token': access_token,
|
||||
'v': api_version,
|
||||
'owner_id': owner_id,
|
||||
'message': message,
|
||||
'attachments': ','.join(attachments)
|
||||
})
|
||||
uri = VK_API + 'wall.post?{q}'.format(q=q)
|
||||
return http_get(uri)
|
||||
|
||||
|
||||
def vk_photos_get_albums(owner_id, album_ids):
|
||||
q = urlencode({
|
||||
'v': api_version,
|
||||
'owner_id': owner_id,
|
||||
'album_ids': ','.join(album_ids),
|
||||
'photo_sizes': '1',
|
||||
'need_covers': '1',
|
||||
})
|
||||
uri = VK_API + 'photos.getAlbums?{q}'.format(q=q)
|
||||
return http_get(uri)
|
||||
|
||||
|
||||
def vk_photos_get(owner_id, album_id):
|
||||
q = urlencode({
|
||||
'v': api_version,
|
||||
'owner_id': owner_id,
|
||||
'album_id': album_id,
|
||||
'photo_sizes': '1',
|
||||
})
|
||||
uri = VK_API + 'photos.get?{q}'.format(q=q)
|
||||
return http_get(uri)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import os
|
||||
|
||||
print(os.getcwd())
|
||||
|
||||
# redirect_uri = 'http://tmshv.ru/callback'
|
||||
redirect_uri = 'http://vk.com/blank.html'
|
||||
|
||||
# print(vk_auth(client_id, redirect_uri, ['photos', 'wall', 'offline', 'messages'], 'token'))
|
||||
|
||||
# exit()
|
||||
|
||||
upload_url = vk_photos_get_wall_upload_server()['upload_url']
|
||||
print(upload_url)
|
||||
|
||||
# UPLOADING
|
||||
uploaded = upload_file(upload_url, open('1.jpg', 'rb'))
|
||||
print(uploaded)
|
||||
exit()
|
||||
|
||||
# uploaded_photo = uploaded['photo']
|
||||
saved_photo = vk_photos_save_wall_photo(**uploaded)
|
||||
print(saved_photo)
|
||||
|
||||
# {'response': [
|
||||
# {
|
||||
# 'text': '',
|
||||
# 'id': 420377937,
|
||||
# 'album_id': -14,
|
||||
# 'photo_75': 'http://cs631817.vk.me/v631817931/37c93/eiG89yDwZl4.jpg',
|
||||
# 'photo_130': 'http://cs631817.vk.me/v631817931/37c94/FDDyNTsJYYQ.jpg',
|
||||
# 'date': 1467142947,
|
||||
# 'width': 200,
|
||||
# 'owner_id': 1338931,
|
||||
# 'photo_604': 'http://cs631817.vk.me/v631817931/37c95/_TMbT8QHwAc.jpg',
|
||||
# 'height': 200}
|
||||
# ]
|
||||
# }
|
||||
|
||||
photo_id = saved_photo['response'][0]['id']
|
||||
photo_owner_id = saved_photo['response'][0]['owner_id']
|
||||
|
||||
attachment = '{type}{owner_id}_{media_id}'.format(
|
||||
type='photo',
|
||||
owner_id=photo_owner_id,
|
||||
media_id=photo_id
|
||||
)
|
||||
|
||||
post = vk_wall_post(photo_owner_id, '#api #test', [attachment])
|
||||
print(post)
|
||||
Reference in new issue
Block a user