Welcome to mirror list, hosted at ThFree Co, Russian Federation.

attachments.py « extensions - github.com/mrDoctorWho/vk4xmpp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1aca767c667bee5e800799c5dda0fe86d7cfdeee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# coding: utf-8
# This file is a part of VK4XMPP transport
# © simpleApps, 2013 — 2016.

import re
import urllib
from printer import *

VK_AUDIO_SEARCH_LINK = "https://vk.com/search?c[q]=%s&c[section]=audio"
WALL_LINK = "https://vk.com/wall%(to_id)s_%(id)s"
WALL_COMMENT_LINK = "https://vk.com/wall%(owner_id)s_%(post_id)s?w=wall%(owner_id)s3_%(post_id)s"

PHOTO_SIZES = ("src_xxxbig", "src_xxbig", "src_xbig", "src_big", "src", "url", "src_small")
STICKER_SIZES = ("photo_256", "photo_128", "photo_64")

ATTACHMENT_REGEX = re.compile(r"^(?P<type>Photo|Document|Sticker)\:\s(?P<name>“.+?”\s—\s)?(?P<url>http[s]?:\/\/[^\s]+)$", re.UNICODE)

GLOBAL_USER_SETTINGS["parse_wall"] = {"value": 1, "label": "Parse wall attachments"}
GLOBAL_USER_SETTINGS["make_oob"] = {"value": 1, "label": "Allow OOB for attachments",
	"desc": "Attach incoming files as attachments,\nso they would be displayed by your client (if supported)"}


# The attachments that don't require any special movements
SIMPLE_ATTACHMENTS = {"doc": "Document: “%(title)s” — %(url)s",
	"link": "URL: %(title)s — %(url)s",
	"poll": "Poll: %(question)s",
	"page": "Page: %(title)s — %(view_url)s"}


def parseAttachments(self, msg, spacer=""):
	"""
	“parses” attachments from the json to a string
	"""
	result = ""
	if "attachments" in msg:
		attachments = msg["attachments"]
		# Add new line and "Attachments" if there some text added
		if msg.get("body") and len(attachments) > 1:
			result += chr(10) + spacer + _("Attachments:")
		if spacer:
			result += "\n"
		elif msg.get("body"):
			result += "\n"

		for num, attachment in enumerate(attachments):
			body = spacer
			type = attachment.get("type")
			current = attachment[type]
			if num > 0:
				body = chr(10) + spacer

			if type == "wall":
				if self.settings.parse_wall:
					tid = current.get("to_id", 1)
					name_ = self.vk.getName(tid)
					if tid > 0:
						name = "%s's" % name_
					else:
						name = "“%s”" % name_

					body += "Post on %s wall:\n" % name
					if current.get("text") or current.get("copy_text"):
						body += spacer + uhtml(compile_eol.sub("\n" + spacer, current["text"] or current.get("copy_text"))) + "\n"
					if current.get("attachments"):
						body += spacer + parseAttachments(self, current, spacer) + "\n" + spacer + "\n"
				body += spacer + ("Wall: %s" % WALL_LINK % current)

			elif type == "photo":
				for key in PHOTO_SIZES:
					if key in current:
						body += "Photo: %s" % current[key]  # No new line needed if we have just one photo and no text
						break

			elif type == "audio":
				current["performer"] = uhtml(current.get("performer", ""))
				current["title"] = uhtml(current.get("title", ""))
				current["url"] = VK_AUDIO_SEARCH_LINK % urllib.quote(str("%(artist)s %(title)s" % current))
				current["time"] = current["duration"] / 60.0
				body += "Audio: %(artist)s — “%(title)s“ (%(time)s min) — %(url)s" % current

			elif type == "sticker":
				for key in STICKER_SIZES:
					if key in current:
						body += "Sticker: %s" % current[key]
						break

			elif type == "wall_reply":
				# TODO: What if it's a community? from_id will be negative.
				# TODO: Remove "[idxxx|Name]," from the text or make it a link if XHTML is allowed
				current["name"] = self.vk.getName(current["uid"])
				current["text"] = uhtml(compile_eol.sub("\n" + spacer, current["text"]))
				current["url"] = WALL_COMMENT_LINK % current

				body += "Commentary to the post on a wall:\n"
				body += spacer + "<%(name)s> %(text)s\n" % current
				body += spacer + "Post URL: %(url)s" % current

			elif type == "video":
				current["title"] = current.get("title", "Untitled")

				current["desc"] = ""
				if current.get("description"):
					current["desc"] += uhtml(compile_eol.sub(" / ", "%(description)s, " % current))

				current["desc"] += "%(views)d views" % current
				current["time"] = "%d:%d" % (current["duration"] // 60, current["duration"] % 60)

				body += "Video: %(title)s (%(desc)s, %(time)s min) — https://vk.com/video%(owner_id)s_%(vid)s" % current

			elif type in SIMPLE_ATTACHMENTS:
				body += SIMPLE_ATTACHMENTS[type] % current

			else:
				body += "Unknown attachment: %s\n%s" % (type, str(current))
			result += body
	return result


def attachments_msg03(msg, destination, source):
	body = msg.getBody()
	if body:
		if msg.getType() == "groupchat":
			user = Chat.getUserObject(destination)
		else:
			user = Users.get(destination)
		if user and user.settings.make_oob:
			match = ATTACHMENT_REGEX.match(body.encode("utf-8"))
			if match:
				link = match.group("url")
				typ = match.group("type")
				name = match.group("name")
				if link and name:
					# shorten links only for audio messages
					# todo: is there a better way to detect them?
					# probably having "psv4." in the domain might
					# be considered as private storage and hence shouldn't be downloaded by us
					if typ == "Document" and ".ogg" in name:
						try:
							link = urllib.urlopen(link).url
						except Exception:
							crashLog("attachments_msg03")
							logger.error("unable to fetch real url for link %s and (jid: %s)", (link, user.source))
				oob = msg.setTag("x", namespace=xmpp.NS_OOB)
				oob.setTagData("url", link)
				msg.setBody(link)



registerHandler("msg03", attachments_msg03)
registerHandler("msg01", parseAttachments)