-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweet_archive_logger.py
92 lines (71 loc) · 2.22 KB
/
tweet_archive_logger.py
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
import csv
username = 'jobbogamer'
csv_path = '/Users/Josh/Downloads/tweets/tweets.csv'
output_path = '/Users/Josh/Downloads/tweets.md.txt'
tweet_format = '''
{{text}}
[{{date}}]({{url}})
---
'''
date_format = '{{month}} {{day}}, {{year}} at {{hour_12}}:{{minute}}{{am}}'
def get_12_hour(hour):
if int(hour) in [0, 12]:
return '12'
elif int(hour) > 12:
hour = str(int(hour) - 12)
while len(hour) < 2:
hour = '0' + hour
return hour
def get_month(n):
d = {1: "January", 2: "February", 3: "March", 4: "April", 5: "May",
6: "June", 7: "July", 8: "August", 9: "September", 10: "October",
11: "Novermber", 12: "December"}
return d.get(n) # None if n not in range 1-12
def format_date(date, format):
year = date[:4]
month = date[5:7]
day = date[8:10]
hour = date[11:13]
minute = date[14:16]
second = date[17:19]
am = "AM" if int(hour) < 12 else "PM"
hour12 = get_12_hour(hour)
out = format
out = out.replace("{{year}}", year)
out = out.replace("{{month_num}}", month)
out = out.replace("{{month}}", get_month(int(month)))
out = out.replace("{{month_short}}", get_month(int(month))[0:3])
out = out.replace("{{day}}", day)
out = out.replace("{{hour}}", hour)
out = out.replace("{{hour_12}}", get_12_hour(hour))
out = out.replace("{{minute}}", minute)
out = out.replace("{{second}}", second)
out = out.replace("{{am}}", am)
return out
def format_tweet(tweet, format):
tweet_id = 0
in_reply_to_status_id = 1
in_reply_to_user_id = 2
timestamp = 3
source = 4
text = 5
retweeted_status_id = 6
retweeted_status_user_id = 7
retweeted_status_timestamp = 8
expanded_urls = 9
base_url = 'http://twitter.com/' + username + '/status/'
out = format
out = out.replace("{{id}}", tweet[tweet_id])
out = out.replace("{{date}}", format_date(tweet[timestamp], date_format))
out = out.replace("{{text}}", tweet[text])
out = out.replace("{{url}}", base_url + tweet[tweet_id])
return out
output = ''
with open(csv_path, 'rb') as csvfile:
reader = csv.reader(csvfile)
for row in reversed(list(reader)):
if (row[0] != "tweet_id"):
output += format_tweet(row, tweet_format)
output_file = open(output_path, 'wb')
output_file.write(output)
output_file.close()