@IEATPYTHON/

8C7 Energy Diet Top 10 Videos

Python

No description

fork
loading
Files
  • main.py
  • requirements.txt
main.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
from requests_html import HTMLSession
import re
from flask import Flask

counter = 0
allvotes = [] 
voteorganization = [] 
session = HTMLSession()
print("GETTING LINKS")
r = session.get('http://energydiet.canadiangeographic.ca/2019/main/video_gallery')
print("DONE GETTING LINKS")
links = r.html.links
reallinks = list(links)

for i in range(len(reallinks)):
  print("GETTING LINK ", i)
  e = session.get(reallinks[counter])
  print("GOT LINK ", i)
  votes = [element.text for element in e.html.find(".video_info")] 
  votes = str(votes)
  votes = re.sub(r'.*/', '/', votes)
  votes = votes[85:]
  votes = votes[:-2]

  for i in range(2):
    votes = votes[votes.find("\\") + 1:]
    votes = votes[votes.find("n") + 1:]

  #if "8C7" in votes:
    #print('\033[93m','\033[1m', votes, '\033[0m')
  #else:
    #print(votes) 
  
  numvotes = votes[:-6]

  numvotes = votes[votes.find("n")+1:] 
 # print(numvotes)
  allvotes.append(votes)
  #voteorganization.append()
  #print(voteorganization)
  counter = counter + 1
print("done preparing reallinks")
vote_by_idx = []
vote_idx = 0
for vote in allvotes:
  if(len(vote) > 1):
    vote_info = vote.split("\\n")
    num_votes = vote_info[len(vote_info)-1]
    num_votes = num_votes[0:len(num_votes)-5]
    #print("VOTES FOR " + vote_info[0] + ":" + num_votes )
    # make an index for the votes_by_idx in allvotes
    # of a float numvotes.allvotesidx, then we can use this after it is sorted to get the "allvotes" list sorted by vote.
    vote_by_idx_idx = str(num_votes.strip()) + "." + str(vote_idx)
    #print("NEW VOTE IDX IS " + str(vote_by_idx_idx)) 
    vote_by_idx.append(float(vote_by_idx_idx))
  vote_idx = vote_idx + 1
vote_by_idx = sorted(vote_by_idx,reverse=True,key=float)
html = "<h1>8C7 Top 10 Energy Diet Videos List</h1>"
vote_sorted_idx = 1
html = html + "<ol>"
for vote_sorted in vote_by_idx:
  vote_sorted = str(vote_sorted)
  #print("VOTE SORTED RAW:", vote_sorted)
  indexes = vote_sorted.split(".")
  pretty_name = allvotes[int(indexes[1])][0:allvotes[int(indexes[1])].rfind("\\n")]
  #print("RAW: " + allvotes[int(indexes[1])])
  #print(vote_sorted_idx, ". ", pretty_name , " ", indexes[0], " votes")
  html = html + "<li>" + pretty_name + " " + str(indexes[0]) + " votes" + "</li>"
  vote_sorted_idx = vote_sorted_idx + 1
#allvotes = str(allvotes)
#html = "<h1><"+str(allvotes)+"><h1>"
html = html + "</ol>"
#print(str(html))
app = Flask(__name__) 

@app.route("/")
def main():
  return html
if __name__ == '__main__':
  app.run(host='0.0.0.0')