-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsave_for_twitter.py
More file actions
executable file
·115 lines (87 loc) · 2.91 KB
/
save_for_twitter.py
File metadata and controls
executable file
·115 lines (87 loc) · 2.91 KB
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
"""
The MIT License (MIT)
Copyright (c) 2014 Marat S.
"""
from twython import Twython
try:
import Credentials
except Exception:
Credentials = None
"""
код, который выкачивает твиты аккаунта через API твиттера,
выдирает оттуда все ссылки, в том числе на картинки,
выкачивает и кладёт на диск в виде
[
{
idтвита: N,
дата: Date,
другие параметры твита...,
текст_твита_с_ссылками_заменёнными_на_медиа_айдишники: Text,
медиа: {
медиаid1:binaryData,
медиаid2:binaryData
}
},
{....}
]
"""
def connectToTwitter():
APP_KEY = ''
APP_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''
if Credentials is not None:
APP_KEY = Credentials.APP_KEY
APP_SECRET = Credentials.APP_SECRET
OAUTH_TOKEN = Credentials.OAUTH_TOKEN
OAUTH_TOKEN_SECRET = Credentials.OAUTH_TOKEN_SECRET
if APP_KEY == '' or OAUTH_TOKEN == '':
print("You should setup twitter application,"
"and use its Credentials in APP_KEY etc.")
return
return Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
def loadTimeline(twitter):
return twitter.get_user_timeline()
# def replace_url_in_twit_with_id_to_media_id():
def analyseMediaInTwit(text, media):
indices = _allIndicesOfMedia(text, media)
print(indices)
def _allIndicesOfMedia(text, media):
indicesArray = []
if 'urls' in media:
a = _indicesOfMedia(text, media['urls'])
if len(a) > 0:
indicesArray.append(a)
if 'media' in media:
a = _indicesOfMedia(text, media['media'])
if len(a) > 0:
indicesArray.append(a)
reveredUrlIndices = sorted(indicesArray, reverse=True)
return reveredUrlIndices
def _indicesOfMedia(text, media):
indicesArray = []
for mediaUrl in media:
# urlStarts = mediaUrl['indices'][0]
# urlEnds = mediaUrl['indices'][1]
indicesArray += mediaUrl['indices']
return indicesArray
def saveForTwitterTimeline():
twitter = connectToTwitter()
if twitter:
timelineJson = None
try:
timelineJson = loadTimeline(twitter)
except Exception:
print('Credentials are wrong??')
else:
for twitMeta in timelineJson:
# print(twitMeta.keys())
if 'entities' in twitMeta:
print(twitMeta['entities'])
analyseMediaInTwit(twitMeta['text'], twitMeta['entities'])
# parsed = parseTimeline(timelineJson)
# saveToDisk(parsed)
def main():
saveForTwitterTimeline()
if __name__ == '__main__':
main()