Good day.

In django, when trying to read the latest entries on the wall of the VKontakte group, wall.get returns the text of the entry in the text field. At the same time, if there is a link to the VC group in the post body, then the returned text contains in square brackets the group id and its name, separated by a vertical bar. For example:

  _'text':'[clubXXXXXXX|Название группы]Текст записи на стене'_. 

All this is displayed on the page. How to beat this behavior?

  • I am not a python expert, but in java you can take, for example, the desired character for the index of its position in the string, in your case these are the two characters "[" and "]" - >> index1 = yourText.indexOf ("[") and index2 = yourText.indexOf ("]") so you will get character indices between which you don’t need text, then you can create a new string from the source type text = yourText.subString (index2, yourText.lenght) where index2 is the place with which starts the text of the record and yourText.lenght is the length of the source text, this number will be the index value of the last element. - Kirill Stoianov
  • Well, the point is that the name of the group should be present in the context of writing on the wall. Example: "Friends, today a vote is being held in the GroupName group, in which we invite you to take part." The name of the Group is in the form of a link and when trying to read this post by the wall.get function, the return value contains: 'text': 'Friends, today in the group [clubXXXXXXX | Name of the Group] a vote is taken, in which we invite you to participate.' I think that this behavior can be somehow suppressed or circumvented. But I do not know how. - Vladimir Linnik 5:09

1 answer 1

I made a request to the technical support of Vkontakte developers, the answer is to only process the lines enclosed in square brackets and make a replacement. If someone needs to give the code:

 # Если в тексте записи на стене ВКонтакте встречается ссылка на # группу ВКонтакте, то ссылка возвращается в виде [clubXXXXXXXX|Название группы] # pattern_link находит все такие подстроки в записи на стене. # pattern_group выделяет из соответствующего pattern_link текста подстроку между | и ] # после чего происходит замена подстроки # Параметры: # текст записи на стене def replace_link_with_group(record): # Построка в квадратных скобках,начинающаяся с club и имеющая разделитель | pattern_link = re.compile(ur'(\[club[^\[\]]+\])', re.UNICODE) # Подстрока между | закрывающей квадратной скобкой pattern_group = re.compile(ur'\|([^\[\]]+)\]', re.UNICODE) to_replace = re.findall(pattern_link, record['text']) message = record['text'] for pos in to_replace: replace_by = re.findall(pattern_group, pos) message = message.replace(pos, replace_by[0]) return message 

Powered by django 1.9