-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomic.py
More file actions
executable file
·89 lines (60 loc) · 2.28 KB
/
comic.py
File metadata and controls
executable file
·89 lines (60 loc) · 2.28 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
#!/usr/bin/python3
import requests
from bs4 import BeautifulSoup
import json
import webbrowser
import sys
URL = "http://www.commitstrip.com/en/?"
JSONFILE = './commitStrip.json'
# Helper functions to read and write json files
def load_json(filename):
with open(filename, 'r') as f:
return json.load(f)
def write_json_file(jsonObj, filename):
with open(filename, 'w') as f:
json.dump(jsonObj, f, indent=4)
def get_strip(section):
title = section.find('strong').text
url = section.find('a').get('href')
return {'title': title, 'url': url}
def store_data(soup):
data = [get_strip(section) for section in soup.find_all('section')]
write_json_file(data, JSONFILE)
def check_new(soup):
titles = [title.text for title in soup.find_all('strong')]
data = load_json(JSONFILE)
old_titles = [comic['title'] for comic in data]
if sorted(titles) == sorted(old_titles):
return False
return titles
def main():
try:
command = sys.argv[1]
if command == '-c':
response = requests.get(URL)
soup = BeautifulSoup(response.text, 'lxml')
new = check_new(soup)
if new:
store_data(soup)
print("\n\tNew Commic: {0}".format(new[0]))
else:
print("\n\tNo New Comics, Use comic -t to list available comics\n")
elif command == '-t':
data = load_json(JSONFILE)
titles = [comic['title'] for comic in data]
print('')
for i in xrange(len(titles)):
print('\t' + str(i) + '.\t' + titles[i])
print('')
elif command == '-o':
try:
option = int(sys.argv[2])
data = load_json(JSONFILE)
links = [comic['url'] for comic in data]
webbrowser.open(links[option])
except IndexError:
print("Comic Number missing")
except IndexError:
print("\n\tSimple script to check out commitStrip new comics\n\n\tAvailable Commmands:\n\n\t\t-c\t\t check new commics\n\t\t-t \t\t to list comics titles\n\t\t-o [number] \t to open comic link in browser\n")
if __name__ == '__main__':
main()